npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-logger-aknudsen

v1.3.1

Published

Lightweight, unobtrusive, configurable JavaScript logger

Downloads

4

Readme

js-Logger Build Status npm version npm dependencies

Lightweight, unobtrusive, configurable JavaScript logger.

logger.js will make you rich, famous and want for almost nothing - oh and it's a flexible abstraction over using console.log as well.

Installation

js-Logger has zero dependencies and comes with AMD and CommonJS module boilerplate. If the last sentence meant nothing to you then just lob the following into your page:

<script src="https://raw.github.com/jonnyreeves/js-logger/master/src/logger.min.js"></script>

Usage

Nothing beats the sheer ecstasy of logging! js-Logger does its best to not be awkward and get in the way. If you're the sort of person who just wants to get down and dirty then all you need is one line of code:

// Log messages will be written to the window's console.
Logger.useDefaults();

Now, when you want to emit a red-hot log message, just drop one of the following (the syntax is identical to the console object)

Logger.debug("I'm a debug message!");
Logger.info("OMG! Check this window out!", window);
Logger.warn("Purple Alert! Purple Alert!");
Logger.error("HOLY SHI... no carrier.");

Log messages can get a bit annoying; you don't need to tell me, it's all cool. If things are getting too noisy for your liking then it's time you read up on the Logger.setLevel method:

// Only log WARN and ERROR messages.
Logger.setLevel(Logger.WARN);
Logger.debug("Donut machine is out of pink ones");  // Not a peep.
Logger.warn("Asteroid detected!");  // Logs "Asteroid detected!", best do something about that!

// Ah, you know what, I'm sick of all these messages.
Logger.setLevel(Logger.OFF);
Logger.error("Hull breach on decks 5 through to 41!");  // ...

Log Handler Functions

All log messages are routed through a handler function which redirects filtered messages somewhere. You can configure the handler function via Logger.setHandler nothing that the supplied function expects two arguments; the first being the log messages to output and the latter being a context object which can be inspected by the log handler.

Logger.setHandler(function (messages, context) {
	// Send messages to a custom logging endpoint for analysis.
	// TODO: Add some security? (nah, you worry too much! :P)
	jQuery.post('/logs', { message: messages[0], level: context.level });
});

Default Log Handler Function

When you invoke Logger.useDefaults(), you can specify a default LogLevel and a custom logFormatter function which can alter the messages printed to the console:

Logger.useDefaults({
	logLevel: Logger.WARN,
	formatter: function (messages, context) {
		messages.unshift('[MyApp]');
		if (context.name) messages.unshift('[' + context.name + ']');
	}
})

In order to get the default log handler function, you may call Logger.getDefaultHandler().

Named Loggers

Okay, let's get serious, logging is not for kids, it's for adults with serious software to write and mission critical log messages to trawl through. To help you in your goal, js-Logger provides 'named' loggers which can be configured individual with their own contexts.

// Retrieve a named logger and store it for use.
var myLogger = Logger.get('ModuleA');
myLogger.info("FizzWozz starting up");

// This logger instance can be configured independent of all others (including the global one).
myLogger.setLevel(Logger.WARN);

// As it's the same instance being returned each time, you don't have to store a reference:
Logger.get('ModuleA').warn('FizzWozz combombulated!");

Note that Logger.setLevel() will also change the current log filter level for all named logger instances; so typically you would configure your logger levels like so:

// Create a couple of named loggers (typically in their own module)
var loggerA = Logger.get('LoggerA');
var loggerB = Logger.get('LoggerB');

// Configure log levels.
Logger.setLevel(Logger.WARN);  // Global logging level.
Logger.get('LoggerB').setLevel(Logger.DEBUG);  // Enable debug logging for LoggerB

Profiling

Sometimes its good to know what's taking so damn long; you can use Logger.time() and Logger.timeEnd() to keep tabs on things, the default log handler implementation delegates to the equivalent console methods if they exist, or write to console.log if they don't.

// Start timing something
Logger.time('self destruct sequence');

// ... Some time passes ...

// Stop timing something.
Logger.timeEnd('self destruct sequence'); // logs: 'self destruct sequence: 1022ms'.

Note that time and timeEnd methods are also provided to named Logger instances.