api/utils/log

Log provides a wrapper over debug or console functions with log level filtering, module filtering and ability to store log in database. Uses configuration require('../config.js').logging: { 'info': ['app', 'auth', 'static'], // log info and higher level for modules 'app*', 'auth*', 'static*' 'debug': ['api.users'], // log debug and higher (in fact everything) for modules 'api.users*' 'default': 'warn', // log warn and higher for all other modules } Note that log levels supported are ['debug', 'info', 'warn', 'error']

Usage is quite simple: var log = require('common.js').log('module[:submodule[:subsubmodule]]'); log.i('something happened: %s, %j', 'string', {obj: 'ect'}); log.e('something really bad happened: %j', new Error('Oops'));

Whenever DEBUG is in process.env, log outputs all filtered messages with debug module instead of console so you could have pretty colors in console. In other cases only log.d is logged using debug module.

To control log level at runtime, call require('common.js').log.setLevel('events', 'debug'). From now on 'events' logger will log everything.

There is also a handy method for generating standard node.js callbacks which log error. Only applicable if no actions in case of error needed: collection.find().toArray(log.callback(function(arg1, arg2){ // all good }));

  • if error didn't happen, function is called
  • if error happened, it will be logged, but function won't be called
  • if error happened, arg1 is a first argument AFTER error, it's not an error
Source:

(require("api/utils/log"))(name) → {module:api/utils/log~Logger}

Source:

Creates new logger object for provided module

Parameters:
Name Type Description
name string

name of the module

Returns:

logger object

Type
module:api/utils/log~Logger

Classes

Logger

Members

(inner) levels

Source:

Current levels for all modules

Methods

(static) getLevel(module) → {string}

Source:

Get currently set logging level for module

Parameters:
Name Type Description
module string

name of the module for logging

Returns:

level of logging, possible values are: debug, info, warn, error

Type
string

(static) ipcHandler(msg)

Source:

Handle messages from ipc

Parameters:
Name Type Description
msg string

message received from other processes

(static) setDefault(level)

Source:

Sets default logging level for all modules, that do not have specific level set

Parameters:
Name Type Description
level string

level of logging, possible values are: debug, info, warn, error

(static) setLevel(module, level)

Source:

Sets current logging level

Parameters:
Name Type Description
module string

name of the module for logging

level string

level of logging, possible values are: debug, info, warn, error

(inner) log(level, prefix, enabled, outer, out, styler) → {function}

Source:

Returns logger function for given preferences

Parameters:
Name Type Description
level string

log level

prefix string

add prefix to message

enabled boolean

whether function should log anything

outer object

this for @out

out function

output function (console or debug)

styler function

function to apply styles

Returns:

logger function

Type
function

(inner) logLevel(name) → {string}

Source:

Looks for logging level in config for a particular module

Parameters:
Name Type Description
name string

module name

Returns:

log level

Type
string