Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import type { TransformableInfo } from '../type'
import format from '../format'
export interface MetadataOptions {
/**
* The name of the key used for the metadata object. Defaults to `metadata`.
*/
key?: string
/**
* An array of keys that should not be added to the metadata object.
*/
fillExcept?: string[]
/**
* An array of keys that will be added to the metadata object.
*/
fillWith?: string[]
}
function fillExcept(info: TransformableInfo, fillExceptKeys: string[], metadataKey: string) {
const savedKeys = fillExceptKeys.reduce((acc, key) => {
acc[key] = info[key]
delete info[key]
return acc
}, {} as TransformableInfo)
const metadata = Object.keys(info).reduce((acc, key) => {
acc[key] = info[key]
delete info[key]
return acc
}, {} as TransformableInfo)
Object.assign(info, savedKeys, {
[metadataKey]: metadata,
})
return info
}
function fillWith(info: TransformableInfo, fillWithKeys: string[], metadataKey: string) {
info[metadataKey] = fillWithKeys.reduce((acc, key) => {
acc[key] = info[key]
delete info[key]
return acc
}, {} as TransformableInfo)
return info
}
/**
* Adds in a "metadata" object to collect extraneous data, similar to the metadata
* object in winston 2.x.
*/
export default format<MetadataOptions>((info, opts = {}) => {
const metadataKey = opts.key || 'metadata'
let fillExceptKeys: string[] = []
if (!opts.fillExcept && !opts.fillWith) {
fillExceptKeys.push('level')
fillExceptKeys.push('message')
}
if (opts.fillExcept) {
fillExceptKeys = opts.fillExcept
}
if (fillExceptKeys.length > 0) {
return fillExcept(info, fillExceptKeys, metadataKey)
}
if (opts.fillWith) {
return fillWith(info, opts.fillWith, metadataKey)
}
return info
})
|