Integrates winston 3 logging system with your Sails 1.x application.
Note: This library requires sails >= 1.x
npm install sails-hook-winstonwinston is one of the most powerful logging libraries for NodeJS. You can unlock the features of winston in your Sails application when you use this hook.
All available winston transports can be configured through the transports option.
The console transport is included by default (by default based on the Sails log options).
This library extend config/log and use the winston API for setup the Transport. This is an example of configuration for an application named 'myApp':
const env = process.env.NODE_ENV;
const { createLogger, format, transports } = require('winston');
const { colorize, combine, timestamp, printf, errors } = format;
const DailyRotateFile = require('winston-daily-rotate-file');
const util = require('util');
function transform(info, opts) {
const args = info[Symbol.for('splat')];
if (args) { info.message = util.format(info.message, ...args); }
return info;
}
function utilFormatter() { return {transform}; }
const logFormat = combine(
colorize(),
timestamp(),
utilFormatter(),
errors({ stack: true }),
printf( info => `${info.timestamp} ${info.level}: ${info.message}`,),
);
const transportInfo = new transports.DailyRotateFile({
name: 'silly-file',
dirname:'./.tmp/logs',
frequency:'24h',
datePattern: 'YYYY-MM-DD-HH',
maxSize: '20m',
maxFiles: '14d',
filename: 'MyApp-%DATE%.log',
level: 'silly',
json: true,
colorize: false
});
const transportError = new transports.DailyRotateFile({
name: 'error-file',
dirname:'./.tmp/logs',
frequency:'24h',
datePattern: 'YYYY-MM-DD-HH',
maxSize: '20m',
maxFiles: '14d',
filename: 'MyApp-%DATE%.error',
level: 'error',
json: true,
colorize: false
});
transportInfo.on('rotate', function(oldFilename, newFilename) {
// do something fun
console.log( 'Log info rotate :', oldFilename, newFilename);
});
transportError.on('rotate', function(oldFilename, newFilename) {
// do something fun
console.log( 'Log error rotate :', oldFilename, newFilename);
});
const logger = createLogger({
format: logFormat,
transports: [
new transports.Console({
level: 'silly',
colorize: true
}),
transportError,
transportInfo
]
});
module.exports.log = {
/***************************************************************************
* *
* Valid `level` configs: i.e. the minimum log level to capture with *
* sails.log.*() *
* *
* The order of precedence for log levels from lowest to highest is: *
* silly, verbose, info, debug, warn, error *
* *
* You may also set the level to "silent" to suppress all logs. *
* *
***************************************************************************/
level: env === 'development' ? 'silly' : 'info',
colorize: true,
custom: logger,
inspect:false
};Note how the options are different for each transport. For example, you can use info level in console but store silly level in DailyFileRotate.
MIT