How to flush winston logs? - node.js

I want to flush the winston logger before process.exit.
process.on('uncaughtException', function(err){
logger.error('Fatal uncaught exception crashed cluster', err);
logger.flush(function(){ // <-
process.exit(1);
});
});
Is there anything like logger.flush available? I couldn't find anything about it, other than people complaining about winston not being very actively maintained.
As an alternative, is there any popular (actively maintained) multi-transport logging framework that provides a flushing capability?

Winston actually allows you to pass in a callback which is executed when all transports have been logged:
process.on('uncaughtException', function(err) {
logger.log('error', 'Fatal uncaught exception crashed cluster', err, function(err, level, msg, meta) {
process.exit(1);
});
});
Docs: https://github.com/flatiron/winston#events-and-callbacks-in-winston

Unfortuantely, Winston will sometimes call the logging callback before the transport has had a chance to flush, so the accepted answer can still lead to un-saved log messages (especially on the first turn of the event loop). A better solution is implemented in the winston-log-and-exit package / patch.

Calling process.exit inside the log-callback like Thomas Heymann suggested will not ensure that the logs are actually flushed, especially when using a File-transport.
Instead of calling process.exit directly I would let the logger call process.exit after the log was flushed:
logger.js :
var winston = require('winston');
winston.loggers.add('my-logger', {
console: {
level: 'debug',
colorize: true,
timestamp: true,
handleExceptions: true
},
file: {
level: 'info',
colorize: false,
timestamp: true,
filename: file,
handleExceptions: true
}
});
var logger = winston.loggers.get('my-logger');
/* ******* *
* EXPORTS
* ******* */
exports.exitAfterFlush = function(code) {
logger.transports.file.on('flush', function() {
process.exit(code);
});
};
exports.info = function() {
logger.info.apply(this, arguments);
};
exports.warn = function() {
logger.info.apply(this, arguments);
};
exports.error = function() {
logger.info.apply(this, arguments);
};
And in your code:
var logger = require('./logger.js');
logger.exitAfterFlush(0);
info('Done!');
Tested on NodeJS v4.1.2 and winston 1.1.0

This may help someone. logger is an instance of winston.createLogger which defines two transports (console & file). The exit code is reflected in the shell.
const logger = require('../library/log');
function exitAfterFlush() {
logger.on('finish', function () {
logger.end();
});
};
process.on('exit', (code) => {
console.log(`About to exit with code: ${code}`);
});
logger.info(`Started with PID:${process.pid}`);
logger.error(`*****`);
process.exitCode = 11;
exitAfterFlush();

Winston has a better way to deal with this exceptions.
var logger = new (winston.Logger)({
transports: [
new winston.transports.File({ filename: 'path/to/all-logs.log' })
],
handleExceptions: true,
exceptionHandlers: [
new winston.transports.File({ filename: 'path/to/exceptions.log' })
]
});

Related

How to use winston in NodeJs?

I am trying to create a sample nodeJs program to use and understand winston library for nodeJs,
I had a sample code from one of the existing projects and I am trying to create a sample code sample,
The code contains winston configuration and a simple test function to print logs using different log levels.
I'm using node version 4.6.2, I know it's pretty old but, the existing application is using the same, therefore I wanted to understand the implementation on the same version, The example consists of the following code and a log folder, in which I wish to print the log.
Following is a sample code which I am trying to run -
var winston = require('winston');
var envData = require('dotenv').config({path: 'server.env'})
require('winston-daily-rotate-file');
var levelLog = 'debug';
var winstonTransports = [];
const logDTFormat = () => (new Date().toFormat('DD MMM YYYY HH24:MI:SS'));
// Winston Rotate File Logs
var transportDailyRotate = new (winston.transports.DailyRotateFile)({
filename: './logs/hrfid_app_',
datePattern: 'yyyy-MM-dd.log',
prepend: false,
level: levelLog,
timestamp: function() {
var dateTime = new Date(Date.now());
return dateTime.toFormat('DD/MM/YYYY HH24:MI:SS');
}
});
winstonTransports.push(transportDailyRotate);
// Winston Console Log
var trasportConsole = new (winston.transports.Console)({
timestamp: logDTFormat,
colorize: true,
level: levelLog
});
winstonTransports.push(trasportConsole);
// Winston Config
winston.configure({
level: levelLog,
transports: winstonTransports
});
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
// Validate Server Configuration
if (envData.error) {
winston.error('[APP-CONFIG]:', JSON.stringify(envData.error))
winston.error('[APP-CONFIG]:', 'Error on Server Configuration')
return
}
var test = function(){
winston.error("ERROR log");
winston.info("INFO log");
winston.debug("DEBUG log");
}
test();
I am getting the following error,Can someone please help fix my sample code?
Upgrade to node 6 or 8.
Latest winston require at least node 6.4
https://github.com/winstonjs/winston/blob/c42ab7fdc51b88db180a7dd90c52ce04ddd4e054/package.json#L69
Or use an older version of winston by deleting the existing one and execute npm install winston#2.4.4

