Winston: how to rotate logs - node.js

How can I rotate logs when using Winston to handle logging for node.js. That is, how can I create a new file for each day the app runs?
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: '2012-07-09.log' })
]
});
logger.log('info', 'Test Log Message', { anything: 'This is metadata' });

winston author and maintainer here.
Logging to a new file everyday is currently an open feature request: https://github.com/flatiron/winston/issues/10. Would love to see someone implement it.
That said, there are other options:
The File transport accepts a maxsize option which will rotate the logfile when it exceeds a certain size in bytes.
There is also an open pull-request with a new transport I haven't had a chance to really dig into "fileRotate", which it seems does this kind of date-based rotation: https://github.com/flatiron/winston/pull/120/files

The feature is present and we are using it in production, winston.transports.DailyRotateFile:
var timeFormatFn = function() {
'use strict';
return moment().format(cfg.timeFormat);
};
var logger = new(winston.Logger)({
exitOnError: false,
transports: [
new(winston.transports.DailyRotateFile)({
filename: cfg.appLogName,
dirname: __dirname + '/../' + cfg.logsDirectory,
datePattern: cfg.rollingDatePattern,
timestamp: timeFormatFn
}),
new(winston.transports.Console)({
colorize: true,
timestamp: timeFormatFn
})
]
});

You can use the following code to rotate the log files daily:
var winston = require('winston');
require('winston-daily-rotate-file');
var transport = new (winston.transports.DailyRotateFile)({
filename: './log',
datePattern: 'yyyy-MM-dd.',
prepend: true,
level: 'info'
});
var logger = new (winston.createLogger)({
transports: [
transport
]
});
logger.info('Hello World!');

According to the author of winston-filerotatedate it is a:
File transport for winston that allows the log files to be rotated depending on size and time.
The File transport accepts a filename via the 'filename' option and uses that file as the primary logging target.
Should the file grow past 'maxsize' bytes then the current log file is renamed and a new primary log tile is created.
The name of the renamed log file is formated as such 'basenameYYYYMMDD[a-z].bak'.
Available options are:
level: Level of messages that this transport should log.
silent: Boolean flag indicating whether to suppress output.
timestamp: Boolean flag indicating if we should prepend output with timestamps (default true). If function is specified, its return value will be used instead of timestamps.
filename: The filename of the logfile to write output to.
dirname: The folder the logfile will be created in.
maxsize: Max size in bytes of the logfile, if the size is exceeded then a new file is created.
json: If true, messages will be logged as JSON (default true).

Other people have already given good answers for this problem. But for those people looking for an example of how to do this in more modern JavaScript syntax(ES6 and beyond) using winston-daily-rotate-file, the following code should help:
const { createLogger, transports } = require('winston');
require('winston-daily-rotate-file');
const logger = createLogger({
level: 'info',
transports: [
new transports.DailyRotateFile({
filename: 'info-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
level: 'info'
})
]
});
logger.log('info', 'Test Log Message', { anything: 'This is metadata' });

