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 |
constructor(options: ColorLoggerOptions) 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 |
_logLevel: * |
|
private |
Stores ColorLogger options. |
Method Summary
Public Methods | ||
public |
addFilter(config: TraceFilterData): boolean Adds a new trace filter. |
|
public |
addFilters(filterConfigs: Array<TraceFilterData>): boolean Initializes multiple trace filters in a single call. |
|
public |
Display debug (blue) log. |
|
public |
debugCompact(msg: ...*): string Display debug (blue) log; objects compacted. |
|
public |
debugNoColor(msg: ...*): string Display debug log. |
|
public |
Display raw debug log (no style / no color). |
|
public |
Display error(red) log. |
|
public |
errorCompact(msg: ...*): string Display error(red) log; objects compacted. |
|
public |
errorNoColor(msg: ...*): string Display error log. |
|
public |
Display raw error log (no style / no color). |
|
public |
Display fatal (light red) log. |
|
public |
fatalCompact(msg: ...*): string Display fatal (light red) log; objects compacted. |
|
public |
fatalNoColor(msg: ...*): string Display fatal log. |
|
public |
Display raw fatal log (no style / no color). |
|
public |
getAllFilterData(enabled: boolean | undefined): Array<TraceFilterData> Gets the filter data for a trace filter by name. |
|
public |
getFilterData(type: string, name: string): TraceFilterData | undefined Gets the filter data for a trace filter by name. |
|
public |
getFilterEnabled(type: string, name: string): boolean Gets a trace filter enabled state. |
|
public |
getLogLevel(): * Get the log level |
|
public |
Returns a copy of the logger options. |
|
public |
Display info (green) log. |
|
public |
infoCompact(msg: ...*): string Display info (green) log; objects compacted. |
|
public |
infoNoColor(msg: ...*): string Display info log. |
|
public |
Display raw info log (no style / no color). |
|
public |
isLevelEnabled(level: string): boolean Returns whether the given log level is enabled. |
|
public |
isValidLogLevel(level: string): boolean Returns true if the given level is a valid log level. |
|
public |
Removes all trace filters. |
|
public |
removeFilter(type: string, name: string): boolean Removes a trace filter by name |
|
public |
setFilterEnabled(type: string, name: string, enabled: boolean): boolean Sets a trace filters enabled state. |
|
public |
setLogLevel(level: string): boolean Sets the current log level. |
|
public |
setOptions(options: ColorLoggerOptions) Set optional parameters. |
|
public |
Display trace (purple) log. |
|
public |
traceCompact(msg: ...*): string Display trace (purple) log; objects compacted. |
|
public |
traceNoColor(msg: ...*): string Display trace log. |
|
public |
Display raw trace log (no style / no color). |
|
public |
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 |
Display warning (yellow) log. |
|
public |
warnCompact(msg: ...*): string Display warning (yellow) log; objects compacted. |
|
public |
warnNoColor(msg: ...*): string Display warning log. |
|
public |
Display raw warn log (no style / no color). |
Private Methods | ||
private |
_applyFilters(value: string): boolean 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:
Name | Type | Attribute | Description |
options | ColorLoggerOptions |
|
Optional ColorLoggerOptions to set. |
Private Members
private _logLevel: * source
Public Methods
public addFilter(config: TraceFilterData): boolean source
Adds a new trace filter.
Params:
Name | Type | Attribute | Description |
config | TraceFilterData | The filter config to add. |
public addFilters(filterConfigs: Array<TraceFilterData>): boolean source
Initializes multiple trace filters in a single call.
Params:
Name | Type | Attribute | Description |
filterConfigs | Array<TraceFilterData> | An array of filter config object hash entries. |
public debug(msg: ...*): string source
Display debug (blue) log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public debugCompact(msg: ...*): string source
Display debug (blue) log; objects compacted.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public debugNoColor(msg: ...*): string source
Display debug log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public debugRaw(msg: ...*): string source
Display raw debug log (no style / no color).
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public error(msg: ...*): string source
Display error(red) log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public errorCompact(msg: ...*): string source
Display error(red) log; objects compacted.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public errorNoColor(msg: ...*): string source
Display error log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public errorRaw(msg: ...*): string source
Display raw error log (no style / no color).
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public fatal(msg: ...*): string source
Display fatal (light red) log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public fatalCompact(msg: ...*): string source
Display fatal (light red) log; objects compacted.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public fatalNoColor(msg: ...*): string source
Display fatal log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public fatalRaw(msg: ...*): string source
Display raw fatal log (no style / no color).
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public getAllFilterData(enabled: boolean | undefined): Array<TraceFilterData> source
Gets the filter data for a trace filter by name.
public getFilterData(type: string, name: string): TraceFilterData | undefined source
Gets the filter data for a trace filter by name.
public getFilterEnabled(type: string, name: string): boolean source
Gets a trace filter enabled state.
public info(msg: ...*): string source
Display info (green) log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public infoCompact(msg: ...*): string source
Display info (green) log; objects compacted.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public infoNoColor(msg: ...*): string source
Display info log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public infoRaw(msg: ...*): string source
Display raw info log (no style / no color).
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public isLevelEnabled(level: string): boolean source
Returns whether the given log level is enabled.
Params:
Name | Type | Attribute | Description |
level | string | log level |
public isValidLogLevel(level: string): boolean source
Returns true if the given level is a valid log level.
Params:
Name | Type | Attribute | Description |
level | string | The log level string to test. |
public setFilterEnabled(type: string, name: string, enabled: boolean): boolean source
Sets a trace filters enabled state.
public setLogLevel(level: string): boolean source
Sets the current log level.
Params:
Name | Type | Attribute | Description |
level | string | log level |
public setOptions(options: ColorLoggerOptions) source
Set optional parameters.
Params:
Name | Type | Attribute | Description |
options | ColorLoggerOptions | Defines optional parameters to set. |
public trace(msg: ...*): string source
Display trace (purple) log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public traceCompact(msg: ...*): string source
Display trace (purple) log; objects compacted.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public traceNoColor(msg: ...*): string source
Display trace log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public traceRaw(msg: ...*): string source
Display raw trace log (no style / no color).
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public verbose(msg: ...*): string source
Display verbose (purple) log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public verboseCompact(msg: ...*): string source
Display verbose (purple) log; objects compacted.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public verboseNoColor(msg: ...*): string source
Display verbose log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public verboseRaw(msg: ...*): string source
Display raw verbose log (no style / no color).
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public warn(msg: ...*): string source
Display warning (yellow) log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public warnCompact(msg: ...*): string source
Display warning (yellow) log; objects compacted.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public warnNoColor(msg: ...*): string source
Display warning log.
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
public warnRaw(msg: ...*): string source
Display raw warn log (no style / no color).
Params:
Name | Type | Attribute | Description |
msg | ...* | log message. |
Private Methods
private _applyFilters(value: string): boolean source
Applies any exclusive then inclusive filters against a given value.
Params:
Name | Type | Attribute | Description |
value | string | A value to test against all filters. |
private _output(level: string, compact: boolean, nocolor: boolean, raw: boolean, msg: ...*): string | undefined source
Display log message.
Params:
Name | Type | Attribute | Description |
level | string | log level: |
|
compact | boolean |
|
If true then all JSON object conversion is compacted. |
nocolor | boolean |
|
If true then no color is applied. |
raw | boolean |
|
If true then just the raw message is logged at the given level. |
msg | ...* | log message. |