Home Reference Source Test Repository
import {ColorLogger} from 'typhonjs-color-logger/src/ColorLogger.js'
public class | source

ColorLogger

Provides a color coded logger for ANSI terminal usage. In addition to providing a global scope logger typhonjs-color-logger is optionally plugin enabled via typhonjs-plugin-manager and can self-register on an eventbus with all methods exposed as event bindings.

In the future an option to use CSS colors for browser usage will be enabled.

There are several format options to display additional data / info including location where the log method is invoked in addition to a time stamp. By default the time stamp option is disabled.

When passing in an Error for logging the stack trace of the error will be used for info and trace creation. The trace method will automatically generate a stack trace.

format: [LogLevel] [Time] [File] log text

Log level and color:

  • fatal: light red
  • error: red
  • warn: yellow
  • info: green
  • debug: blue
  • verbose: purple
  • trace: light cyan

Each log method for the log levels above have two alternate versions that are accessed by appending Compact, NoColor or Raw to the method name. Or if using event bindings appending :compact, :nocolor or :raw. The no color option with, well, no color outputting the message with the current log format and the raw format will output just the raw message with no format or color applied.

In addition trace inclusive and exclusive regexp filtering is available to eliminate spurious code removing it from the stack trace. By default the typhonjs-color-logger and backbone-esnext-events is excluded from trace results. Additionally the following events from typhonjs-plugin-manager are handled to automatically add and remove trace filters from plugins added & removed via the event bindings: typhonjs:plugin:manager:added:plugin, typhonjs:plugin:manager:plugin:changed:eventbus, and typhonjs:plugin:manager:removed:plugin. To skip auto filter registration for a particular plugin set logAutoFilter to false in the associated plugins options.

See:

Example:

import logger from 'typhonjs-color-logger';

// simple usage
logger.error('An error occurred!');
import PluginManager    from 'typhonjs-plugin-manager';
import eventbus         from 'backbone-esnext-eventbus';

const pluginManager = new PluginManager({ eventbus });

// This will automatically wire up typhonjs-color-logger to the eventbus.
pluginManager.add({ name: typhonjs-color-logger });

// simple usage
eventbus.trigger('log:error', 'An error occurred!');

Constructor Summary

Public Constructor
public

Instantiates ColorLogger allowing optional options to be set.

Member Summary

Private Members
private

Stores all exclusive trace filters.

private

Stores all inclusive trace filters.

private
private

Stores ColorLogger options.

Method Summary

Public Methods
public

Adds a new trace filter.

public

Initializes multiple trace filters in a single call.

public

debug(msg: ...*): string

Display debug (blue) log.

public

debugCompact(msg: ...*): string

Display debug (blue) log; objects compacted.

public

debugNoColor(msg: ...*): string

Display debug log.

public

debugRaw(msg: ...*): string

Display raw debug log (no style / no color).

public

error(msg: ...*): string

Display error(red) log.

public

errorCompact(msg: ...*): string

Display error(red) log; objects compacted.

public

errorNoColor(msg: ...*): string

Display error log.

public

errorRaw(msg: ...*): string

Display raw error log (no style / no color).

public

fatal(msg: ...*): string

Display fatal (light red) log.

public

fatalCompact(msg: ...*): string

Display fatal (light red) log; objects compacted.

public

fatalNoColor(msg: ...*): string

Display fatal log.

public

fatalRaw(msg: ...*): string

Display raw fatal log (no style / no color).

public

Gets the filter data for a trace filter by name.

public

Gets the filter data for a trace filter by name.

public

Gets a trace filter enabled state.

public

Get the log level

public

Returns a copy of the logger options.

public

info(msg: ...*): string

Display info (green) log.

public

infoCompact(msg: ...*): string

Display info (green) log; objects compacted.

public

infoNoColor(msg: ...*): string

Display info log.

public

infoRaw(msg: ...*): string

Display raw info log (no style / no color).

public

Returns whether the given log level is enabled.

public

Returns true if the given level is a valid log level.

public

Removes all trace filters.

public

Removes a trace filter by name

public

