Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

MIT License Build Status Code Climate Codacy Badge Coverage Status

#console-logger'Hello Legacy %s!', 'world', { 'extra': ['pass-through params'] }); 
// Hello Legacy %s! world >Object { "extra": "pass-through params" }

// enhance logging
consoleLogger.prefixPattern = '%s::[%s]>';
consoleLogger.datetimePattern = 'LLL'; // moment's localization pattern
consoleLogger.logLevels = {
    '*': consoleLogger.LEVEL.INFO,
    'main': consoleLogger.LEVEL.WARN,
    'main.subB': consoleLogger.LEVEL.TRACE
};'Hello %s!', 'World'); // June 4, 2015 7:43 AM::[global]> Hello World!

console.getLogger('banana').debug('Hello brave new world!'); // ignored, logging set to INFO for '*'
console.getLogger('main.subA').info('Hello brave new world!'); // ignored, doesn't pass logging threshold of 'main'
console.getLogger('main.subB').info('Hello %s!', 'brave new world', { 'extra': ['pass-through params'] });
// June 4, 2015 7:44 AM::[main.subB]> Hello World! >Object { "extra": "pass-through params" }


## About

This library enhances the console logging capabilities. It works with any console object by modifying the arguments going into the original functions. As such it is able to 'decorate' the browser console, but also console shims and polyfills such as Firebug Lite or log4javascript in page console.

  • Enhances the standard console's logging functions so that you can define separate contexts to log for, where the output will be prepended with the context's name and a datetime stamp.
  • Further enhances the logging functions so that you can apply patterns eliminatinging the need of manually concatenating your strings
  • Introduces log levels, where you can manage logging output per context or even a group of contexts
  • Works as a complete drop-in replacement for your current console logging statements
## Installing

console-logger has optional dependencies on momentjs and sprintf.js: without moment you can't pattern a nicely readable datetime stamp and without sprintf you can't pattern your logging input lines. Default fixed patterns are applied if either they are missing.

#### Bower
bower install console-logger

// optionally (but recommended):
bower install sprintf
bower install moment
#### Manually

Include console-logger.js, momentjs and sprintf.js in your web app.

## Getting Started

After installing, console-logger should have registered a global consoleLogger on the Window object.

If available, it will be exported as constructor on module.exports or exports. However, outside of unit testing this in nodejs itself, this has not been tested to work properly. Register an issue if you run into trouble.

## Applying Patterns #### Prefix pattern

By default, the prefix is formatted like so:

datetime here::[context's name here]>your logging input here

However, you can change this as follows:

consoleLogger.prefixPattern = '%s - %s: ';
console.getLogger('app').info('Hello World');
// was:    Sunday 12:55:07 am::[app]>Hello World
// became: Sunday 12:55:07 am - app: Hello World

You can also remove it completely, or have just the datetime stamp or just the context prefixed:

// by the power of sprintf!
consoleLogger.prefixPattern = '%s - %s: '; // both
consoleLogger.prefixPattern = '%s: '; // timestamp
consoleLogger.prefixPattern = '%1$s: '; // timestamp by index
consoleLogger.prefixPattern = '%2$s: '; // context by index
consoleLogger.prefixPattern = '%2$s - %1$s: '; // both, reversed

This works, because console-logger will use two arguments for the prefix, which can be referenced by index.

#### Datetime stamp patterns

If you have included moment.js in your webapp, you can start using datetime stamp patterns with console-logger. The default pattern is dddd h:mm:ss a, which translates to Sunday 12:55:07 am. You customize the pattern as follows:

consoleLogger.datetimePattern = 'dddd';
console.getLogger()('app').info('Hello World');
// was:    Sunday 12:55:07 am::[app]>Hello World
// became: Sunday::[app]>Hello World

This way you can switch to a 24h format this way as well, for example, or use your locale-specific format.

#### Logging patterns

If you have included sprintf.js in your webapp, you can start using patterns with console-logger.

Traditional style with console:

logger.error ("Error uploading document [" + filename + "], Error: '" + err.message + "'. Try again later.")
// Error uploading document [contract.pdf], Error: 'Service currently down'. Try again later. "{ ... }"

Modern style with console-logger enhanced console.error:

var logger = console.getLogger("myapp.file-upload");
logger.error("Error uploading document [%s], Error: '%s'. Try again later.", filename, err.message)
// Sunday 12:13:06 pm::[myapp.file-upload]> Error uploading document [contract.pdf], Error: 'Service currently down'. Try again later.

You can even combine pattern input and normal input:

var logger = console.getLogger('test');
logger.warn("This %s pattern %j", "is", "{ 'in': 'put' }", "but this is not!", ['this', 'is', ['handled'], 'by the browser'], { 'including': 'syntax highlighting', 'and': 'console interaction' });
// 17-5-2015 00:16:08::[test]>  This is pattern "{ 'in': 'put' }" but this is not! ["this", "is handled", "by the browser"] Object {including: "syntax highlighting", and: "console interaction"}

To log an Object, you now have three ways of doing it, but the combined solution shown above has best integration with the browser.

logger.warn("Do it yourself: " + JSON.stringify(obj)); // json string with stringify's limitations
logger.warn("Let sprintf handle it: %j", obj); // json string with sprintf's limitations
logger.warn("Let the browser handle it: ", obj); // interactive tree in the browser with syntax highlighting
logger.warn("Or combine all!: %s, %j", JSON.stringify(obj), obj, obj);
## Managing logging priority

Using logging levels, we can manage output on several levels. Contexts can be named using dot '.' notation, where the names before dots are intepreted as groups or packages.

For example for 'a.b' and a.c we can define a general log level for a and have a different log level for only 'a.c'.

visual result of angular-logger

The following logging functions (left side) are available:

logging function mapped to: with logLevel
logger.trace console.debug TRACE
logger.debug console.debug DEBUG
logger.log* console.log INFO INFO
logger.warn console.warn WARN
logger.error console.error ERROR
* maintained for backwards compatibility with console.log

The level's order are as follows:

  1. TRACE: displays all levels, is the finest output and only recommended during debugging
  2. DEBUG: display all but the finest logs, only recommended during develop stages
  3. INFO :  Show info, warn and error messages
  4. WARN :  Show warn and error messages
  5. ERROR: Show only error messages.
  6. OFF  : Disable all logging, recommended for silencing noisy logging during debugging. *will* surpress errors logging.


consoleLogger.prefixPattern = '%s::[%s]> ';
consoleLogger.logLevels = {
	'a.b.c': consoleLogger.LEVEL.TRACE, // trace + debug + info + warn + error
	'a.b.d': consoleLogger.LEVEL.ERROR, // error
	'a.b': consoleLogger.LEVEL.DEBUG, // debug + info + warn + error
	'a': consoleLogger.LEVEL.WARN, // warn + error
	'*': consoleLogger.LEVEL.INFO // info + warn + error
// globally only INFO and more important are logged
// for group 'a' default is WARN and ERROR
// a.b.c and a.b.d override logging everything-with-TRACE and least-with-ERROR respectively

// modify log levels later
consoleLogger.logLevels['a.b.c'] = $log.LEVEL.ERROR;
consoleLogger.logLevels['*'] = $log.LEVEL.OFF;


Enhance console logging for better logging, including patterns and level management




No packages published
You can’t perform that action at this time.