@dotcom-reliability-kit/logger
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -43,2 +43,3 @@ export = Logger; | ||
transforms?: LogTransform[] | undefined; | ||
withPrettifier?: boolean | undefined; | ||
withTimestamps?: boolean | undefined; | ||
@@ -45,0 +46,0 @@ }; |
@@ -23,2 +23,4 @@ const pino = require('pino').default; | ||
* Transforms to apply to logs before sending. | ||
* @property {boolean} [withPrettifier = true] | ||
* Whether to prettify log output if it's possible. | ||
* @property {boolean} [withTimestamps = true] | ||
@@ -184,2 +186,8 @@ * Whether to send the timestamp that each log method was called. | ||
// Default and set the prettifier option | ||
const withPrettifier = | ||
typeof options.withPrettifier === 'boolean' | ||
? options.withPrettifier | ||
: !Boolean(process.env.LOG_DISABLE_PRETTIFIER); | ||
// Default and set the timestamps option. | ||
@@ -206,3 +214,3 @@ const withTimestamps = options.withTimestamps !== false; | ||
}; | ||
if (PRETTIFICATION_AVAILABLE) { | ||
if (withPrettifier && PRETTIFICATION_AVAILABLE) { | ||
pinoOptions.transport = { | ||
@@ -344,2 +352,10 @@ target: 'pino-pretty', | ||
if (sanitizedLogData.error && sanitizedLogData.error instanceof Error) { | ||
sanitizedLogData.error = serializeError(sanitizedLogData.error); | ||
} | ||
if (sanitizedLogData.err && sanitizedLogData.err instanceof Error) { | ||
sanitizedLogData.err = serializeError(sanitizedLogData.err); | ||
} | ||
// Transform the log data | ||
@@ -346,0 +362,0 @@ let transformedLogData = clone(sanitizedLogData, { |
{ | ||
"name": "@dotcom-reliability-kit/logger", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A simple and fast logger based on Pino, with FT preferences baked in", | ||
@@ -22,4 +22,12 @@ "repository": { | ||
"@ungap/structured-clone": "^1.0.2", | ||
"pino": "^8.11.0" | ||
"pino": "^8.12.0" | ||
}, | ||
"peerDependencies": { | ||
"pino-pretty": "^10.0.0" | ||
}, | ||
"peerDependencyMeta": { | ||
"pino-pretty": { | ||
"optional": true | ||
} | ||
}, | ||
"devDependencies": { | ||
@@ -26,0 +34,0 @@ "@financial-times/n-logger": "^10.3.0", |
@@ -13,2 +13,3 @@ | ||
* [`options.transforms`](#optionstransforms) | ||
* [`options.withPrettifier`](#optionswithprettifier) | ||
* [`options.withTimestamps`](#optionswithtimestamps) | ||
@@ -193,2 +194,10 @@ * [`logger.log()` and shortcut methods](#loggerlog-and-shortcut-methods) | ||
#### `options.withPrettifier` | ||
Whether to send prettified logs if available. This option has no effect if you have the `NODE_ENV` environment variable set to either `production` or if you have not installed [pino-pretty](https://github.com/pinojs/pino-pretty#readme). See [local development usage](#local-development-usage) for more information. | ||
Must be a `Boolean` and defaults to `true`. | ||
It's also possible to set this option as an environment variable, which is how you configure the default logger. Set the `LOG_DISABLE_PRETTIFIER` environment variable to `true` if you want to force the prettifier not to load. | ||
#### `options.withTimestamps` | ||
@@ -494,18 +503,38 @@ | ||
Errors found in sub-properties of the log data are _not_ serialized like this. This is for performance reasons: looping over every nested property to check if it's an error is expensive. Do **not** do this: | ||
Errors found in sub-properties of the log data are _not_ serialized like this due to performance reasons: looping over every nested property to check if it's an error is expensive (do **not** do this). Also, be mindful of passing in errors with a custom error property e.g. `myErrorProperty`, as you'll have to serialize them yourself like below: | ||
```js | ||
logger.info({ err: new Error('Oops') }); | ||
// Outputs: | ||
// { | ||
// "level": "info", | ||
// "err": {} | ||
// } | ||
``` | ||
```js | ||
logger.info({ myErrorProperty: serializeError(new Error('Oops')) }); | ||
``` | ||
If you _need_ to pass error objects as a property, you must serialize it yourself: | ||
... if not, you'll get the following output: | ||
```js | ||
logger.info({ myErrorProperty: new Error('Oops') }); | ||
// Outputs: | ||
// { | ||
// "level": "info", | ||
// "myErrorProperty": {} | ||
// } | ||
``` | ||
The exception to this is if the sub-property name is either `error` or `err`, as these are automatically serialized. E.g. | ||
```js | ||
logger.info({ err: serializeError(new Error('Oops')) }); | ||
``` | ||
logger.info({ error: new Error('Oops') }); | ||
// Outputs: | ||
// { | ||
// "level": "info", | ||
// "error": { | ||
// "cause": null, | ||
// "code": "UNKNOWN", | ||
// "data": {}, | ||
// "isOperational": false, | ||
// "message": "Oops", | ||
// "name": "Error", | ||
// "relatesToSystems": [], | ||
// "statusCode": null | ||
// } | ||
// } | ||
``` | ||
@@ -539,2 +568,4 @@ #### Order of precedence | ||
3. Ensure you don't disable prettification via the [`withPrettifier` option](#optionswithprettifier). | ||
### Production usage | ||
@@ -541,0 +572,0 @@ |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
47828
801
585
5
3
+ Addedcolorette@2.0.20(transitive)
+ Addeddateformat@4.6.3(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedfast-copy@3.0.2(transitive)
+ Addedfast-safe-stringify@2.1.1(transitive)
+ Addedhelp-me@5.0.0(transitive)
+ Addedjoycon@3.1.1(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpino-pretty@10.3.1(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedsecure-json-parse@2.7.0(transitive)
+ Addedstrip-json-comments@3.1.1(transitive)
+ Addedwrappy@1.0.2(transitive)
Updatedpino@^8.12.0