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
- Description:
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:
- Description:
Creates a new logger object for the provided module
- Source:
Example
const logger = require('./log.js')('myModule');
logger.i('MyModule initialized');
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string | Name of the module |
Returns:
Logger object
- Type
- Logger
Members
(inner) d
- Description:
Log debug level messages
- Source:
Log debug level messages
Example
logger.d('Debug message: %s', 'Some debug info');
(inner) e
- Description:
Log error level messages
- Source:
Log error level messages
Example
logger.e('Error occurred: %o', errorObject);
(inner) i
- Description:
Log information level messages
- Source:
Log information level messages
Example
logger.i('Info message: User %s logged in', username);
(inner) levels
- Description:
Current levels for all modules
- Source:
Current levels for all modules
(inner) sub
- Description:
Pass sub one level up
- Source:
Pass sub one level up
(inner) w
- Description:
Log warning level messages
- Source:
Log warning level messages
Example
logger.w('Warning: %d attempts failed', attempts);
Methods
(static) getLevel(module) → {string}
- Description:
Get currently set logging level for module
- Source:
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)
- Description:
Handle messages from ipc
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
msg |
string | message received from other processes |
(static) setDefault(level)
- Description:
Sets default logging level for all modules, that do not have specific level set
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
level |
string | level of logging, possible values are: debug, info, warn, error |
(static) setLevel(module, level)
- Description:
Sets current logging level
- Source:
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) callback(nextopt) → {function}
- Description:
Logging inside callbacks
- Source:
Example
const logCallback = logger.callback((result) => {
console.log('Operation completed with result:', result);
});
someAsyncOperation(logCallback);
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
next |
function |
<optional> |
next function to call, after callback executed |
Returns:
function to pass as callback
- Type
- function
(inner) f(l, fn, fl, …fargs) → {boolean}
- Description:
Log variable level messages (for cases when logging parameters calculation are expensive enough and shouldn't be done unless the level is enabled)
- Source:
Example
logger.f('d', (log) => {
const expensiveOperation = performExpensiveCalculation();
log('Debug: Expensive operation result: %j', expensiveOperation);
}, 'i', 'Skipped expensive debug logging');
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
l |
string | log level (d, i, w, e) |
|
fn |
function | function to call with single argument - logging function |
|
fl |
string | fallback level if l is disabled |
|
fargs |
* |
<repeatable> |
fallback level arguments |
Returns:
true if f() has been called
- Type
- boolean
(inner) f(l, fn, fl, fargs) → {boolean}
- Description:
Log variable level messages (for cases when logging parameters calculation are expensive enough and shouldn't be done unless the level is enabled)
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
l |
String | log level (d, i, w, e) |
fn |
function | function to call with single argument - logging function |
fl |
String | fallback level if l is disabled |
fargs |
Array.<any> | fallback level arguments |
Returns:
true if f() has been called
- Type
- boolean
(inner) id() → {string}
- Description:
Get logger id
- Source:
Example
const loggerId = logger.id();
console.log(`Current logger ID: ${loggerId}`);
Returns:
id of this logger
- Type
- string
(inner) id() → {string}
- Description:
Get logger id
- Source:
Returns:
id of this logger
- Type
- string
(inner) log(level, prefix, enabled, outer, out, styler) → {function}
- Description:
Returns logger function for given preferences
- Source:
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}
- Description:
Looks for logging level in config for a particular module
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
name |
string | module name |
Returns:
log level
- Type
- string
(inner) logdb(opname, nextopt, nextErroropt) → {function}
- Description:
Logging database callbacks
- Source:
Example
const dbCallback = logger.logdb('insert user',
(result) => { console.log('User inserted:', result); },
(error) => { console.error('Failed to insert user:', error); }
);
database.insertUser(userData, dbCallback);
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
opname |
string | name of the performed operation |
|
next |
function |
<optional> |
next function to call, after callback executed |
nextError |
function |
<optional> |
function to pass error to |
Returns:
function to pass as callback
- Type
- function
(inner) sub(subname) → {Logger}
- Description:
Add one more level to the logging output while leaving loglevel the same
- Source:
Example
const subLogger = logger.sub('database');
subLogger.i('Connected to database');
Parameters:
| Name | Type | Description |
|---|---|---|
subname |
string | sublogger name |
Returns:
new logger
- Type
- Logger
Type Definitions
Logger
- Source:
Properties:
| Name | Type | Description |
|---|---|---|
id |
function | Get the logger id |
d |
function | Log debug level messages |
i |
function | Log information level messages |
w |
function | Log warning level messages |
e |
function | Log error level messages |
f |
function | Log variable level messages |
callback |
function | Create a callback function for logging |
logdb |
function | Create a callback function for logging database operations |
sub |
function | Create a sub-logger |
Type:
- Object
Examples
const loggerId = logger.id();
console.log(`Current logger ID: ${loggerId}`);
logger.d('Debug message: %s', 'Some debug info');
logger.i('Info message: User %s logged in', username);
logger.w('Warning: %d attempts failed', attempts);
logger.e('Error occurred: %o', errorObject);
logger.f('d', (log) => {
const expensiveOperation = performExpensiveCalculation();
log('Debug: Expensive operation result: %j', expensiveOperation);
}, 'i', 'Skipped expensive debug logging');
const logCallback = logger.callback((result) => {
console.log('Operation completed with result:', result);
});
someAsyncOperation(logCallback);
const dbCallback = logger.logdb('insert user',
(result) => { console.log('User inserted:', result); },
(error) => { console.error('Failed to insert user:', error); }
);
database.insertUser(userData, dbCallback);
const subLogger = logger.sub('database');
subLogger.i('Connected to database');