1.0.1 • Published 8 years ago

transport-logger v1.0.1

Weekly downloads
20
License
MIT
Repository
github
Last release
8 years ago

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

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

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.

1.0.1

8 years ago

1.0.0

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago