I have my winston setup as follows
// Config import.
var config = require('../config');
// Library imports.
var winston = require('winston');
var logger = new(winston.Logger)({
transports: [
new(winston.transports.Console)({
level: 'debug',
colorize: true,
timestamp: true,
json: true,
showLevel: true
}),
new(require('winston-daily-rotate-file'))({
prepend: true,
level: 'debug',
colorize: true,
timestamp: true,
filename: 'log.txt',
maxSize: config.log.maxSize,
json: true,
prettyPrint: true
})
]
});
It logs to the console fine - as expected.
It logs to file fine - as expected.
However, the filename has the date tacked onto the end of the filename, not the beginning, like the prepend option would suggest.
Is anyone able to offer any insight into this?
Worked this one out - I'm a bit silly.
The version showing in master on the project's github page is not the same as the version that is dished out by npm install - the version that is served via npm doesn't currently have support for the prepend option.
Hopefully a release is coming soon :)
Related
I am using winston to write logs to a file on a host that I have mounted to a docker container. The mounted volume inside the docker container is supposed to tail the file that we mounted, but when winston writes logs to the file location I either have to close the app or manually go into the log file and save it in order for the file contents to be synced to the mounted volume in docker which eventually tails the file.
Is there a way for winston to write the logs and save the file so the contents are synced immediately? Below is my winston config
const winston = require('winston');
const path = require('path');
let logsPath = path.join(__dirname, '../logs/');
const options = {
file: {
timestamp: true,
level: 'info',
filename: logsPath + `app.logs`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: true,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
let logger;
logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console),
],
exitOnError: false, // do not exit on handled exceptions
});
module.exports = logger;
i am using winston to add logs in my nodejs project which is basically a kakfka based service which runs 24 x 7, below code only generate log on separate file(the file dated when the service restarted) but it only changes file when the i restart the server after the change date, is their anything which automatically changes the log file when the day changes.
const {createLogger, format, transports} = require("winston");
const {combine, timestamp, label, prettyPrint} = format;
const logger = createLogger({
level: "info",
format: format.combine(
format.splat(),
format.simple(),
format.json(),
timestamp(),
prettyPrint()
),
transports: [
new transports.File({
filename: `${appRoot}/Log/error/Log_${new Date().getUTCDate()}-${new Date().getMonth()}-${new Date().getFullYear()}.log`,
level: "error",
handleExceptions: true,
json: true,
stack: true,
}),
],
exitOnError: false,
});
module.exports = logger;
As far as I understood has Winston a hierarchy in its logging levels. You can set the level with winston.level = 'error' and all levels below should not be shown. Unfortunately I still get the logs of info and debug shown in my console.
Question 1):
How to really set winston to show me only the log level
Question 2)
Why is Debug anyways shown in the console, I configured it to show up in a log file (what it additionally does)
winston = require('winston')
winston.emitErrs = true
logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info'
filename: 'logs/log.log'
handleExceptions: true
json: true
maxsize: 5242880 #5MB
maxFiles: 5
colorize: false
timestamp: true
}),
new winston.transports.Console({
level: 'debug'
handleExceptions: true
json: false
colorize: true
})
],
exitOnError: false
})
winston.level = 'error'
module.exports = logger
module.exports.stream = {
write: (message, encoding) ->
logger.info(message)
}
The code is basically from this tut:
http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
Now I got the concept. It logs everything up to the log level in the defined transport. So I guess it is not possible to stream debug just to console and info to the log without manipulating the order.
This does not log debug in the console:
new winston.transports.Console({
level: 'info'
handleExceptions: true
json: false
colorize: true
})
My first assumption was that I can specify a transport for each log level and then decide how much logging I want to have with:
winston.setLevels(winston.config.syslog.levels)
e.g.
error to mongoDb
info to log.log file
debug to console
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.
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)