Winston Logger (v2.4) writing to file even after rotation - node.js

I have a transport for console out as well as one for file out.
This works fine and creates a new log file, while renaming the old one
eg. logs/appLogs.txt and logs/appLogs1.txt
The issue i face is that it continues to write to the first file even though its above the maxsize. Winston is logging to both files, but splitting the logs between the two.
eg: appLogs1.txt
2019-02-21T18:53:04.581Z - debug: ... logs
and appLogs.txt
2019-02-21T18:53:04.538Z - debug: ... logs
I can ssh into the container and with ls -l see both files grow.
below is my winston config:
new winston.Logger({
transports: [
new winston.transports.Console({
colorize: true,
timestamp: true,
handleExceptions: true,
stderrLevels: ['error'],
humanReadableUnhandledException: true,
level: level,
label: category
}),
new winston.transports.File({
filename: 'logs/appLogs.txt',
maxsize: 5000000,
maxFiles: 20,
tailable : true,
timestamp: true,
handleExceptions: true,
humanReadableUnhandledException: true,
level: level,
label: category,
json: false
})
]
})
appLogs1.txt is now 9948153 bytes, and appLogs.txt is 882922

Related

Trouble sending transport to remote server using Winston

I have been trying to implement centralized logging across several clustered servers. I am impressed with Winston, but have been running into a path issue. Need to know the syntax for accessing remote server (code snippet included below)
I am able to log to console, a local file and a rotating local file(s)...but when I try to send a transport over to a remote server, it gets lost in the ether.
const options = {
file: {`enter code here`
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880,
maxFiles: 5,
colorize: true,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
rotator:{
frequency: '1m',
filename: `${appRoot}/logs/application-%DATE%.log`,
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: '20k',
maxFiles: '1d',
level: 'info'
},
remoteServer:{
level:'info',
filename: '\\\\SERVERNAME.corp.COMPANY.com\\Filestore\\PM2_logs\\SM-`API-out.log'`
},
};
const logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console),
new winston.transports.DailyRotateFile(options.rotator),
new winston.transports.File(options.remoteServer),
],
exitOnError: false,
});
I expected a copy of the log output to go to the file at the end of the path, but no such luck as of yet. I have tried a variety of forward/backward slash syntax.

PM2 + Winston Log Management

I want to have log files on a daily basis. I dont want both of them to create separate log files. I just want a single log file on a daily
basis.
example: application-YYYY-MM-DD:HH:MM.log (Log Rotation)
How can I achieve this ?
Also, PM2 Log Rotate doesn't support timestamps for logging. So, How can I merge both features?
I Attached ecosystem.config.js as well as logger.js
ecosystem.config.js
module.exports = {
apps: [{
name: 'API',
script: 'app.js',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
output: `./logs/starter-logs.log`,
error: `./logs/starter-errors.log`,
log: './logs/combined.outerr.log',
log_date_format: 'DD-MM-YYYY HH:mm:ss:SS Z',
append_env_to_name: true,
// For merging cluster mode logs
merge_logs: true,
env: {
NODE_ENV: 'development',
PORT: 3002,
MONGODB_URI: 'mongodb://localhost:27017/test'
},
env_production: {
NODE_ENV: 'production',
PORT: 3004,
}
}]
logger.js
const logger = createLogger({
level: 'info',
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.errors({ stack: true }),
format.splat(),
format.json()
),
transports: [
new transports.File({ filename: `${logFileName}-error.log`, level: 'error' }),
new transports.File({ filename: `${logFileName}-combined.log` }),
new transports.Console(),
]
});
Need a Single Log file, Instead of PM2 creating logs as well as Winston creating log files.
Also, I need File Transport as well as Console Transport. Please check the above code.

TypeError: winston.Logger is not a constructor with winston and morgan

I tried with Winston for logger. I used in one project their It's working well when I copy paste the code from their to the current existing project than I face an issue like TypeError: winston.Logger is not a constructor
let logger = new (winston.Logger)({
^
TypeError: winston.Logger is not a constructor
Please guide me, Why this error and what should I have to do for solving this issue.
"morgan": "^1.9.0", "winston": "^3.0.0"
Following is my code in logger.js file.
var appRoot = require('app-root-path');
var winston = require('winston');
var options = {
file: {
level: 'info',
name: 'file.info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 100,
colorize: true,
},
errorFile: {
level: 'error',
name: 'file.error',
filename: `${appRoot}/logs/error.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 100,
colorize: true,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// your centralized logger object
let logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(options.console),
new (winston.transports.File)(options.errorFile),
new (winston.transports.File)(options.file)
],
exitOnError: false, // do not exit on handled exceptions
});
As you mention, you are using 3.0.0, you can not not use winston.Logger, you may refer library code( https://github.com/winstonjs/winston/blob/master/lib/winston.js#L178 )
You need to make small update in your code, use winston.createLogger instead of new (winston.Logger)
// your centralized logger object
let logger = winston.createLogger({
transports: [
new (winston.transports.Console)(options.console),
new (winston.transports.File)(options.errorFile),
new (winston.transports.File)(options.file)
],
exitOnError: false, // do not exit on handled exceptions
});

Set how many files winston daily logrotate should keep

I'm implementing logging for a node app and need some advice: I've read some stuff about winston.transports.DailyRotateFile but I still don't get, where can I configure, how much daily log files should it keep. Where can I set it? Also, where do I configure it to compress old logs?
You should use the following two options:
zippedArchive
maxFiles
Example:
new (winston.transports.DailyRotateFile)({
level: process.env.LOG_LEVEL || 'error',
name: 'log.all',
colorize: false,
timestamp: true,
json: false,
filename: `logs/famitsu-server`,
datePattern: '.yyyy-MM-dd.log',
zippedArchive: true,
maxFiles: 10,
}),

logging with Express Node.js using winston

I have my logging configured like this in my express/nodejs web application. The timestamp in the log messages are in GMT. Is there a way to get the local time instead?
var winston = require('winston');
winston.emitErrs = true;
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'debug',
filename: './logs/all-logs.log',
handleExceptions: true,
json: false,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false,
timestamp:true
}),
new winston.transports.Console({
timestamp :true,
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
module.exports = logger;
module.exports.stream = {
write: function(message, encoding){
logger.info(message);
}
};
This is a sample log out put I get
2014-11-18T18:30:33.570Z - debug: Authenticated
Have you tried with :
function myTimestamp() {
return new Date().toString();
};
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'debug',
filename: './logs/all-logs.log',
handleExceptions: true,
json: false,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: false,
timestamp: myTimestamp
}),
new winston.transports.Console({
timestamp :true,
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});

Resources