bitnacle-express
bitnacle-express
is a dead simple middleware logger to use with Express apps.
It is compatible with request-ip and express-request-id, if you are using them make sure to use bitnacle-express
after them.
Installation
npm i bitnacle-express
Quick start
const app = ;const bitnaclExpress = ; app; // use default "simple" format
Usage
-
Log incoming HTTP requests using
logger
:You can specify 3 different formats:
simple
(default),json
andextended
app;These are the outputs for the 3 different formats,
simple
,json
andextended
respectively:[2020-05-28T00:53:13:658+0200] [INFO] [GET] [/] [::1] [Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36] [cd820302-740b-4f30-8a68-a4348c68bdd9] [304] [9ms]
time: '2019-08-25T17:13:52:079+0200'level: 'INFO'hostname: 'localhost'req:method: 'GET'endpoint: '/'headers:host: 'localhost:3100'connection: 'keep-alive''cache-control': 'max-age=0''upgrade-insecure-requests': '1''user-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36''sec-fetch-mode': 'navigate''sec-fetch-user': '?1'accept:'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3''sec-fetch-site': 'none''accept-encoding': 'gzip, deflate, br''accept-language': 'es-ES,es;q=0.9,en;q=0.8'cookie: 'io=ohrfY8fxk-ZqSXbYAAAC''if-none-match': 'W/"424-gtDsLN/eQBxV76fISNK6wSdFMmY"'remoteAddress: '::1'params: 0query: 0id: '9ff20b68-f46d-4eb5-9ef3-9cb077de1677'statusCode: 200elapsedTime: '2ms'IMPORTANT: As noted before, if you are using request-ip and/or express-request-id in your app, you must use
bitnacle-express
after them:const app = ;...appappapp -
Log errors using
errorLogger
The
errorLogger
only supports thesimple
andjson
formats. Your app must use it after the routes declaration.const router = ;app;app;IMPORTANT:
errorLogger
doesn't handle the errors/exceptions, it only logs them, so place it before your error handlers.I personally recommend to
try/catch
your routes and handle errors locally unless you have a general handler for some or all of your routes. You can use Bitnacle logger along withbitnacle-express
to log your errors on thecatch
block of your routes.
Log levels
bitnacle-express.logger
will use predefined log levels based on the response.statusCode
.
statusCode
>= 500 will use[ERROR]
levelstatusCode
>= 400 will use[WARNING]
levelstatusCode
>= 100 will use[INFO]
level
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 router = ; const writableStream = fs; app; app;