Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    transport-loggerpublic

    Transport Logger

    A multi-transport logger inspired by winston with the goal of being simple and small while still retaining some of the flexibility found in larger multi-transport loggers like winston.

    Table of Contents

    Basic usage

    Install via npm:

    npm install transport-logger

    Default logging

    Instantiating a logger without any arguments creates a console logger with five levels ('error', 'warn', 'info', 'debug', and 'trace') and a minimum logging level of 'info'.

    var Logger = require('transport-logger');
    var logger = new Logger();
     
    logger.error('error');
    logger.warn('warn');
    logger.info('info');
    logger.debug('debug');
    logger.trace('trace');

    will print

    error
    warn
    info

    Customizing the output

    The output can be customized as follows:

    var Logger = require('transport-logger');
    var logger = new Logger({
      minLevel: 'trace',
      timestamp: true,
      prependLevel: true,
      colorize: true
    });
     
    logger.error('error');
    logger.warn('warn');
    logger.info('info');
    logger.debug('debug');
    logger.trace('trace');

    which will print

    [ERROR] Sun May 05 2013 16:36:19 GMT-0700 (PDT): error
    [WARN] Sun May 05 2013 16:36:19 GMT-0700 (PDT): warn
    [INFO] Sun May 05 2013 16:36:19 GMT-0700 (PDT): info
    [DEBUG] Sun May 05 2013 16:36:19 GMT-0700 (PDT): debug
    [TRACE] Sun May 05 2013 16:36:19 GMT-0700 (PDT): trace
    

    Further customization of output can be accomplished using a formatter callback function. Custom log levels can also be specified. For more information, read the Complete API section.

    Logging to a file

    To log to a file, specify a path in the options object:

    var Logger = require('transport-logger');
    var logger = new Logger({
      destination: 'path/to/logfile'
    });
     
    logger.error('error');
    logger.warn('warn');
    logger.info('info');
    logger.debug('debug');
    logger.trace('trace');

    When logging to a file, it may be useful to cap files at a certain size. Setting the maxLines option will cause the current log file to be moved to a new filed called [destination].# once maxLines number of lines is reached. The number at the end the filename is incremented each time a new file is archived, such that [destination].1 is older than [destination].2

    var Logger = require('transport-logger');
    var logger = new Logger({
      destination: 'path/to/logfile',
      maxLines: 5
    });
     
    logger.error('error');
    logger.warn('warn');
    logger.info('info');
    logger.debug('debug');
    logger.trace('trace'); // Will cause a new log file to be created
    logger.info('new'); // Will be the first line logged to the new file

    Using multiple transports

    To log to multiple transports, specify an array of configuration options

    var Logger = require('transport-logger');
    var logger = new Logger([{
      destination: 'path/to/logfile',
      minLevel: 'trace'
    }, {
      minLevel: 'debug'
    }]);
     
    logger.error('error');
    logger.warn('warn');
    logger.info('info');
    logger.debug('debug');
    logger.trace('trace');

    To use the default console logger as one of the transports, just specify an empty object

    Custom log levels

    To use custom log levels, specify an object mapping log level names to colors as the second argument to the Logger constructor:

    var Logger = require('transport-logger');
    var logger = new Logger({
      minLevel: 'b',
      colorize: true
    }, {
      levels: [{
        id: 'a',
        color: 'red'
      }, {
        id: 'b',
        color: 'green'
      }, {
        id: 'c',
        color: 'blue'
      }, {
        id: 'd',
        color: 'cyan'
      }, {
        id: 'e',
        color: 'magenta'
      }]
    });
     
    logger.a('a');
    logger.b('b');
    logger.c('c');
    logger.d('d');
    logger.e('e');

    Available colors are 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', and 'normal' (default terminal color). If not using colors, these can be any value.

    Note: if one of the transports is a console transport and the log level is not a native console method, the closest log level that is on console will be found. For example, if syslog levels are defined, then emergency, alert, and critical are specified, they will be logged via console.error().

    Named transports

    Each transport can have a name assigned to it, so that they can be referenced later. With named transports, you can specify a different message for each transport in a log. This can be used, for example, to log a message that is localized for the user to the console, but log a message that is localized for the developer to a file.

    var Logger = require('transport-logger');
    var logger = new Logger([{
      name: 'file',
      destination: 'path/to/logfile'
    },{
      name: 'console'
    }]);
    logger.info({
      console: 'Dies ist eine Nachricht',
      file: 'This is a message'
    })

    Formatter functions

    If the built-in options are not sufficient for your logging needs, you can define a formatter function to perform any arbitrary formatting:

    var Logger = require('transport-logger');
    var logger = new Logger({
      formatter: function (messages, level, settings) {
        return level.id + '?' + messages.join('+');
      }
    });
     
    logger.error('error', 'foo');
    logger.warn('warn', 'foo');
    logger.info('info', 'foo');
    logger.debug('debug', 'foo');
    logger.trace('trace', 'foo');

    will print

    error?error+foo
    warn?warn+foo
    info?info+foo

    Complete API

    Constructor

    new Logger(transports, settings);

    Parameters

    Name Type Description
    transports <optional> Object | Array[Object] The settings for the transport or transports
    Name Type Description
    timestamp <optional> Boolean Timestamps each log message with a UTC date (default false)
    prependLevel <optional> Boolean Prepends each log message with the log level (default false)
    minLevel <optional> String The minimum logging level (default 'info'). If something falsey is passed in, then nothing is logged. This can be used to implement a --silent flag in the application
    formatter <optional> Function A function to format messages with. An array of all messages to log is passed in as the only argument. The formatter function shall return a string that is logged
    destination <optional> String | stream.Writable The path to the log file, or an existing stream. If a file path is specified, it does not have to exist already. If the file exists, new log messages are appended to the existing contents. Takes precedence over the stream property.
    name <optional> String The name of the transport
    settings <optional> Object The global settings that apply to all transports
    Name Type Description
    levels Object The custom levels for the logger
    Name Type Description
    <level name> String Each level name is specified as a key, and it's log color is the value

    Returns

    An instance of the logger. Each logger instance has a method that corresponds with each log level. Each method takes an arbitrary number of arguments, converts them to strings, and contatenates them with a space in between them.

    License

    The MIT License (MIT)

    Copyright (c) 2013 Bryan Hughes bryan@theoreticalideations.com (http://theoreticalideations.com)

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    install

    npm i transport-logger

    Downloadslast 7 days

    25

    version

    1.0.1

    license

    MIT

    repository

    github.com

    last publish

    collaborators

    • avatar