setFilterEnabled(type: string, name: string, enabled: boolean): boolean

Sets a trace filters enabled state.

public

Sets the current log level.

public

Set optional parameters.

public

trace(msg: ...*): string

Display trace (purple) log.

public

traceCompact(msg: ...*): string

Display trace (purple) log; objects compacted.

public

traceNoColor(msg: ...*): string

Display trace log.

public

traceRaw(msg: ...*): string

Display raw trace log (no style / no color).

public

verbose(msg: ...*): string

Display verbose (purple) log.

public

verboseCompact(msg: ...*): string

Display verbose (purple) log; objects compacted.

public

verboseNoColor(msg: ...*): string

Display verbose log.

public

verboseRaw(msg: ...*): string

Display raw verbose log (no style / no color).

public

warn(msg: ...*): string

Display warning (yellow) log.

public

warnCompact(msg: ...*): string

Display warning (yellow) log; objects compacted.

public

warnNoColor(msg: ...*): string

Display warning log.

public

warnRaw(msg: ...*): string

Display raw warn log (no style / no color).

Private Methods
private

Applies any exclusive then inclusive filters against a given value.

private

_output(level: string, compact: boolean, nocolor: boolean, raw: boolean, msg: ...*): string | undefined

Display log message.

private

getTraceInfo(error: Error, isTrace: boolean): {info: string, trace: String[]}

Generates log information from where the logger invocation originated.

Public Constructors

public constructor(options: ColorLoggerOptions) source

Instantiates ColorLogger allowing optional options to be set.

Params:

NameTypeAttributeDescription
options ColorLoggerOptions
  • optional

Optional ColorLoggerOptions to set.

Private Members

private _exclusiveTraceFilters: Map<string, TraceFilter> source

Stores all exclusive trace filters.

private _inclusiveTraceFilters: Map<string, TraceFilter> source

Stores all inclusive trace filters.

private _logLevel: * source

private _options: ColorLoggerOptions source

Stores ColorLogger options.

Public Methods

public addFilter(config: TraceFilterData): boolean source

Adds a new trace filter.

Params:

NameTypeAttributeDescription
config TraceFilterData

The filter config to add.

Return:

boolean

True if the filter was added.

public addFilters(filterConfigs: Array<TraceFilterData>): boolean source

Initializes multiple trace filters in a single call.

Params:

NameTypeAttributeDescription
filterConfigs Array<TraceFilterData>

An array of filter config object hash entries.

Return:

boolean

If true all filters were added successfully.

public debug(msg: ...*): string source

Display debug (blue) log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public debugCompact(msg: ...*): string source

Display debug (blue) log; objects compacted.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public debugNoColor(msg: ...*): string source

Display debug log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public debugRaw(msg: ...*): string source

Display raw debug log (no style / no color).

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public error(msg: ...*): string source

Display error(red) log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public errorCompact(msg: ...*): string source

Display error(red) log; objects compacted.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public errorNoColor(msg: ...*): string source

Display error log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public errorRaw(msg: ...*): string source

Display raw error log (no style / no color).

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public fatal(msg: ...*): string source

Display fatal (light red) log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public fatalCompact(msg: ...*): string source

Display fatal (light red) log; objects compacted.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public fatalNoColor(msg: ...*): string source

Display fatal log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public fatalRaw(msg: ...*): string source

Display raw fatal log (no style / no color).

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public getAllFilterData(enabled: boolean | undefined): Array<TraceFilterData> source

Gets the filter data for a trace filter by name.

Params:

NameTypeAttributeDescription
enabled boolean | undefined

If enabled is a boolean it will return filters given their enabled state.

public getFilterData(type: string, name: string): TraceFilterData | undefined source

Gets the filter data for a trace filter by name.

Params:

NameTypeAttributeDescription
type string

The type of filter; must be 'exclusive' or 'inclusive'.

name string

The name of the filter.

public getFilterEnabled(type: string, name: string): boolean source

Gets a trace filter enabled state.

Params:

NameTypeAttributeDescription
type string

The type of filter; must be 'exclusive' or 'inclusive'.

name string

The name of the filter.