As of Dec 18, 2012, this feature is now available in Winston (see https://github.com/flatiron/winston/pull/205)

Related

How to do logrotate logs into separate files daily in Node.js using Winston library

I have a nodejs app. I need to log each event in this program and I use winston library for this. I am trying to create a log file for each day.
All logs should be separated day by day. Those file's names should be the date of the current day.
For example today I open my program and do a get request. This action should be saved as a file on my computer ( eg on my desktop) and the file's name should be 17/10/2019.log. Any suggestion?
EDIT: SOLVED!!
require('winston-daily-rotate-file');
const logDir= 'C://Users/Desktop/LogFiles';
var options = {
file: {
level:'info',
filename: path.resolve(`${logDir}/${new Date().getFullYear().toString()} - ${new Date().getMonth()+1}/%DATE%.log`),
datePattern: 'YYYY-MM-DD',
timestamp: new Date()
};
let logger = winston.createLogger({
level:'info',
format: winston.format.combine(
winston.format.printf(info => { return `${info.timestamp} || ${info.level} || Message: ${info.message}`; })
),
transports: [
new winston.transports.DailyRotateFile(options.file)
],
exitOnError: false,
});
you could use a Transport for Winston, checkout https://github.com/winstonjs/winston-daily-rotate-file
it can do all what you're asking for and more.

How to use winston in NodeJs?

I am trying to create a sample nodeJs program to use and understand winston library for nodeJs,
I had a sample code from one of the existing projects and I am trying to create a sample code sample,
The code contains winston configuration and a simple test function to print logs using different log levels.
I'm using node version 4.6.2, I know it's pretty old but, the existing application is using the same, therefore I wanted to understand the implementation on the same version, The example consists of the following code and a log folder, in which I wish to print the log.
Following is a sample code which I am trying to run -
var winston = require('winston');
var envData = require('dotenv').config({path: 'server.env'})
require('winston-daily-rotate-file');
var levelLog = 'debug';
var winstonTransports = [];
const logDTFormat = () => (new Date().toFormat('DD MMM YYYY HH24:MI:SS'));
// Winston Rotate File Logs
var transportDailyRotate = new (winston.transports.DailyRotateFile)({
filename: './logs/hrfid_app_',
datePattern: 'yyyy-MM-dd.log',
prepend: false,
level: levelLog,
timestamp: function() {
var dateTime = new Date(Date.now());
return dateTime.toFormat('DD/MM/YYYY HH24:MI:SS');
}
});
winstonTransports.push(transportDailyRotate);
// Winston Console Log
var trasportConsole = new (winston.transports.Console)({
timestamp: logDTFormat,
colorize: true,
level: levelLog
});
winstonTransports.push(trasportConsole);
// Winston Config
winston.configure({
level: levelLog,
transports: winstonTransports
});
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
// Validate Server Configuration
if (envData.error) {
winston.error('[APP-CONFIG]:', JSON.stringify(envData.error))
winston.error('[APP-CONFIG]:', 'Error on Server Configuration')
return
}
var test = function(){
winston.error("ERROR log");
winston.info("INFO log");
winston.debug("DEBUG log");
}
test();
I am getting the following error,Can someone please help fix my sample code?
Upgrade to node 6 or 8.
Latest winston require at least node 6.4
https://github.com/winstonjs/winston/blob/c42ab7fdc51b88db180a7dd90c52ce04ddd4e054/package.json#L69
Or use an older version of winston by deleting the existing one and execute npm install winston#2.4.4

Logging only the message using winston

I am using winston logger in my node app and i want to log my custom message to the log file.
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info',
filename: '/tmp/test.log',
handleExceptions: true,
json: true,
maxsize: 20971520 //20MB
})
],
exitOnError: false
});
I only need to log message in the log, i dont need to log level and timestamp in the log file. Current logged sample is as below.
{"level":"info","message":"sample entry to log","timestamp":"2015-12-11T09:43:50.507Z"}
My intention is to get entry in the log file as below
sample entry to log
How to achieve this?
Winston 3:
import winston from 'winston';
const logger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.Console({
format: winston.format.printf(log => log.message),
})
]
});
logger.info('eat my shorts');
On Winston 2.x
If you want to print only the message, you can achieve this by setting:
json: false,
timestamp: false,
showLevel: false
Full Logger example:
let logger = new Logger({
transports: [
new (transports.File)({
name: 'info-file',
filename: 'filelog-info.log',
json: false,
timestamp: false,
showLevel: false
})
],
exitOnError: false
});
logger.log('info', 'eat my shorts');
gives in filelog-info.log: eat my shorts
FYI: filters and rewriters didn't help here
If anyone, as did I, finds this question in hopes of making express-winston log only message (to the console in my case)
Here is what I did:
const winston = require('winston');
const expressWinston = require('express-winston');
const { MESSAGE } = require('triple-beam');
expressWinston.logger({
transports: [
new winston.transports.Console({
format: simpleformatter()
})
]
});
const simpleformatter = winston.format(info => {
info[MESSAGE] = info.message;
return info;
});
Yep got the one i was looking for
https://github.com/winstonjs/winston#filters-and-rewriters
https://github.com/winstonjs/winston#custom-log-format

Multiple file transports in using Winston

