1 # @squeep/logger-json-console
3 A simple logger class, which mostly just structures messages, data, and some metadata as JSON and spews it, by default, to Console.
5 Supports basic scrubbing of well-defined sensitive fields, detection of circular references, and an extensible set of JSON replacer functions for common objects.
7 If provided with an asyncLocalStorage instance, it expects a stored object which will be spread over the resulting logged JSON.
9 Intended to be specific to Squeep Framework Applications, this module has some opinions.
13 - `new Logger(options, commonObject, asyncLocalStorage, backend)`
14 - `commonObject` will be merged into every log
15 - `asyncLocalStorage`, if provided, should store an object, which will also be merged into log
16 - `backend` is `console` by default, but may be anything implementing the same log-level functions
18 - `error(scope, message, data, ...)`
19 - `scope` - identifies source of message, e.g. 'class:method'
20 - `message` - text to be logged
21 - `data` - object to be logged, can be scrubbed of sensitive fields, and will be serialized with provided replacers
22 - any additional arguments are included as an array
23 - `warn(scope, message, data, ...)`
24 - `info(scope, message, data, ...)`
25 - `log(scope, message, data, ...)`
26 - `debug(scope, message, data, ...)`
30 Includes replacers for these objects:
34 - http.IncomingMessage
35 - http.OutgoingMessage
38 Additional replacers may be inserted into the logger instance's `jsonReplacers` array.
42 Sanitizers may be inserted into the logger instance's `dataSanitizers` array.
47 const http = require('node:http');
48 const { AsyncLocalStorage } = require('node:async_hooks');
49 const uuid = require('uuid');
50 const Logger = require('@squeep/logger-json-console');
52 const loggerOptions = {
53 ignoreBelowLevel: 'info',
55 const commonObject = {
56 nodeIdentifier: uuid.v1(),
58 const asyncLocalStorage = new AsyncLocalStorage();
59 const logger = new Logger(loggerOptions, commonObject, asyncLocalStorage);
61 const scope = 'exampleServer';
62 http.createServer((req, res) => {
63 asyncLocalStorage.run({ requestId: uuid.v1() }, () => {
64 logger.debug(scope, 'start', { req, res });
67 logger.info(scope, 'finish', { req, res }, 'other stuff');
73 Results in messages such as this:
77 "nodeIdentifier": "64610280-fc17-11ed-a918-1dd19f027d43",
78 "timestamp": "2023-05-26T22:48:17.995Z",
79 "timestampMs": 1685141297995,
81 "scope": "exampleServer",
89 "host": "localhost:8088",
90 "user-agent": "curl/7.83.1",
97 "statusMessage": "OK",
104 "requestId": "68233aa0-fc17-11ed-a918-1dd19f027d43"