Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to manage logging using winston for production and development in node js "github"

const winston = require('winston');
const { createLogger, format, transports } = require('winston');

const dotenv = require('dotenv');
dotenv.config();

var logger = createLogger({
    level: 'info',
    format: format.combine(
        format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss',
        }),
        format.printf((info) =>
            JSON.stringify({
                t: info.timestamp,
                l: info.level,
                m: info.message,
                s: info.splat !== undefined ? `${info.splat}` : '',
            }) + ','
        )
    ),
});

if (process.env.NODE_ENV !== 'PRODUCTION') {
    logger.add(new transports.Console({ format: winston.format.cli() }));

    // Turn these on to create logs as if it were production
    // logger.add(new transports.File({ filename: 'log/output/error.log', level: 'error' }));
    // logger.add(new transports.File({ filename: 'log/output/warn.log', level: 'warn' }));
    // logger.add(new transports.File({ filename: 'log/output/info.log', level: 'info' }));
} else {
    logger.add(new transports.File({ filename: 'log/output/error.log', level: 'error' }));
    logger.add(new transports.File({ filename: 'log/output/warn.log', level: 'warn' }));
    logger.add(new transports.File({ filename: 'log/output/info.log', level: 'info' }));
}

module.exports = {
    logger,
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery window new tab with post 
Javascript :: buildpack for nodejs 
Javascript :: reactjs navbar component 
Javascript :: splash screen react native 
Javascript :: javascript map to object 
Javascript :: convert days in years js 
Javascript :: js for array length 
Javascript :: how to change input value in javascript using class 
Javascript :: after effects loop wiggle 
Javascript :: generate empty array js 
Javascript :: jquery function done 
Javascript :: javascript unicode to string 
Javascript :: multiple conditions for JavaScript .includes() method 
Javascript :: composer require ozee 31/cakephp-cors:^1 
Javascript :: await and catch javascript 
Javascript :: node express params 
Javascript :: setinterval vs settimeout 
Javascript :: jquery for element which doesnt exist on page load 
Javascript :: javascript palindrome 
Javascript :: jquery not equal 
Javascript :: var js 
Javascript :: random color js 
Javascript :: reactjs date display 
Javascript :: json data types 
Javascript :: add class name in html 
Javascript :: js ignore case 
Javascript :: javascript check if consecutive array 
Javascript :: react native Setting a timer for a long period of time 
Javascript :: ion icon react 
Javascript :: js string to blob 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =