Home Manual Reference Source Test Repository
import Logger from '@ls-age/logger/src/Logger.js'
public class | source

Logger

Extends:

events~Emitter → Logger

The logger class. Please note that, when importing default from this module, you will always get a shared Logger instance.

See:

Test:

Static Member Summary

Static Public Members
public static get

The default log types.

Constructor Summary

Public Constructor
public

constructor(options: Object)

Creates a new Logger with the given options.

Member Summary

Public Members
public

The log level where no messages are printed.

public

The log levels available.

public

[type](messages: ...*): Logger: *

Prints the given messages for the given message types.

public

colors: chalk

The colors used.

public get

The message prefix.

Private Members
private

The log types available.

Method Summary

Public Methods
public

pipe(stream: stream~Writable, options: Object): Logger

Writes all logs to a writable stream.

public

typesForLevel(logLevel: number): string[]

Returns the log types that should be handled for the specific log level.

Private Methods
private

_format(messages: *[]): string

Creates a log message from some messages, prefixed and postfixed with a newline.

Static Public Members

public static get defaultTypes: string[] source

The default log types.

Public Constructors

public constructor(options: Object) source

Creates a new Logger with the given options.

Params:

NameTypeAttributeDescription
options Object
  • optional

The options to use.

options.types string[]
  • optional
  • default: Logger.defaultTypes

The log types the new instance should have.

Test:

Public Members

public LEVEL_SILENT: number source

The log level where no messages are printed.

public ['LEVEL_' + typeName]: number source

The log levels available.

public [type](messages: ...*): Logger: * source

Prints the given messages for the given message types.

Return:

Logger

The logger instance.

Emit:

string

Emits an event named after type with the message printed.

Example:

Subscribing to specific log types
Logger.on('warn', msg => console.log('Warning:', msg))
  .warn('Test warning');
// Will print "Waring: Test warning" to the console.

public colors: chalk source

The colors used.

public get prefix: string source

The message prefix. Override this getter in your own logger class to customize output.

Test:

Private Members

private _types: string[] source

The log types available.

Public Methods

public pipe(stream: stream~Writable, options: Object): Logger source

Writes all logs to a writable stream.

Params:

NameTypeAttributeDescription
stream stream~Writable

The stream to write to

options Object
  • optional

The options to use.

options.logLevel number
  • optional
  • default: 3

The log level from which to write.

Return:

Logger

The logger instance.

Example:

Pipe logger output to stdout
Logger.pipe(process.stdout);
Pipe with specific log level
Logger.pipe(process.stdout, { logLevel: Logger.LEVEL_ERROR });
// In this case, only error messages will be printed to stdout

Test:

public typesForLevel(logLevel: number): string[] source

Returns the log types that should be handled for the specific log level.

Params:

NameTypeAttributeDescription
logLevel number

The log level to use.

Return:

string[]

The log types to handle.

Private Methods

private _format(messages: *[]): string source

Creates a log message from some messages, prefixed and postfixed with a newline.

Params:

NameTypeAttributeDescription
messages *[]

The messages to format.

Return:

string

The resulting message.

Test: