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.
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:
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;
Now, when you want to emit a red-hot log message, just drop one of the following (the syntax is identical to the
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
// Only log WARN and ERROR messages.Logger;Logger; // Not a peep.Logger; // Logs "Asteroid detected!", best do something about that!// Ah, you know what, I'm sick of all these messages.Logger;Logger; // ...
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.
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:
In order to get the default log handler function, you may call
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;myLogger;// This logger instance can be configured independent of all others (including the global one).myLogger;// As it's the same instance being returned each time, you don't have to store a reference:Logger;
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;var loggerB = Logger;// Configure log levels.Logger; // Global logging level.Logger; // Enable debug logging for LoggerB
Sometimes its good to know what's taking so damn long; you can use
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 somethingLogger;// ... Some time passes ...// Stop timing something.Logger; // logs: 'self destruct sequence: 1022ms'.
timeEnd methods are also provided to named Logger instances.