Nutrient Packed Morsels

    corie-logger

    3.3.2 • Public • Published

    corie-logger

    中文文档

    npm package

    Note: The Logging Framework for JavaScript with log4j configuration writes lines to a file that roll over when they reach a maximum size, or a date/time. You could provide any custom Appenders to write log messages wherever you need, it currently support logging messages to console and files.


    Table of contents


    Installation

    Node >= 8

    npm install --save corie-logger
     
    # or 
     
    cnpm install --save corie-logger

    Usage

     
    const { configure, getLogger } = require('corie-logger');
     
    // './conf/corie-logger.json' will become to
    // `${process.cwd()}/conf/corie-logger.json`
    configure('./conf/corie-logger.json');
     
    const appLogger = getLogger('app');
     
    // logging for log4j
    appLogger.trace('message', 'message2', ...);
    appLogger.debug('message', 'message2', ...);
    appLogger.info('message', 'message2', ...);
    appLogger.warn('message', 'message2', ...);
    appLogger.error('message', 'message2', ...);
    appLogger.fatal('message', 'message2', ...);
     
    // changed levels
    appLogger.setLevels('syslog');
     
    // logging for syslog
    appLogger.debug('message', 'message2');
    appLogger.info('message', 'message2');
    appLogger.notice('message', 'message2');
    appLogger.warn('message', 'message2');
    appLogger.error('message', 'message2');
    appLogger.critical('message', 'message2');
    appLogger.alert('message', 'message2');
    appLogger.emergency('message', 'message2');
     
    // The first optional param is level
    appLogger.log('INFO', 'message', 'message2', ...);
    appLogger.log('error', 'message', 'message2', ...);
    appLogger.log('message', 'message2', ...);
     

    Custom Appender

    Must implement functions append and destroy

     
    'use strict';
     
    const { AbstractAppender } = require('corie-logger');
     
    class CustomAppender extends AbstractAppender {
     
      constructor(name, options, config) {
        super(name, options, config);
        // options - current appender options
        // config - global config info
        // ... 
        // codes
        // ...
      }
     
      append(args, options) {
        // options.category - logger name
        // options.level    - logger level
        // options.timestamp
     
        // codes
      }
     
      destroy() {
        // destroy this appender
      }
     
    }
     
    module.exports = CustomAppender;
     

    Custom Layout

     
    'use strict';
     
    module.exports = function (config) {
      return function (args, options) {
        // options.category    logger name
        // options.level       logger level
        // options.timestamp
     
        // return formal args
      };
    };
     

    API

    Global API for corie-logger

    configure( opts )
    Param Type Description
    [ opts ] Object a JSON Object
    [ opts ] String a JSON file path
    destroy()

    destroy internal cache

    getLogger( name )
    Param Type Description
    [ name ] String get a Logger instance
    addLayout( name, serializerGenerator )
    Param Type Description
    [ name ] String a name of serializer generator function
    [ serializerGenerator ] Function a serializer generator function
    addAppender( name, Appender )
    Param Type Description
    [ name ] String a name of custom Appender
    [ Appender ] Class a custom Appender

    API for Logger instance

    setLevels
    Param Type Description
    [ levels ] String provide log4j and syslog currently
    [ levels ] Array custom levels
    setLevel
    Param Type Description
    [ levels ] String logger level

    Configuration

    appenders

    • type - "file", "dateFile" or "console". "file" means to log messages to a file; "dateFile" means to log messages to a file with datetime filename; "console" means to log messages to console.
    • layout - optional. "basic", "json" or an object(default: "basic"). "json" means to format a message to a JSON string.
    • fileName - when type is "dateFile", the date pattern must be configured in the file name.
    • maxLogSize - optional, use case insensitive k、m、g as unit
    • numBackups - optional, "10d" can be configured that means to back up logs for 10 days
    • level - optional, log level

    categories

    • appenders - configure different appenders
    • level - log level
    • levels - log levels

    level - log level

    levels - "log4j", "syslog", Array. log levels

     
    {
      "appenders": {
        "log1": { // getLogger('log1')
          "type": "file", // log messages to a file
          "layout": "json", // optional, not configured or json, or an object
          "fileName": "./logs/app.log", // log file path
          "maxLogSize": "10M", // optional, use case insensitive k、m、g as unit
          "numBackups": 10 // optional, "10d" can be configured that means to back up logs for 10 days
        },
        "log2": { // getLogger('log2')
          "type": "dateFile", // log messages to a file with a datetime file name
          "fileName": "./logs/log1-%d{YYYY-MM-DD}.log" // log file path with a datetime pattern
        },
        "log3": { // getLogger('log3')
          "type": "console", // log messages to console
          "layout": {
            "datePattern": null // default: YYYY-MM-DD hh:mm:ss.SSS Z. null means ISOString formatter
          }
        }
      },
      "categories": {
        "default": {
          "appenders": [
            "app"
          ],
          "level": "ERROR"
        },
        "console": {
          "appenders": [
            "log2"
          ],
          "level": "INFO"
        },
        "file": {
          "appenders": [
            "app",
            "log2"
          ],
          "level": "INFO"
        },
        "dateFile": {
          "appenders": [
            "log1",
            "log2"
          ],
          "level": "INFO"
        }
      },
      "level": "ERROR", // optional, log lovel
      "levels": "syslog" // optional(default: "log4j"), "log4j", "syslog"
    }
     

    Examples

    Release History

    3.1.0

    • Fix bugs about throwing a Error before calling function configure
    • Removed caches after calling function getConsole
    • Changed property name filename to fileName, filename can be compatible
    • Property pattern have been removed in property appenders
    • Changed property name pattern to datePattern in property layout

    3.1.1

    • Added function setLevel on the Logger instance
    • Removed the RedisAppender
    • Fix bugs about using custom Appender

    3.1.2

    • Optimized codes

    3.1.3

    • Supported to change levels for all category of logger
    • Supported to change level for all category of logger

    3.2.0

    • Supported to change levels for a category of logger
    • added jest

    3.2.1

    • extends corie-console

    3.2.2

    • added extra options to append function for Appender

    3.2.3

    • updated tests and package.json

    3.2.4

    • added stdout and stderr Appender

    3.2.5

    • extra options will be passed to each Appender

    3.2.6

    • removed type "dateFile"

    3.2.7

    • fixed ...extraOptions not being supported in node 8.2.1

    3.3.1

    • updated plugins

    3.3.2

    • updated plugins

    Install

    npm i corie-logger

    DownloadsWeekly Downloads

    0

    Version

    3.3.2

    License

    MIT

    Unpacked Size

    55.3 kB

    Total Files

    32

    Last publish

    Collaborators

    • fengxinming