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?
Creating the Custom Logger classcreateLogger() method. If you wish to completely change the format of the logs you could refer to Winston docs.
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).
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With