Node-Logging
This is a logging library for use with node.js. It decouples log reporting from publishing and is based on the Java API.
Installation
Fork the latest source from github.
or
npm install logg
Usage
Call getLogger(name)
with the name of your class, namespace, or made up identifier.
Loggers expose fine()
, info()
, warn()
, error()
and log(level, args)
. These logging methods take variable arguments and will call sys.inspect
on any objects that are passed. There is special handling for Error objects.
var logging = require('logg');
var logger = logging.getLogger('my.class);
logger.setLogLevel(logging.Level.WARN);
logger.info('This will not show up');
logger.warn('But warnings will', new Error('aargg'));
Loggers are arranged in a hierarchy based on their names, separated by dots. Log reporting levels are inherited based on the hierarchy, INFO being the default level. For example, the following will silence everything but errors within the subproject
namespace:
var a = logging.getLogger('project.subproject.foo');
var b = logging.getLogger('project.subproject.bar');
var c = logging.getLogger('project.subproject.baz');
var d = logging.getLogger('project.subproject.bam');
logging.getLogger('project.subproject').setLogLevel(logging.Level.SEVERE);
Every logger can have watchers associated with it, which will get called with a log record. Usually you'd just want to attach to root logger.
logging.registerWatcher(function(logRecord) {
// Don't use sync API in real life...
fs.writeFileSync('logs.log', JSON.stringify(logRecord) + '\n');
});
A default watcher is automatically registered which outputs to the console.