PM2 + Winston Log Management - node.js

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.

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.

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

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

How to categorize the log file based on current date?

currently I have log file where I am storing the log details. But I want to store the set of details on the every-day basis and it should generate the new file automatically.
module.exports.log = log4js.configure({
appenders: { log-info: { type: 'file', filename: 'logs/info.text'}},
categories: { default: { appenders: ['log-info'], level: 'info' } }
});
I browsed through few stuffs, but I din't got any proper required info. Any link/advice, greatly appreciated.
I have use winston which work as your requirement.
Create logs directory in your root folder and try this code
var winston = require('winston');
var path = require('path');
let __base = path.resolve(__dirname, '..');
var logger = new (winston.Logger)({
levels: {
error: 0,
warn: 1,
info: 2,
debug :4
},
transports: [
new (winston.transports.Console)({
prettyPrint: true,
colorize: true,
timestamp: true
}),
new (winston.transports.File)({
name: 'errorLogger',
filename: `${__base}/logs/log-${new Date().toLocaleDateString().replace(/\//g, '-')}.log`,
colorize: true,
/**
*
*
* #returns
*/
timestamp: function () {
return (new Date().toLocaleString())
}
})
],
colors: {
info: "green",
error: "red",
warn: "yellow",
debug: "blue"
}
});
logger.level = 'info';
logger.info('Hello');
module.exports = logger;
After running this code you will see a file is generated in logs folder with Hello content.
You can also import this file in other file and use logger.info for printing and saving the logs
You can use the alwaysIncludePattern, just set it to true, like alwaysIncludePattern: true and dont forget to set the pattern with pattern: 'yyyy-MM-dd.log
const log4js = require("log4js");
logger = log4js.getLogger();
log4js.configure({
appenders: {
appender: {
type: 'file',
filename: 'log',
keepFileExt: true,
compress: true,
pattern: 'yyyy-MM-dd.log',
alwaysIncludePattern: true,
},
},
categories: {
default: {
appenders: ['appender'],
level: 'all',
},
},
});
logger.debug(`up and running`);
Filename: log.2022-01-26.log
Content: [2022-01-26T14:44:32.769] [DEBUG] default - up and running
Source: Date Rolling File Appender

How add two or more logger using different files?

I am working on node Js logging library. I am using winston for this.I created log files but I stuck when I want to create two log files.Suppose I have to create all log file which contain all logs and error log file which contain only error logs.But I stuck here:
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const tsFormat = () => (new Date()).toLocaleTimeString();
let logger = new(winston.Logger)({
transports: [
new(winston.transports.Console)({
level: 'debug',
timestamp: tsFormat,
handleExceptions: true,
colorize: true,
json: false
}),
new(winston.transports.File)({
name: 'all-file',
handleExceptions: true,
filename: `${logDir}/all-file.log`,
level: 'debug',
maxsize: 100000000,
json: true
}),
new(winston.transports.File)({
name: 'error-file',
handleExceptions: true,
filename: `${logDir}/error-file.log`,
level: 'error',
maxsize: 100000000,
json: false
})
]
});
I want that error log show different type of console than debug console.But by default its only taking debug one. how can I manually add console config for both files.When I try to add another transport.console it shows error.
Transport already attached: console, assign a different name
var winston = require('winston');
let consoleLogger = function(options){
return new (winston.Logger)({
transports: [
new (winston.transports.Console)(options),
]
});
}
// the CustomTransport is to prevent error log show multiple times
class CustomTransport extends winston.Transport {
constructor(options) {
super(options);
this.options = options;
this.levels = options && options.levels || [this.level];
}
log(level, msg, meta, callback) {
if(this.levels.indexOf(level) > -1){
consoleLogger(this.options)[level](msg, meta);
}
callback(null, true);
}
}
winston.transports.CustomTransport = CustomTransport;
const tsFormat = () => (new Date()).toLocaleTimeString();
var logger = new (winston.Logger)({
transports: [
// add name attribute to prevent Transport already attached error
// no error log for this transport
new (winston.transports.CustomTransport)(
{
name: 'info-console',
level: 'debug',
levels : ['debug', 'verbose', 'info', 'warn'],
timestamp: tsFormat,
handleExceptions: true,
colorize: true,
json: false
}
),
// only error log for this transport, modify the configuration as you need
new (winston.transports.CustomTransport)(
{
name: 'error-console',
level: 'error',
timestamp: tsFormat,
handleExceptions: true,
colorize: true,
json: false
}
),
new (winston.transports.File)(
{
name: 'info-file',
filename: 'info.log',
timestamp: true,
maxsize: 1024000,
level: 'info'
}),
new (winston.transports.File)(
{
name: 'error-file',
filename: 'error.txt',
timestamp: true,
maxsize: 1024000,
level: 'error'
})
]});
usage:
logger.debug('debug');
logger.error('error');

Log4js javascript create daily log file

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

Resources