Winston logs to all files. (or: Restrict transport to only 1 level)
See original GitHub issueThis is my winston config:
var winston = require('winston');
var fs = require('fs');
var _ = require('lodash');
winston.emitErrs = true;
var logDir = 'logs';
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
//add custom logging levels
var levels = _.clone(winston.config.syslog.levels);
var colors = _.clone(winston.config.syslog.colors);
levels.request = _.max(levels) + 1;
colors.request = 'blue';
var logger = new winston.Logger({
levels: levels,
colors: colors,
exceptionHandlers: [
new winston.transports.File({filename: 'logs/exceptions.log'})
],
transports: [
new winston.transports.File({
name: 'info-file',
level: 'info',
filename: 'logs/all-logs.log',
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.File({
name: 'error-file',
level: 'error',
filename: 'logs/error-logs.log',
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.File({
name: 'request-file',
level: 'request',
filename: 'logs/requests.log',
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false
})
],
exitOnError: false
});
module.exports = logger;
I’m trying to log, errors, http-requests, and exceptions to their own files. However, I see, for example, in error-logs.log (http)requests logs.
Can anybody explain to me what I’m doing wrong?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:4
- Comments:25 (2 by maintainers)
Top Results From Across the Web
Winston Logging - separate levels to separate Transports
Winston allows you to define a level property on each transport which specifies the maximum level of messages that a transport should log....
Read more >Winston logs to all files. (or: Restrict transport to only 1 level)
This is my winston config: var winston = require('winston'); var fs = require('fs'); var _ = require('lodash'); winston.emitErrs = true; var logDir =...
Read more >A Complete Guide to Winston Logging in Node.js - Better Stack
Learn how to start logging with Winston in Node.js and go from basics to best practices ... (not just File ), since they...
Read more >Complete Winston Logger Guide With Hands-on Examples
With Winston, you can change your code in a single place and customize the format of your logs (e.g., logging in JSON format,...
Read more >Node.js Logging with Winston - Reflectoring
Transports is a Winston feature that makes use of the Node.js networking, stream, and non-blocking I/O properties. Transport in Winston refers ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m using the 3.x winston, after trials and errors I find out that we can filter log by level to log them to separate files. Here is the code I used:
@NukaPunk Isn’t this the expected behaviour of a logger?
Usually a logger instance will log to all transports that match the minimum level of the message. Ex: debug -> info -> warning -> error. A transport that handles info will not log debug messages but will log info, warning and error.
This is usually solved by creating multiple logger instances, each handling a specific case.