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,
}),
Related
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.
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.
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
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
});
I have a project nodejs and use log4js to write log.
I want create new file log when start new date.
Example:
daily.2017_07_31.log
daily.2017_08_01.log
daily.2017_08_02.log
daily.2017_08_03.log
In java, I know config log4j but in nodejs with log4js I don't know.
Thank every body for your help :)
winston is recommended for nodejs. Its pretty easy to use.
Create a logger.js file and have this configuration '
require('winston-daily-rotate-file');
var winston = require('winston');
winston.loggers.add('logger', {
transports: [
new (winston.transports.Console)(
{
level: config.debugLogLevel,
colorize: true
}),
//new files will be generated each day, the date patter indicates the frequency of creating a file.
new winston.transports.DailyRotateFile({
name: 'debug-log',
filename: '<log file name>',
level: '<level>',
prepend: true,
datePattern: '<pattern>',
maxFiles: <max file>
}
),
new (winston.transports.DailyRotateFile)({
name: 'error-log',
filename: '<log file name>',
level: '<level>',
prepend: true,
datePattern: '<pattern>',
maxFiles: <max file>
})
]
});
var logger = winston.loggers.get('logger');
Object.defineProperty(exports, "LOG", {value: logger});
now you can use it anywhere like
var log = require('./logger.js').LOG
log.error('hello');
See: https://github.com/log4js-node/log4js-node/blob/master/docs/dateFile.md
log4js.configure({
appenders: {
everything: { type: 'dateFile', filename: 'all-the-logs.log' }
},
categories: {
default: { appenders: [ 'everything' ], level: 'debug' }
}
});
This example will result in files being rolled every day. The initial
file will be all-the-logs.log, with the daily backups being
all-the-logs.log.2017-04-30, etc.
Not found for daily rolling, but for size the configuration of log4js allows file rolling. Pay attention to maxLogSize, backups and compress properties. Example from its docs:
log4js.configure({
appenders: {
everything: {
type: 'file',
filename: 'all-the-logs.log',
maxLogSize: 10485760,
backups: 3,
compress: true
}
},
categories: {
default: { appenders: [ 'everything' ], level: 'debug'}
}
});
See https://github.com/log4js-node/log4js-node/blob/master/docs/file.md