ZEST / base.logger / Module: base-logger

base-logger

require("base-logger")(options) → {module:base-logger~Logger}

This function returns a Logger object that can be used to log messages.

Parameters:
Name Type Argument Description
options string | Array.<module:base-logger~LoggerConfigurations> | module:base-logger~LogOptions <optional>

the options used for initializing the logger.

Source:
Returns:

the logger object

Type
module:base-logger~Logger
Examples
// To create a logger with all defaults, we just call the exported module function
// without any option
// create the logger
var logger = require('base.logger')();
// start logging
logger.debug('hello world!');
// If initialized with an array, the logger will assume it to be an array of
// configuration options.
// create the logger
var logger = require('base.logger')([
    // configuration option
    {
        match: '^.*$', // group patterns matcher
        appender: 'console', // where to log
        level: 'debug', // the log level
        pattern: '%t[HH.mm.ss.SSS]|%-1.1l%20.20g| %m' // the logger pattern
    }
]);
// start logging
logger.debug('hello world!');
// If a string is used for initialization, it is assumed to be the group name. The
// logger will initialize with default configuration (which makes it log everything
// to console) and the returned logger will have its group initialized
// create the logger
var logger = require('base.logger')('main');
// start logging
logger.debug('hello world!');
// To initialize the logger with both the group and settings, we use an object form
// as shown below.
// create the logger
var logger = require('base.logger')({
    group: 'main',
    settings: [
        // configuration option
        {
            match: '^.*$', // group patterns matcher
            appender: 'console', // where to log
            level: 'debug', // the log level
            pattern: '%t[HH.mm.ss.SSS]|%-1.1l%20.20g| %m' // the logger pattern
        }
    ]
});
// start logging
logger.debug('hello world!');

base.logger is a basic logger component used throughout zest to log messages onto the node console or in log files. It can be used as a zest component or as a standalone module

Source:

Requires

Classes

Logger
LoggerBase

Methods

<static> start()

Resumes logging function after it has been stopped

Source:

<static> stop()

Stops all loggers from logging

Source:

Type Definitions

LoggerConfigurations

This object is used to configure the logger.

Properties:
Name Type Argument Default Description
match string <optional>
^.*$

this configuration will be used for group patterns that match the regex provided in match property

appender string <optional>
console

where to log. Possible values are console and file

level string <optional>
debug

the log level upto which logging should happen. Possible values are debug, info, warn, error and none.

pattern string | module:base-logger~LogPattern <optional>

the pattern used for formatting the logs. If the value is a string, the same pattern will be used for logging debug, info, warn and error messages. See module:base-logger~LogPattern for details on how patterns are used.

Source:

LogOptions

LogOptions object can be passed as an alternate way to configure the logger.

Properties:
Name Type Argument Description
group string <optional>

the group pattern for which the configuration should be used.

settings Array.<module:base-logger~LoggerConfigurations> <optional>

the logger configurations

Source:

LogPattern

the LogPattern object defines the different patterns to be used for formatting the logs Pattern Strings are parsed to generate logs. The conversion pattern is closely related to the conversion pattern of the printf function in C. A conversion pattern is composed of literal text and format control expressions called conversion specifiers.

Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character. The conversion character specifies the type of data, e.g. level, group, message and time. The format modifiers control such things as field width, padding, left and right justification. Any literal text can be inserted within the conversion pattern.

The conversion characters are:

  • g for outputting the group name
  • l for outputting the log level
  • t for outputting the current time
  • m for outputting the actual log

A typical conversion specifier will be of the form

%[<<- for left align>>][<<minwidth>>][.][<<max width>>]<<conversion character>>[<<format>>]

format are supported only in case of Dates and is should be moment.js compatible

Example:

%10g | %4.4l - %t[dd/MM/yyyy] - %m

would yield the output

modulename |ebug - [12/12/2014] - Message 1
modulename |warn - [12/12/2014] - Message 1

%15g | %4l - %t[dd/MM/yyyy] - %m

would yield the output

     modulename |debug - [12/12/2014] - Message 1
     modulename |warn - [12/12/2014] - Message 1
Properties:
Name Type Description
debug string

pattern to be used to format debug logs

info string

pattern to be used to format info logs

warn string

pattern to be used to format warn logs

error string

pattern to be used to format error logs

Source: