Bitnacle
Bitnacle
is a dead simple logger module.
Installation
npm i bitnacle
Quick start
const Bitnacle = ;const logger = ; // Uses default "simple" format logger; logger;
They both produce the same output:
[2019-08-25T15:49:47:928+0200] [DEBUG] [This is a debug message]
Formats
Bitnacle
uses 2 formats:
simple
: is the default format and logs plain text log messagesjson
: outputs json-stringified like log messages.
Bitnacle
creates log messages with the following structure:
[:time] [:level] [:message]
To use the json
format:
const jsonLogger = format: 'json' ; jsonLogger; jsonLogger;
Loggin extra info
If you want to add more info to your loggs you can do it by passing an extraInfo
object to Bitnacle
.
IMPORTANT: if you create a nested object it won't be logged if using
simple
format, butjson
format will do it for you.
const extra = someKey: 'someValue' anotherKey: 'anotherValue' yetAnotherKey: 'yetAnotherValue' someObjectProp: someKeyInsideObjectProp: 'someValueInsideObjectProp' ; const extraInfo = extra ;logger;
Outputs for simple
and json
formats, note the nested object is not being logged with simple
format:
[2019-08-26T18:07:07:226+0200] [DEBUG] [someValue] [anotherValue] [yetAnotherValue] [This is a debug message]
Usage with express
If you are using Bitnacle
with Express
, you can pass the request object to Bitnacle
for even more extra info. This is really usefull for debug purposes, since you can have Bitnacle
and bitnacle-express working together identifying your requests easily.
Note that
Bitnacle
is compatible with request-ip and express-request-id and it will log theclientIp
andid
props if they are present on therequest
object.
This is the log message structure:
[:time] [:level] [:method] [:endpoint] [:remoteAddress] [:requestId] [:message]
app;
Outputs for simple
and json
formats:
[2019-08-25T16:07:52:686+0200] [DEBUG] [GET] [/] [::1] [cd657929-e0da-4f9b-ad92-d6a4551a7636] [This is a debug message]
More extra fun
It's not over yet!
Of course you can pass both extra
and req
objects in extraInfo
and Bitnacle
will log it, now you have tons of info! :D
Wikipedia who?
The log structure if using extra
and req
objects together would be:
[:time] [:level] [:method] [:endpoint] [:remoteAddress] [:requestId] [someValue] [anotherValue] [yetAnotherValue] [:message]
Outputs for simple
and json
formats:
[2019-08-26T18:18:50:817+0200] [DEBUG] [GET] [/] [::1] [9c9d856c-1684-41c0-893d-3d047e80a01c] [someValue] [anotherValue] [yetAnotherValue] [This is a debug message]
Just don't get too crazy logging whatever comes to your mind!
Log level API
Bitnacle
exposes a predefined set of level functions you can use directly.
- Default levels for
Bitnacle
are the following:- ERROR
- WARNING
- INFO
- DEBUG
const logger = format: 'json' // optional: default is "simple"; logger;logger;logger;logger;
If you want to use your own levels, you can specify the level using logger.log
.
Of course you can pass the extraInfo
object to logger.log
for you daily dose of info!
app;
Log to stream files
In order to log to files, you must create streams and pass them to bitnacle. You can add as many streams as you want:
const writableStream = fs; const logger = streams: writableStream ;