Node.js Logging

前端 未结 9 1842
一个人的身影
一个人的身影 2021-01-29 17:28

Is there any library which will help me to handle logging in my Node.Js application? All I want to do is, I want to write all logs into a File and also I need an options like ro

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-29 17:52

    Each answer is 5 6 years old, so bit outdated or depreciated. Let's talk in 2020.

    simple-node-logger is simple multi-level logger for console, file, and rolling file appenders. Features include:

    1. levels: trace, debug, info, warn, error and fatal levels (plus all and off)

    2. flexible appender/formatters with default to HH:mm:ss.SSS LEVEL message add appenders to send output to console, file, rolling file, etc

    3. change log levels on the fly

    4. domain and category columns

    5. overridable format methods in base appender

    6. stats that track counts of all log statements including warn, error, etc

    You can easily use it in any nodejs web application:

       // create a stdout console logger
      const log = require('simple-node-logger').createSimpleLogger();
    

    or

      // create a stdout and file logger
      const log = require('simple-node-logger').createSimpleLogger('project.log');
    

    or

      // create a custom timestamp format for log statements
      const SimpleNodeLogger = require('simple-node-logger'),
      opts = {
          logFilePath:'mylogfile.log',
          timestampFormat:'YYYY-MM-DD HH:mm:ss.SSS'
       },
      log = SimpleNodeLogger.createSimpleLogger( opts );
    

    or

      // create a file only file logger
      const log = require('simple-node-logger').createSimpleFileLogger('project.log');
    

    or

      // create a rolling file logger based on date/time that fires process events
      const opts = {
          errorEventName:'error',
           logDirectory:'/mylogfiles', // NOTE: folder must exist and be writable...
            fileNamePattern:'roll-.log',
             dateFormat:'YYYY.MM.DD'
      };
      const log = require('simple-node-logger').createRollingFileLogger( opts );
    

    Messages can be logged by

      log.info('this is logged info message')
      log.warn('this is logged warn message')//etc..
    

    PLUS POINT: It can send logs to console or socket. You can also append to log levels.

    This is the most effective and easy way to handle logs functionality.

提交回复
热议问题