Winston not logging uncaught exceptions

I'm trying to use winston to log unhandled exceptions to file and console. The problem is that it is not logging them. Here my setup:
var winston = require('winston');
let file = process.env.APP_TOOL_SET_API_PROJECT+"/logs/app.log";
console.log(process.env.APP_TOOL_SET_API_PROJECT);
// define the custom settings for each transport (file, console)
var options = {
file: {
level: 'info',
filename: file,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: false,
json: true,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
var logger = winston.createLogger({
format: winston.format.combine(
//winston.format.label({ label: '[my-label]' }),
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
//
// The simple format outputs
// `${level}: ${message} ${[Object with everything else]}`
//
winston.format.simple(),
//
// Alternatively you could use this custom printf format if you
// want to control where the timestamp comes in your final message.
// Try replacing `format.simple()` above with this:
//
winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exceptionHandlers: [
new winston.transports.File({ filename: process.env.APP_TOOL_SET_API_PROJECT+"/logs/exceptions.log" }),
],
exitOnError: false, // do not exit on handled exceptions
});
// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
write: function(message, encoding) {
// use the 'info' log level so the output will be picked up by both transports (file and console)
logger.info(message);
},
};
module.exports = logger;
To add a bit more of information, if I wrap a thrown exception with setTimer it then is logged correctly.
setTimeout(() => {
throw new Error('hello world');
}, 250);
But this only would work for user thrown exceptions and feels really ugly to have to wrap each throw with setTimeout.
Is there any solution for this?
Well, just after posting the answer, my mental background thread spit the possible problem.
Obviously I was throwing the exception (just testing) too early and I wasn't given winston enough time to init properly.
This is why waiting with the setTimer was logging correctly.

Winston not Logging to console in typescript

I am confused by winston. I am using the following typescript code to log onto the console in my *.ts file:
import { Logger, LoggerInstance } from "winston";
const logger:LoggerInstance = new Logger();
logger.info('Now my debug messages are written to the console!');
the console remains empty. There are no compile errors or other issues.
At the same time the following works fine:
const wnstn = require("winston");
wnstn.info('Finally my messages are written to the console!');
Does anyone have a clue why that is the case? Do I have to configure the Logger differently? How would I use the defaults I get from the second example?
This is the Typescript way to import Winston.
First, be sure you have installed the typing :
npm i -D #types/winston
Then, in your script.ts
import { Logger, transports } from 'winston';
var logger = new Logger({
transports: [
new transports.Console(),
new transports.File ({ filename: 'somefile.log' })
]
});
In genral, you can import all constants and types without using winston.<...> before.
When you instantiate a new Logger instance you need to provide it a list of transports so it knows where to send the logs:
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
Well,
thanks to the hint of #idbehold , I found that a plain and easy:
import * as winston from "winston";
winston.info('Now my debug messages are written to the console!');
works for the default logger..
Well, I am following below file as winston.ts
import * as appRoot from 'app-root-path';
import * as winston from 'winston';
// define the custom settings for each transport (file, console)
const options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
const logger: winston.Logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exitOnError: false, // do not exit on handled exceptions
});
// create a stream object with a 'write' function that will be used by `morgan`
const myStream = {
write: (message: string) => {
// use the 'info' log level so the output will be picked up by both transports (file and console)
logger.info(message);
}
};
export default logger;
as using inside app.ts as this.app.use(morgan('combined', { stream: winston.stream }));

Logging only the message using winston

