-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor pretty to use context object
- Loading branch information
Showing
2 changed files
with
212 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
'use strict' | ||
|
||
module.exports = parseFactoryOptions | ||
|
||
const { | ||
LEVEL_NAMES | ||
} = require('../constants') | ||
const colors = require('../colors') | ||
const handleCustomLevelsOpts = require('./handle-custom-levels-opts') | ||
const handleCustomLevelsNamesOpts = require('./handle-custom-levels-names-opts') | ||
|
||
/** | ||
* A `PrettyContext` is an object to be used by the various functions that | ||
* process log data. It is derived from the provided {@link PinoPrettyOptions}. | ||
* It may be used as a `this` context. | ||
* | ||
* @typedef {object} PrettyContext | ||
*/ | ||
|
||
/** | ||
* @param {PinoPrettyOptions} options The user supplied object of options. | ||
* | ||
* @returns {PrettyContext} | ||
*/ | ||
function parseFactoryOptions (options) { | ||
const EOL = options.crlf ? '\r\n' : '\n' | ||
const IDENT = ' ' | ||
const { | ||
customPrettifiers, | ||
errorLikeObjectKeys, | ||
hideObject, | ||
levelFirst, | ||
levelKey, | ||
levelLabel, | ||
messageFormat, | ||
messageKey, | ||
minimumLevel, | ||
singleLine, | ||
timestampKey, | ||
translateTime | ||
} = options | ||
const errorProps = options.errorProps.split(',') | ||
const useOnlyCustomProps = typeof options.useOnlyCustomProps === 'boolean' | ||
? options.useOnlyCustomProps | ||
: (options.useOnlyCustomProps === 'true') | ||
const customLevels = handleCustomLevelsOpts(options.customLevels) | ||
const customLevelNames = handleCustomLevelsNamesOpts(options.customLevels) | ||
|
||
let customColors | ||
if (options.customColors) { | ||
customColors = options.customColors.split(',').reduce((agg, value) => { | ||
const [level, color] = value.split(':') | ||
|
||
const condition = useOnlyCustomProps | ||
? options.customLevels | ||
: customLevelNames[level] !== undefined | ||
const levelNum = condition | ||
? customLevelNames[level] | ||
: LEVEL_NAMES[level] | ||
const colorIdx = levelNum !== undefined | ||
? levelNum | ||
: level | ||
|
||
agg.push([colorIdx, color]) | ||
|
||
return agg | ||
}, []) | ||
} | ||
|
||
const customProperties = { customLevels, customLevelNames } | ||
if (useOnlyCustomProps === true && !options.customLevels) { | ||
customProperties.customLevels = undefined | ||
customProperties.customLevelNames = undefined | ||
} | ||
|
||
const includeKeys = options.include !== undefined | ||
? new Set(options.include.split(',')) | ||
: undefined | ||
const ignoreKeys = (!includeKeys && options.ignore) | ||
? new Set(options.ignore.split(',')) | ||
: undefined | ||
|
||
const colorizer = colors(options.colorize, customColors, useOnlyCustomProps) | ||
const objectColorizer = options.colorizeObjects | ||
? colorizer | ||
: colors(false, [], false) | ||
|
||
return { | ||
EOL, | ||
IDENT, | ||
colorizer, | ||
customColors, | ||
customLevelNames, | ||
customLevels, | ||
customPrettifiers, | ||
customProperties, | ||
errorLikeObjectKeys, | ||
errorProps, | ||
hideObject, | ||
ignoreKeys, | ||
includeKeys, | ||
levelFirst, | ||
levelKey, | ||
levelLabel, | ||
messageFormat, | ||
messageKey, | ||
minimumLevel, | ||
objectColorizer, | ||
singleLine, | ||
timestampKey, | ||
translateTime, | ||
useOnlyCustomProps | ||
} | ||
} |