Log

A thin & fast logger system.

You typically don't need to create an instance of this class, since an instance is already 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

Reference to the app instance

Type:

logDir :string

Directory to save log files. Defaults to {dataDir}/log. If log saving is on (see app.config.log.save) and directory does not exist, this directory will be created automatically.

Type:
  • string

Methods

debug(prefix, …args)

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

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

args*<repeatable>

Arguments to be passed to the message. See formatMsg for details.

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

Dispose internal references.

Returns:
Type: 
Promise.<void>

error(prefix, …args)

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

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

args*<repeatable>

Arguments to be passed to the message. See formatMsg for details.

fatal(prefix, …args)

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

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

args*<repeatable>

Arguments to be passed to the message. See formatMsg for details.

formatMsg(level, prefix, …params)

Format & display log message according to the current log level with the following syntax:

  1. level prefix text param 1 param 2 ...param n - Translate text and interpolate with params for level level
  2. level prefix {data} text param 1 param 2 ...param n - Same as above, but with additional stringified data object to be logged
  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

params*<repeatable>

See format above

getErrorMessage(error) → {string}

Get error message from an Error object. If the error message is empty, return the error code or status code 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. Rotation pattern is based on the rotation.cycle configuration. See app.config.log.rotation.cycle for more info.

Parameters:
NameTypeDescription
isPrevboolean

If true, calculate previous rotation pattern.

Returns:

Calculated pattern

Type: 
string

info(prefix, …args)

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

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

args*<repeatable>

Arguments to be passed to the message. See formatMsg for details.

save(text, prefixopt)

Save log to file in logDir.

Parameters:
NameTypeAttributesDefaultDescription
textstring

Log message to save

prefixstring<optional>
'bajo'

Use prefix as basename. Defaults to bajo

silent(prefix, …args)

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

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

args*<repeatable>

Arguments to be passed to the message. See formatMsg for details.

trace(prefix, …args)

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

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

args*<repeatable>

Argumets to be passed to the message. See formatMsg for details.

warn(prefix, …args)

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

Parameters:
NameTypeAttributesDescription
prefixstring

Message prefix

args*<repeatable>

Arguments to be passed to the message. See formatMsg for details.

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