I am using winston logger in my node app and i want to log my custom message to the log file.
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info',
filename: '/tmp/test.log',
handleExceptions: true,
json: true,
maxsize: 20971520 //20MB
})
],
exitOnError: false
});
I only need to log message in the log, i dont need to log level and timestamp in the log file. Current logged sample is as below.
{"level":"info","message":"sample entry to log","timestamp":"2015-12-11T09:43:50.507Z"}
My intention is to get entry in the log file as below
sample entry to log
How to achieve this?
Winston 3:
import winston from 'winston';
const logger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.Console({
format: winston.format.printf(log => log.message),
})
]
});
logger.info('eat my shorts');
On Winston 2.x
If you want to print only the message, you can achieve this by setting:
json: false,
timestamp: false,
showLevel: false
Full Logger example:
let logger = new Logger({
transports: [
new (transports.File)({
name: 'info-file',
filename: 'filelog-info.log',
json: false,
timestamp: false,
showLevel: false
})
],
exitOnError: false
});
logger.log('info', 'eat my shorts');
gives in filelog-info.log: eat my shorts
FYI: filters and rewriters didn't help here
If anyone, as did I, finds this question in hopes of making express-winston log only message (to the console in my case)
Here is what I did:
const winston = require('winston');
const expressWinston = require('express-winston');
const { MESSAGE } = require('triple-beam');
expressWinston.logger({
transports: [
new winston.transports.Console({
format: simpleformatter()
})
]
});
const simpleformatter = winston.format(info => {
info[MESSAGE] = info.message;
return info;
});
Yep got the one i was looking for
https://github.com/winstonjs/winston#filters-and-rewriters
https://github.com/winstonjs/winston#custom-log-format

Multiple log files with Winston?

We'd like to use Winston for our logging in Node.js. But, we can't figure out how to have two log files: one for just errors, and one for everything else.
Doing this the naive way doesn't work, however: adding multiple winston.transports.File transports gives an error.
Others have run into this problem, with vague hints of a solution, but no real answer.
Any ideas?
Unfortunately, the patch that pesho mentioned seems to be still not included in the official version (see stephenbeeson's comment in the pull request #149).
So, I used a workaround instead. As winston compares the name attributes, you can fool it by defining the name yourself:
winston = require 'winston'
logger = new winston.Logger
transports: [
new winston.transports.File
name: 'file#debug'
level: 'debug'
filename: '/tmp/debug.log'
new winston.transports.File
name: 'file#error'
level: 'error'
filename: '/tmp/error.log'
]
logger.error 'error' # both logs
logger.debug 'debug' # on debug log
Maybe not elegant, but at least it works.
In the meantime, you can implement a rudimentary wrapper using the same interface like so
var winston = require('winston');
var configs = require('./env.js');
var debug = new winston.Logger({
levels: {
debug: 0
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'debug'}),
new (winston.transports.Console)({level: 'debug'})
]
});
var info = new winston.Logger({
levels: {
info: 1
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'info'}),
new (winston.transports.Console)({level: 'info'})
]
});
var warn = new winston.Logger({
levels: {
warn: 2
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'warn'}),
new (winston.transports.Console)({level: 'warn'})
]
});
var error = new winston.Logger({
levels: {
error: 3
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'error'}),
new (winston.transports.Console)({level: 'error'})
]
});
var exports = {
debug: function(msg){
debug.debug(msg);
},
info: function(msg){
info.info(msg);
},
warn: function(msg){
warn.warn(msg);
},
error: function(msg){
error.error(msg);
},
log: function(level,msg){
var lvl = exports[level];
lvl(msg);
}
};
module.exports = exports;
This will cover the basic winston API. could be extended for metadata and so on...
I just sent a pull request that allows using multiple File transports in one logger.
https://github.com/flatiron/winston/pull/149
It is already merged into flatiron/winston.
You can also use my forked repo:
https://github.com/pdobrev/winston
You just need to give the transport a custom name property so you don't have a collision:
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ name: 'text', filename: logFile, json: false }),
new (winston.transports.File)({ name: 'json', filename: logFileJson })
]
});
You can read more about multiple transports in the docs: https://github.com/winstonjs/winston#multiple-transports-of-the-same-type
This feature is now officially supported in Winston and is addressed in the README here
Code example:
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}

Resources