Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 495
codeofnode Avatar asked Jun 14 '14 12:06

codeofnode


People also ask

Which method is used to create your own logger with Winston?

Creating the Custom Logger classcreateLogger() method. If you wish to completely change the format of the logs you could refer to Winston docs.

What is a Winston logger?

winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).

How do I use a Winston logger in typescript?

The logs will be appended, multiple options are available to specify the max size, rotate the file and zip the file. import { createLogger, transports, format } from "winston"; import { createWriteStream } from "fs"; const logger = createLogger({ transports: [ new transports. Stream({ stream: createWriteStream("hello.


1 Answers

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");
like image 59
vivek s vamja Avatar answered Oct 19 '22 23:10

vivek s vamja