Log

A thin & fast logger system.

An instance is created by the app and available to use from anywhere inside your code.

Shortcuts to log's methods are also available on every Bajo plugin. Call on these shortcuts will be prefixed with it's plugin name automatically.

Example:

// ... anywhere inside your code
this.app.log.debug(...)
// or inside a plugin
if (!isValid) this.log.error('Invalid value!') // will be prefixed with plugin namespace automatically

Constructor

new Log(app)

Constructor.

Parameters:
NameTypeDescription
appApp

App instance

Members

_lastDelta :number

Last delta time in millisecond since app started. Used for log's time taken feature.

Type:
  • number

app :App

The app instance.

Type:

Methods

debug(prefix, …params)

Display & format message in debug level. See formatMsg for details.

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

paramsany<repeatable>

Parameters

(async) dispose() → {Promise.<void>}

Dispose internal references.

Returns:
Type: 
Promise.<void>

error(prefix, …params)

Display & format message in error level. See formatMsg for details.

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

paramsany<repeatable>

Parameters

fatal(prefix, …params)

Display & format message in fatal level. See formatMsg for details.

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

paramsany<repeatable>

Parameters

formatMsg(level, prefix, …params)

Display & format message according to one of these rules:

  1. level prefix text var 1 var 2 ...var n - Translate text and interpolate with vars for level level
  2. level prefix data text var 1 var 2 ...var n - As above, and append stringified data
  3. level prefix error - Format as Err object. If current log level is trace, dump it on screen

In prod environment, log will be delivered as JSON stringified object. See Log.TJsonOutput for more info

Parameters:
NameTypeAttributesDescription
levelstring

Log level to use

prefixstring

Prefix to the message

paramsany<repeatable>

See format above

getErrorMessage(error) → {string}

Get error message from an Error object. If the message is empty, return the code or statusCode instead.

Parameters:
NameTypeDescription
errorError

Error object

Returns:

Error message

Type: 
string

getRotationPattern(isPrev) → {string}

Calculate pattern used for log rotation. Used by save method to determine the log file name.

Parameters:
NameTypeDescription
isPrevboolean

If true, calculate previous rotation pattern.

Returns:

Calculated pattern

Type: 
string

info(prefix, …params)

Display & format message in info level. See formatMsg for details.

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

paramsany<repeatable>

Parameters

save(text, prefixopt)

Save log to file in {dataDir}/log.

Parameters:
NameTypeAttributesDefaultDescription
textstring

Log message to save

prefixstring<optional>
'bajo'

Use prefix as basename. Defaults to bajo

silent(prefix, …params)

Display & format message in silent level. See formatMsg for details.

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

paramsany<repeatable>

Parameters

trace(prefix, …params)

Display & format message in trace level. See formatMsg for details.

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

paramsany<repeatable>

Parameters

warn(prefix, …params)

Display & format message in warn level. See formatMsg for details.

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

paramsany<repeatable>

Parameters

Type Definitions

TJsonOutput

Log output in stringified JSON format. Returned when app run in prod environment.

Properties
NameTypeAttributesDescription
prefixstring

Message prefix.

messagestring

The message itself.

levelstring

Log level.

timenumber

Time in millisecond.

pidnumber

Process ID.

hostnamestring

Hostname.

dataObject<optional>

Payload data, if any.

TLevels

Type:
  • Object
Properties
NameTypeDescription
traceObject
Properties
NameTypeAttributesDefaultDescription
numbernumber<optional>
10
colorstring<optional>
gray
debugObject
Properties
NameTypeAttributesDefaultDescription
numbernumber<optional>
20
colorstring<optional>
greenBright
infoObject
Properties
NameTypeAttributesDefaultDescription
numbernumber<optional>
30
colorstring<optional>
blueBright
warnObject
Properties
NameTypeAttributesDefaultDescription
numbernumber<optional>
40
colorstring<optional>
yellowBright
errorObject
Properties
NameTypeAttributesDefaultDescription
numbernumber<optional>
50
colorstring<optional>
redBright
fatalObject
Properties
NameTypeAttributesDefaultDescription
numbernumber<optional>
60
colorstring<optional>
magentaBright
silentObject
Properties
NameTypeAttributesDefaultDescription
numbernumber<optional>
99
colorstring<optional>
white