how to use winston to setup log in a sub directory instead of root directory?

codeofnode picture codeofnode · Jun 14, 2014 · Viewed 7.7k times · Source

I want to setup logs for my nodejs project inside a directory named logs

as per documentation here

i am doing :

winston.add(winston.transports.File, { filename: 'logs/mylogs.log' });

But it is doing nothing.

How do i achieve the same?

Answer

vivek s vamja picture vivek s vamja · Nov 18, 2015

Put the below code in your server file.

var winston = require('winston');
var fs = require( 'fs' );
var path = require('path');
var logDir = 'log'; // directory path you want to set
if ( !fs.existsSync( logDir ) ) {
    // Create the directory if it does not exist
    fs.mkdirSync( logDir );
}
var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)({
            colorize: 'all'
        }),
        new (winston.transports.File)({filename: path.join(logDir, '/log.txt')})
    ]
});
logger.info("Anything you want to write in logfile");