Have ideas to improve npm?Join in the discussion! »

    pm2-logwrapper
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.12 • Public • Published

    pm2-logwrapper

    log wrapper around console.log / console.error to implements different log levels and categories. Useful when used with pm2 and/or docker

    Create logger

    const logger = require('pm2-logwrapper')();

    Create logger with specific category

    const logger = require('pm2-logwrapper')("MY_CUSTOM_CATEGORY");

    or

    const logger = require('pm2-logwrapper')().getLogger("MY_CUSTOM_CATEGORY");

    By default the logger is initialized with this set of options

    let opts = {
        errors_on_std_error: false,
        add_timestamp: false,
        add_process: true,
        add_category: true,
        timestamp_format: "yyyy-mm-dd HH:MM:ss",
        default_category : "default",
        categories: {
            "default": "INFO"
        }
    };
    • errors_on_std_error: if true will print ERROR and FATAL logs also on standard error. By default is false and will print these logs only on standard out.
    • add_timestamp: if true will prepend the current timestamp to each log message. By default is false because PM2 can do it
    • add_category: if true the category will be written in the log message as [<category_name>]. By default is true
    • add_process: if true the process identifier will be written in the log message as [process_id]. By default is true. For master process will be [Master] and for worker process will be ["Worker-1"]
    • timestamp_format: used to format the current timestamp, if add_timestamp is enabled. By default is "yyyy-mm-dd HH:MM:ss"
    • default_category: is the name of the default category if none is specified. By default is "default"
    • categories: object of each category you need to use with this logger. By default only "default" category is defined

    You can specify different set of options by passing them to the init method. The object passed will be merged with the default configuration, so you don't need to specify every options, but only the options you need to change

    logger.init({
         errors_on_std_error: true
    });

    To Specify different categories you can initialize the library with an object like.

    logger.init({
        "categories": {
            "myCustomCategory1": logger.getLevels().DEBUG,
            "myCustomCategory2": logger.getLevels().TRACE
        }
    });

    Categories can be changed also with

    logger.setCategory(logger.getLevels().INFO, "myCustomCategory1");

    or

    logger.setCategories( {
         "myCustomCategory1": logger.getLevels().DEBUG,
         "myCustomCategory2": logger.getLevels().TRACE
    });

    The log levels allowed can be found

    logger.getLevels();

    and the available values are:

    {
        OFF: "OFF",
        FATAL: "FATAL",
        ERROR: "ERROR",
        WARN: "WARN",
        INFO: "INFO",
        DEBUG: "DEBUG",
        TRACE: "TRACE"
    }

    The logging method, for each level are:

    logger.info("myMessage");

    This method will use the default category log level for this logger instance. By default is default unless specified in the constructor or with getLogger method

    logger.info("myMessage","myCustomCategory");

    This method will use myCustomCategory category log level for this logger instance

    Each log level has a corresponding method to check if the log level is enabled

    logger.isInfoEnabled();
    logger.isInfoEnabled("myCustomCategory");

    Keywords

    Install

    npm i pm2-logwrapper

    DownloadsWeekly Downloads

    44

    Version

    2.0.12

    License

    MIT

    Unpacked Size

    25.4 kB

    Total Files

    11

    Last publish

    Collaborators

    • avatar