2. winston - log
Winston is designed to be a simple and
universal logging library with support for
multiple transports.
3. Using the Default Logger
var winston = require('winston');
winston.log('info', 'Hello distributed log files!');
winston.info('Hello again distributed logs');
output
info: Hello distributed log files!
info: Hello again distributed logs
4. Add, Remove Transport
By default, only the Console transport is set on the default logger. You
can add or remove transports via the add() and remove() methods:
var winston = require('winston');
winston.add(winston.transports.File, { filename: 'somefile.log' });
winston.remove(winston.transports.Console);
5. Instantiating your own Logger
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
6. Logging with Metadata
In addition to logging string messages, winston will also
optionally log additional JSON metadata objects.
winston.log('info', 'Test Log Message', { anything: 'Hello' });
output:
info: Test Log Message anything=Hello
7. Logging with Metadata
In addition to logging string messages, winston will also
optionally log additional JSON metadata objects.
winston.log('info', 'Test Log Message', { anything: 'Hello' });
output:
{"anything":"Hello","level":"info","message":"Test Log
Message","timestamp":"2015-07-24T00:09:55.149Z"}
8. Multiple transports of the same type
var logger = new (winston.Logger)({
transports: [
new (winston.transports.File)({
name: 'info-file', filename: 'filelog-info.log', level: 'info'
}),
new (winston.transports.File)({
name: 'error-file', filename: 'filelog-error.log', level: 'error'
})
]
});
9. Profiling
In addition to logging messages and metadata, winston also has
a simple profiling mechanism implemented for any logger:
winston.profile('test');
setTimeout(function () {
winston.profile('test');
}, 1000);
output:
info: test durationMs=1002
10. String interpolation
logger.log('info', 'test message %s', 'my string');
// info: test message my string
logger.log('info', 'test message %d', 123);
// info: test message 123
logger.log('info', 'test message %j', {number: 123}, {});
// info: test message {"number":123}
11. Handling Uncaught Exceptions with
winston
With winston, it is possible to catch and log uncaughtException
events from your process.
winston.handleExceptions(new winston.transports.File({ filename: 'exceptions.log' }));
winston.add(winston.transports.File, {
filename: 'all-logs.log',
handleExceptions: false
});
12. Querying Logs
var options = {
from: new Date - 24 * 60 * 60 * 1000,
until: new Date, limit: 10, start: 0, order: 'asc', fields: ['message']
};
logger.query(options, function (err, result) {
if (err) {
throw err;
}
console.log(result);
});
output:
{
file: [ { message: 'Fail!' } ]
}
13. Events and Callbacks in Winston
logger.on('logging', function (transport, level, msg, meta) {
…..
});
OR
logger.info('Hello!', function (err, level, msg, meta) {
…..
});
14. Working with multiple Loggers in winston
winston.loggers.add('logger1', { …. });
winston.loggers.add('logger2', { …. });
var logger1 = winston.loggers.get('logger1');
logger1.info('Hi!!');
15. Extending another object with Logging
Often in a given code base with lots of Loggers it is useful to add
logging methods to a different object so that these methods can
be called with less syntax.
var myObject = {};
logger.extend(myObject);
myObject.info("127.0.0.1 - there's no place like home");
16. Filters and Rewriters
Filters allow modifying the contents of log messages.
Rewriters allow modifying the contents of log meta .
e.g. to mask data that should not appear in logs.