tektrans-logger

    1.2.3 • Public • Published

    tektrans-logger

    License: MIT NodeJS ESLint Winstonjs Tektrans

    Version npm Dependency Status

    NPM

    A wrapper of winstonjs logger, replacing deprecated logger from KOMODO-SDK.

    This logger should be used by TEKTRANS projects. But ofcourse you can use it too.

    Table of contents

    Features

    • Create multiple transports automatically by default:
    • Not creating DailyRotateFile transport if test environment detected. This is the most reason we need a simple wrapper for winstonjs logger.
    • Ability to change log directory.
    • Ability to change log base filename.
    • Ability to publish to redis channel (implemented using winston-redis) - experimental (v1.2.0)

    Future features

    • Circular buffer transport.
    • MySQL transport (planned for v1.3.0).
    • MongoDB transport.

    Install

    npm i tektrans-logger

    Usage

    Using tektrans-logger is easy. Just include the module and you can use it with default behaviors.

    const logger = require('tektrans-logger');
    
    logger.info('User created', {
        username: 'johndoe',
        fullename: 'John Doe',
        comment: 'who is he?',
    });
    
    logger.warn('A warn message', {
        eCode: e.code, eMessage: e.message
    });

    See here for more examples.

    Behaviors

    You can override behavior by using environment (process.env) or global variable or by specified in config object. Remember to put those override statement before first call of "require('tektrans-logger')" statement.

    Here is the list:

    • TEKTRANS_LOGGER_CONFIG: config object
    • TEKTRANS_LOGGER_LEVEL: minimum log level to dump
      • default: "verbose. "
      • alias: LOGLEVEL
      • config property: config.level
    • TEKTRANS_LOGGER_LABEL: log label
      • default: "" (empty string)
      • alias: KOMODO_LOG_LABEL
      • config property: config.label
    • TEKTRANS_LOGGER_DO_NOT_USING_FILE: set it to any value to make logger without using file transport
      • default: null
    • TEKTRANS_LOGGER_USING_FILE: set it to force using file transport even if it was called from test environtment
      • default: null
    • TEKTRANS_LOGGER_DIRECTORY: directory to put log files
      • default: 'logs' directory on working directory
      • config property: config.directory
    • TEKTRANS_LOGGER_FILENAME: base filename for log file
      • default: 'log'
      • alias: KOMODO_LOG_FILENAME
      • config property: config.filename
    • TEKTRANS_LOGGER_CONSOLE_LEVEL: minimum log level to dump using Console transport
      • default: same as TEKTRANS_LOGGER_LEVEL
      • config property: config.console_level
    • TEKTRANS_LOGGER_FILE_LEVEL: minimum log level to dump using file transport
      • default: same as TEKTRANS_LOGGER_LEVEL
      • config property: config.file_level
    • TEKTRANS_LOGGER_MAX_FILES: maximum number of log files to keep. If not set, no logs will be removed. This can be a number of files or number of days. If using days, add 'd' as the suffix. See DailyRotateFile
      • default: null
      • config property: config.max_files

    Config default object

    {
      level: 'verbose',
      label: null,
    
      // default is "logs" directory on current workdir
      directory: path.join(process.cwd(), 'logs'),
    
      filename: 'log',
    
      // default is using generic level value
      console_level: null,
    
      // default is using generic level value
      file_level: null,
    
      // default is no old file removal
      max_files: null,
    }

    See examples/using-config.js for usage example of using config object.

    Experimental Redis transport

    Redis transport can be enabled by putting "redis" property on config object.

    This config object will use default options for redis transport:

    {
      // ...
      redis: true
    }

    This will specify some property of redis transport:

    {
      // ...
      redis: {
        level: 'verbose',
        host: 'localhost',
        port: 6379,
        auth: null,
        channel: null,
      }
    }

    Default value of Redis transport

    • level: same as general level value
    • host: 'localhost'
    • port: 6379
    • auth: null, no authentication
    • channel: "TEKTRANS-LOGGER_B707E453_<LOG-LABEL-IN-UPPERCASE_IF-SPECIFIED>,". For example if you specify log label as "xyz", channel will be "TEKTRANS-LOGGER_B707E453_XYZ". If you don't specify log label, channel name will be "TEKTRANS-LOGGER_B707E453".

    See examples/redis.js for code example.

    Caution

    Looks like logger.end() will not wait for all logs to be flushed on redis. Expect for missing some of last logs on redis. Or you can put some delay (like 1-2 seconds) before closing the logger with end method.

    Changelog

    See CHANGELOG.md.

    License

    Licensed under MIT License (see this file).

    Feel free to use or fork it if you think it would be usefull for you.

    Copyright PT. TEKNOLOGI TRANSAKSI DIGITAL (TEKTRANS) 2021.

    TEKTRANS

    Install

    npm i tektrans-logger

    DownloadsWeekly Downloads

    10

    Version

    1.2.3

    License

    MIT

    Unpacked Size

    30.8 kB

    Total Files

    18

    Last publish

    Collaborators

    • adhisimon