Supports basic scrubbing of well-defined sensitive fields, and an extensible set of JSON replacer functions for common objects.
-If provided with an asyncLocalStorage instance, it expects a stored object which will be spread over the resulting logged json, taking precedence.
+If provided with an asyncLocalStorage instance, it expects a stored object which will be spread over the resulting logged JSON.
-Intended to be specific to Squeep Framework Applications, this module has opinions.
+Intended to be specific to Squeep Framework Applications, this module has some opinions.
## API
-Expects these arguments in any log call:
-
-- `scope` - e.g. file:method
-- `message` - text
-- `data` - object
-- any additional arguments are included as an array, but not scrubbed
+- `new Logger(options, commonObject, asyncLocalStorage, backend)`
+ - `commonObject` will be merged into every log
+ - `asyncLocalStorage`, if provided, should store an object, which will also be merged into log
+ - `backend` is `console` by default, but may be anything implementing the same log-level functions
+
+- `error(scope, message, data, ...)`
+ - `scope` - identifies source of message, e.g. 'class:method'
+ - `message` - text to be logged
+ - `data` - object to be logged, can be scrubbed of sensitive fields, and will be serialized with provided replacers
+ - any additional arguments are included as an array
+- `warn(scope, message, data, ...)`
+- `info(scope, message, data, ...)`
+- `log(scope, message, data, ...)`
+- `debug(scope, message, data, ...)`
## JSON Replacers
- http.OutgoingMessage
- http.ServerResponse
-Custom replacers may be inserted into the logger instance's `jsonReplacers` array.
+Additional replacers may be inserted into the logger instance's `jsonReplacers` array.
## Data Sanitizers
-Custom sanitizers may be inserted into the logger instance's `dataSanitizers` array.
+Sanitizers may be inserted into the logger instance's `dataSanitizers` array.
## Example
'use strict';
/**
- * Here are some sanitizers to replace data fields before logging.
- * Well, really, this just documents the expected format.
+ * Sanitizers replace data fields before logging.
+ * None are bundled, but here is an example of one.
*/
/**
- * template for sanitizers
+ * Example sanitizer function.
+ * Sanitizers are called initially with `sanitize` false, to only determine
+ * whether the data object will be changed. If so, the sanitizer is then
+ * called again with `sanitize` true, but provided with a clone of the
+ * original data to update, to avoid leaking any applied changes back to the
+ * application.
* @param {Object} data
* @param {Boolean} sanitize
- * @returns {Boolean}
+ * @returns {Boolean} whether sanitizer is applicable to data
*/
/* istanbul ignore next */
function _sanitizer(data, sanitize = true) {
let unclean = false;
- if (undefined) { // eslint-disable-line no-constant-condition
+ const sensitiveFieldLength = data?.sensitiveField?.length;
+ if (sensitiveFieldLength) {
unclean = true;
- }
- if (unclean && sanitize) {
- data;
+
+ if (sanitize) {
+ data.sensitiveField = '*'.repeat(sensitiveFieldLength);
+ }
}
return unclean;
"engines": {
"node": ">=14"
},
- "description": "A simple logger which spews JSON to stdout; intended for use in Squeep Framework Applications.",
+ "description": "A simple logger which spews formatted data and metadata as JSON to stdout; intended for use in Squeep Framework Applications.",
"main": "index.js",
"scripts": {
- "coverage": "nyc npm test",
+ "coverage": "nyc --clean npm test",
"coverage-check": "nyc check-coverage",
"eslint": "eslint *.js lib",
"test": "mocha --recursive"
},
"keywords": [
"json",
- "log"
+ "log",
+ "structured logging"
],
"author": "Justin Wind <jwind-npm@squeep.com>",
"license": "ISC",