@bonniernews/exp-logger
Advanced tools
Comparing version 1.0.0 to 2.0.0
72
index.js
"use strict"; | ||
const pino = require("pino"); | ||
const config = require("exp-config"); | ||
const { getId } = require("exp-correlator"); | ||
let severityLabelsMap = null; | ||
function severity(label) { | ||
// In case of new/different labels it can be added through config | ||
if (config.logging?.severityLabels?.length > 0) { | ||
for (const object of config.severityLabels) { | ||
if (object.label === label) { | ||
return object.newLabel; | ||
} | ||
} | ||
} | ||
if (severityLabelsMap && severityLabelsMap.has(label)) | ||
return severityLabelsMap.get(label); | ||
@@ -35,30 +28,39 @@ switch (label) { | ||
const testLogLocation = | ||
config.envName === "test" | ||
? process.env.LOG_LOCATION || config.logging?.testLog || "./logs/test.log" | ||
: 1; | ||
const shouldPrettyPrint = ["development", "test"].includes(config.envName); | ||
const logger = pino( | ||
{ | ||
level: process.env.LOG_LEVEL || config.logLevel || "info", | ||
formatters: { | ||
level(label) { | ||
return { level: label, severity: severity(label) }; | ||
function init({ | ||
logLevel = "info", | ||
mixin, | ||
shouldPrettyPrint = false, | ||
severityLabels = [], | ||
logLocation = "./logs/test.log", | ||
setDestination = false, | ||
}) { | ||
if (!severityLabelsMap) | ||
severityLabelsMap = new Map( | ||
severityLabels.map((o) => [o.label, o.newLabel]) | ||
); | ||
return pino( | ||
{ | ||
level: logLevel, | ||
formatters: { | ||
level(label) { | ||
return { level: label, severity: severity(label) }; | ||
}, | ||
}, | ||
}, | ||
timestamp: () => `, "time": "${new Date().toISOString()}"`, | ||
transport: { | ||
target: "pino-pretty", | ||
options: shouldPrettyPrint && { | ||
destination: testLogLocation, | ||
colorize: shouldPrettyPrint && config.envName !== "test", | ||
ignore: "pid,hostname", | ||
timestamp: () => `, "time": "${new Date().toISOString()}"`, | ||
transport: { | ||
target: "pino-pretty", | ||
options: shouldPrettyPrint && { | ||
destination: setDestination ? logLocation : 1, | ||
colorize: setDestination === false, | ||
ignore: "pid,hostname", | ||
}, | ||
}, | ||
messageKey: "message", | ||
mixin, | ||
}, | ||
messageKey: "message", | ||
mixin: () => ({ correlationId: getId() }), | ||
}, | ||
config.envName === "test" && pino.destination(testLogLocation) | ||
); | ||
setDestination && pino.destination(logLocation) | ||
); | ||
} | ||
module.exports = logger; | ||
module.exports = init; |
{ | ||
"name": "@bonniernews/exp-logger", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Simple bootstrapping of pino logger", | ||
@@ -15,7 +15,3 @@ "main": "index.js", | ||
"pino-pretty": "^9.1.1" | ||
}, | ||
"peerDependencies": { | ||
"exp-config": "^4.x", | ||
"exp-correlator": "^1.x" | ||
} | ||
} |
# exp-logger | ||
Simple logger package to unify how logging is done with (configurable) remapping | ||
Simple logger package to unify how logging is done with (configurable) remapping | ||
[NPM](https://www.npmjs.com/package/@bonniernews/exp-logger) | ||
___ | ||
@@ -13,31 +14,28 @@ ## defaults | ||
* fatal -> CRITICAL | ||
___ | ||
## optional | ||
if you want to add more logging / platform specific labels you can configure it by adding it to a config file | ||
```json | ||
{ | ||
... | ||
"logging": { | ||
"severityLabels": [ | ||
{ | ||
"label": "foo", | ||
"newLabel": "bar" | ||
} | ||
] | ||
} | ||
... | ||
} | ||
## options | ||
```js | ||
logLevel // defaults to info | ||
mixin // pino mixins, passed down to pino as is | ||
shouldPrettyPrint // defaults to false | ||
severityLabels // replace or add label mappings, [ {label: "oldKey", newLabel: "newKey"}] | ||
logLocation // location of the test log | ||
setDestination // default is false, set this to true to log to logLocation | ||
``` | ||
## example | ||
```js | ||
change the location of the test log | ||
default is `./logs/test.log` | ||
You can change the location with either a config or env variable. | ||
ENV: `LOG_LOCATION=./foo/bar/biz.log` | ||
config: | ||
```json | ||
"logging": { | ||
"testLog": "./foo/bar/biz.log" | ||
} | ||
const logger = require('@bonniernews/exp-logger')({ | ||
shouldPrettyPrint: process.NODE_ENV === "development", | ||
severityLabels: [ {label: "info", newLabel: "foo" }] | ||
}) | ||
logger.info("Hello world") | ||
/* | ||
-- expected output | ||
[15:19:13.240] INFO: | ||
severity: "foo" | ||
message: "Hello world" | ||
*/ | ||
``` |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
2928
2
5
63
1
40