I am attempting to use multiple transports in Winston. This is working...sorta. I have a transport set up for the app's audit log, with a custom level of 'audit', and a transport for 'info'.
var winston_mongo_options = {
level: 'audit', // level that should be logged
safe: true, //makes sure writes happen before firing log event
db: config.db.db, // db in which to write logs
host: config.db.host,
port: config.db.port,
collection: config.db.audit_collection // collection we want logging to occur
};
if (config.db.user) {
winston_mongo_options.username = config.db.user;
winston_mongo_options.password = config.db.pass;
}
var custom_levels = winston.config.syslog.levels;
custom_levels.audit = 8;
var logger = new (winston.Logger)({
levels: custom_levels,
transports : [
new (winston.transports.MongoDB)(winston_mongo_options),
new (winston.transports.File)({
level: 'info',
silent: false,
colorize: true,
timestamp: true,
filename: config.logs.debug,
maxsize: 500000,
maxFiles: 5,
json: true
})
],
exceptionHandlers: [
new (winston.transports.File)({
silent: false,
colorize: false,
timestamp: true,
filename: config.logs.exception,
maxsize: 500000,
maxFiles: 5,
json: true
})
]
});
module.exports.logger = logger;
Obviously, I'm requiring this file where/when I want to do any logging. The problem comes in when wanting to send certain information separately to the logs.
logger.audit('Server Started - to DB');
logger.info('Server Started - to info');
These two lines should write to separate logs. The first line gets properly written to the database AND to the info log file. What am I doing wrong?
SOLVED: The problem was how I am defining the levels. I didn't realize that the way Winston logging works, is a log will receive everything >= the level defined. So my 'info' transport being a level 0 was receiving messages sent to the 'audit' transport, which was a level 8. I set 'audit' to level 0, and it stopped showing up in the 'info' log. I then found a better solution by creation a whole new logger just for the audit database log.

Multiple log files with Winston?

We'd like to use Winston for our logging in Node.js. But, we can't figure out how to have two log files: one for just errors, and one for everything else.
Doing this the naive way doesn't work, however: adding multiple winston.transports.File transports gives an error.
Others have run into this problem, with vague hints of a solution, but no real answer.
Any ideas?
Unfortunately, the patch that pesho mentioned seems to be still not included in the official version (see stephenbeeson's comment in the pull request #149).
So, I used a workaround instead. As winston compares the name attributes, you can fool it by defining the name yourself:
winston = require 'winston'
logger = new winston.Logger
transports: [
new winston.transports.File
name: 'file#debug'
level: 'debug'
filename: '/tmp/debug.log'
new winston.transports.File
name: 'file#error'
level: 'error'
filename: '/tmp/error.log'
]
logger.error 'error' # both logs
logger.debug 'debug' # on debug log
Maybe not elegant, but at least it works.
In the meantime, you can implement a rudimentary wrapper using the same interface like so
var winston = require('winston');
var configs = require('./env.js');
var debug = new winston.Logger({
levels: {
debug: 0
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'debug'}),
new (winston.transports.Console)({level: 'debug'})
]
});
var info = new winston.Logger({
levels: {
info: 1
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'info'}),
new (winston.transports.Console)({level: 'info'})
]
});
var warn = new winston.Logger({
levels: {
warn: 2
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'warn'}),
new (winston.transports.Console)({level: 'warn'})
]
});
var error = new winston.Logger({
levels: {
error: 3
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'error'}),
new (winston.transports.Console)({level: 'error'})
]
});
var exports = {
debug: function(msg){
debug.debug(msg);
},
info: function(msg){
info.info(msg);
},
warn: function(msg){
warn.warn(msg);
},
error: function(msg){
error.error(msg);
},
log: function(level,msg){
var lvl = exports[level];
lvl(msg);
}
};
module.exports = exports;
This will cover the basic winston API. could be extended for metadata and so on...
I just sent a pull request that allows using multiple File transports in one logger.
https://github.com/flatiron/winston/pull/149
It is already merged into flatiron/winston.
You can also use my forked repo:
https://github.com/pdobrev/winston
You just need to give the transport a custom name property so you don't have a collision:
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ name: 'text', filename: logFile, json: false }),
new (winston.transports.File)({ name: 'json', filename: logFileJson })
]
});
You can read more about multiple transports in the docs: https://github.com/winstonjs/winston#multiple-transports-of-the-same-type
This feature is now officially supported in Winston and is addressed in the README here
Code example:
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}

Resources