I am using winston to log in a Sails app. This is my configuration:
var customLogger = new winston.Logger({
transports: [
new(winston.transports.File)({
level: 'debug',
filename: 'app.log',
colorize: false,
showLevel: false,
prettyPrint: false,
exitOnError: false,
json: true,
zippedArchive: true,
maxsize: 1000000000,
maxFiles: 30,
tailable: true
}),
new(winston.transports.Console)({
level: 'info',
exitOnError: false,
colorize: false,
showLevel: false
})
],
});
But in the output file there are odd characters.
{"level":"info","message":"\u001b[32minfo: \u001b[39m","timestamp":"2016-05-12T17:58:03.281Z"}
I did face a similar issue and resolved it by removing winston.format.colorize() statement from the below code to get rid of the odd or escape codes as part of the log file.
const logFormat = winston.format.combine (
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
winston.format.colorize(),
winston.format.align(),
winston.format.printf(
info => `${info.timestamp} ${info.level.toUpperCase()}: ${info.message}`
));
While below shared is the complete working code without any issues associated with odd or escape codes.
Default configurations captured as part of /zipCode/config/default.json file
{
"port": 3000,
"logConfig": {
"logFolder": ".//logs//",
"logFile": "zip-code-%DATE%.log",
"logLevel": "info"
}
}
Incorporating the create logger implementation as part of /zipCode/logger/log.js file
/**
* @author: dinesh.lomte
*/
const winston = require('winston');
const dailyRotateFile = require('winston-daily-rotate-file');
const config = require('config');
const logFormat = winston.format.combine (
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
winston.format.align(),
winston.format.printf(
info => `${info.timestamp} ${info.level.toUpperCase()}: ${info.message}`
)
);
const transport = new dailyRotateFile({
filename: config.get('logConfig.logFolder') + config.get('logConfig.logFile'),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d',
level: config.get('logConfig.logLevel')
});
transport.on('rotate', function(oldFile, newFile) {
// Call function like upload to s3 or on cloud
});
const logger = winston.createLogger({
format: logFormat,
transports: [
transport,
new winston.transports.Console({
level: config.get('logConfig.logLevel')
})
]
});
module.exports = logger;
Below /zipCode/api/controller.js file showing the logger usage
/**
* @author: dinesh.lomte
*/
'use strict';
var distance = require('../service/distance');
var logger = require('../logger/log');
var controllers = {
get_distance: function(req, res) {
logger.info('Processing get_distance request...');
distance.find(req, res, function(err, dist) {
if (err) {
logger.error('Failed to process distance request, due to: ' + err);
res.send(err);
}
logger.debug('Processed get_distance request...');
res.json(dist);
});
}
};
module.exports = controllers;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With