Return:

boolean

True if the filter enabled state was modified.

public getLogLevel(): * source

Get the log level

Return:

*

public getOptions(): ColorLoggerOptions source

Returns a copy of the logger options.

Return:

ColorLoggerOptions

Logger options.

public info(msg: ...*): string source

Display info (green) log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public infoCompact(msg: ...*): string source

Display info (green) log; objects compacted.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public infoNoColor(msg: ...*): string source

Display info log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public infoRaw(msg: ...*): string source

Display raw info log (no style / no color).

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public isLevelEnabled(level: string): boolean source

Returns whether the given log level is enabled.

Params:

NameTypeAttributeDescription
level string

log level

Return:

boolean

public isValidLogLevel(level: string): boolean source

Returns true if the given level is a valid log level.

Params:

NameTypeAttributeDescription
level string

The log level string to test.

Return:

boolean

public removeAllFilters() source

Removes all trace filters.

public removeFilter(type: string, name: string): boolean source

Removes a trace filter by name

Params:

NameTypeAttributeDescription
type string

The type of filter; must be 'exclusive' or 'inclusive'.

name string

The name of the filter.

Return:

boolean

True if the filter was removed.

public setFilterEnabled(type: string, name: string, enabled: boolean): boolean source

Sets a trace filters enabled state.

Params:

NameTypeAttributeDescription
type string

The type of filter; must be 'exclusive' or 'inclusive'.

name string

The name of the filter.

enabled boolean

The new enabled state.

Return:

boolean

True if the filter enabled state was modified.

public setLogLevel(level: string): boolean source

Sets the current log level.

Params:

NameTypeAttributeDescription
level string

log level

Return:

boolean

public setOptions(options: ColorLoggerOptions) source

Set optional parameters.

Params:

NameTypeAttributeDescription
options ColorLoggerOptions

Defines optional parameters to set.

public trace(msg: ...*): string source

Display trace (purple) log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public traceCompact(msg: ...*): string source

Display trace (purple) log; objects compacted.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public traceNoColor(msg: ...*): string source

Display trace log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public traceRaw(msg: ...*): string source

Display raw trace log (no style / no color).

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public verbose(msg: ...*): string source

Display verbose (purple) log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public verboseCompact(msg: ...*): string source

Display verbose (purple) log; objects compacted.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public verboseNoColor(msg: ...*): string source

Display verbose log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public verboseRaw(msg: ...*): string source

Display raw verbose log (no style / no color).

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public warn(msg: ...*): string source

Display warning (yellow) log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public warnCompact(msg: ...*): string source

Display warning (yellow) log; objects compacted.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public warnNoColor(msg: ...*): string source

Display warning log.

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

public warnRaw(msg: ...*): string source

Display raw warn log (no style / no color).

Params:

NameTypeAttributeDescription
msg ...*

log message.

Return:

string

formatted log message.

Private Methods

private _applyFilters(value: string): boolean source

Applies any exclusive then inclusive filters against a given value.

Params:

NameTypeAttributeDescription
value string

A value to test against all filters.

Return:

boolean

If true then the value matched a filter.

private _output(level: string, compact: boolean, nocolor: boolean, raw: boolean, msg: ...*): string | undefined source

Display log message.

Params:

NameTypeAttributeDescription
level string

log level: fatal, error, warn, info, debug, verbose, trace.

compact boolean
  • optional
  • default: false

If true then all JSON object conversion is compacted.

nocolor boolean
  • optional
  • default: false

If true then no color is applied.

raw boolean
  • optional
  • default: false

If true then just the raw message is logged at the given level.

msg ...*

log message.

Return:

string | undefined

formatted log message or undefined if log level is not enabled.

private getTraceInfo(error: Error, isTrace: boolean): {info: string, trace: String[]} source

Generates log information from where the logger invocation originated.

Params:

NameTypeAttributeDescription
error Error

An optional Error to trace instead of artificially generating one.

isTrace boolean
  • optional
  • default: false

If true then process remaining trace.

Return:

{info: string, trace: String[]}

info: file name and line number; trace: remaining stack trace if enabled.