node.jsloggingmorgan

How to use simple node logger module


This below function created mylogfile but can't error the logs of api response with timestamp and error in app

const SimpleNodeLogger = require('simple-node-logger'),
    opts = {
        logFilePath:'mylogfile.log',
        timestampFormat:'YYYY-MM-DD HH:mm:ss.SSS'
    },

log = SimpleNodeLogger.createSimpleLogger( opts );

Solution

  • seems you're missing something ... here's an example

    // utilities/logger.js
    
    
    const SimpleNodeLogger = require('simple-node-logger');
    const opts = {
        logFilePath:'mylogfile.log',
        timestampFormat:'YYYY-MM-DD HH:mm:ss.SSS'
    };
    
    const log = SimpleNodeLogger.createSimpleLogger(opts);
    
    module.exports = log;
    

    and then, just use it

    // index.js
    
    const logger = require('./utilities/logger');
    
    
    logger.info(`I'm an information line`);
    logger.debug(`I'm a debug line`);
    logger.error(`I'm an error line`);
    

    that will output in a new created file called mylogfile.log:

    2020-12-25 13:37:17.139 INFO  I'm an information line 
    2020-12-25 13:37:17.140 ERROR I'm an error line 
    

    set the log level if you want to output more info, like debug. All options are in the package page titled "How to use"