laabr
/laabr.plugin
laabr.token(<string> name, <Function> callback)
laabr.preset(<string> key, <string|false> preset)
laabr.format(<string> event, <string|false> format)
-
tokens:
Object
Optional. Default:{}
It's likelaabr.token(<string> name, <Function> callback)
. Usename
/callback
as key-value pairs. -
presets:
Object
Optional. Default:{}
It's likelaabr.preset(<string> key, <string|false> preset)
. Usekey
/preset
as key-value pairs. -
formats:
Object
Optional. Default:{}
It's likelaabr.format(<string> event, <string|false> format)
. Useevent
/format
as key-value pairs. -
colored:
boolean
Optional. Default:false
Partially colorizes token outputs with ANSI powered by chalk ⇗. -
indent:
string | number
Optional. Default:2
Take a look at thespace
argument of JSON.stringify ⇗. This setting is just relevant for format strings marked as JSON. -
override:
boolean
Optional. Default:false
Override severalconsole
⇗ logging methods with corresponding boundserver.log
⇗ functions to enable logging everywhere. Keep theoptions.pino.level
in mind which is set toinfo
by default. -
preformatter:
Function <[data[, options]]>
Optional. Default:(data) => data
Preformat the originally logged message before getting processed bylaabr
. The function is passed the JSON object and the options as arguments and have to return an object as well. The plugin evaluates the type of the logged message just before – so it is not possible to fake an event. But have in mind that the token's return value could be affected. -
postformatter:
Function <[data[, options]]>
Optional. Default:(data) => data
Preformat the logged message after getting processed bylaabr
. The function is passed the processed string and the options as arguments and have to return a string as well. -
handleUncaught:
boolean
Optional. Default:false
If uncaught exception should be logged. Overrides the default behavior ofEvent: 'uncaughtException'
⇗ but exits the process.
Hint: If enabled it's possible that errors thrown during the processing of the plugin and its underlying plugins get hidden, e.g. in
options.preformatter
/options.postformatter
. So if the process exits without any reason, disable this option for debugging.
-
stream:
Writable
Optional. Default:process.stdout
Take a look at thestream
argument of pino ⇗. -
pino:
Object
Optional. Default:{}
pino ⇗ related options.prettyPrint
,timestamp
andbrowser
are effectless. The created instance is passed to hapi-pino ⇗. -
hapiPino:
Object
Optional. Default:{}
hapi-pino ⇗ related options.prettyPrint
andinstance
are effectless. Useoptions.pino
to configre the passedinstance
.
To define a token, simply invoke laabr.token()
with the name and a callback function.
Best Practise: Run laabr.token
before registering the plugin.
callback(<Object> data, <Object> colors)
The callback function is expected to be called with the arguments data
and colors
. Those represent the logged data and an object containing respective chalk ⇗ functions. Additionally, the token can accept further arguments of it's choosing to customize behavior. The colors
object contains the following key: level
, status
and dim
. Those represent chalk color functions related to the context, the log level and the request status code. This callback function is expected to return a string value. The value returned is then available as :hello
in this case below:
laabr.token('hello', () => 'hello!');
Hint: disable the respective format, get the complete logged message and inspect the properties. With this it is quite easy to define custom tokens.
To define own format presets, simply invoke laabr.preset()
with an unique key and a format string. Use your own or provided presets for an easy reuse and exchange by passing the key to laabr.format()
instead of the format string itself.
Best Practise: Run laabr.preset
before registering the plugin.
laabr.preset('server.env', ':time :environment :host :host[port]');
To define a format, simply invoke laabr.format()
with the event and a format string. Use existing tokens with :<token>
within the format string.
If the format
is set to false
, it logs the whole json message without any pretty-printed format.
Best Practise: Run laabr.format
before registering the plugin.
laabr.format('onPostStart', ':time :hello world!');
Furthermore it is possible to define JSON strings. Therefor enclose the template string with {
& }
and use an object-like structure:
laabr.format('onPostStart', '{ ts::time, msg::hello }');
Hint: If you work with the JSON/object-like structure above and you want to work with custom strings, enclose them in quotes/backticks. If you want to concatinate tokens with custom string use a combination of +
and quotes/backticks – use quotes for invalid object keys. To run inline function/methods use braces.
laabr.format('response', '{ responseTime: { value::responseTime, unit:`ms` }}');
laabr.format('response', '{ responseTime::responseTime + `ms` }');
laabr.format('response', '{ "response-time"::responseTime }');
laabr.format('response', '{ message:(JSON.stringify(:message)) }');
Or use a format preset key instead of a format string:
laabr.format('onPostStart', 'server.env');
The event
is allowed to be onPostStart
, onPostStop
, response
, request-error
, log
and uncaught
. The events are almost analog to the hapi-pino ⇗ ones.