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, 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, taking precedence.
9 Intended to be specific to Squeep Framework Applications, this module has opinions.
13 Expects these arguments in any log call:
15 - `scope` - e.g. file:method
18 - any additional arguments are included as an array, but not scrubbed
22 Includes replacers for these objects:
26 - http.IncomingMessage
27 - http.OutgoingMessage
30 Custom replacers may be inserted into the logger instance's `jsonReplacers` array.
34 Custom sanitizers may be inserted into the logger instance's `dataSanitizers` array.
39 const http = require('node:http');
40 const { AsyncLocalStorage } = require('node:async_hooks');
41 const uuid = require('uuid');
42 const Logger = require('@squeep/logger-json-console');
44 const loggerOptions = {
45 ignoreBelowLevel: 'info',
47 const commonObject = {
48 nodeIdentifier: uuid.v1(),
50 const asyncLocalStorage = new AsyncLocalStorage();
51 const logger = new Logger(loggerOptions, commonObject, asyncLocalStorage);
53 const scope = 'exampleServer';
54 http.createServer((req, res) => {
55 asyncLocalStorage.run({ requestId: uuid.v1() }, () => {
56 logger.debug(scope, 'start', { req, res });
59 logger.info(scope, 'finish', { req, res }, 'other stuff');
65 Results in messages such as this:
69 "nodeIdentifier": "64610280-fc17-11ed-a918-1dd19f027d43",
70 "timestamp": "2023-05-26T22:48:17.995Z",
71 "timestampMs": 1685141297995,
73 "scope": "exampleServer",
81 "host": "localhost:8088",
82 "user-agent": "curl/7.83.1",
89 "statusMessage": "OK",
96 "requestId": "68233aa0-fc17-11ed-a918-1dd19f027d43"