Skip to content

Commit

Permalink
Merge d3e9ae4 into b1a6f63
Browse files Browse the repository at this point in the history
  • Loading branch information
felixheck committed Apr 17, 2020
2 parents b1a6f63 + d3e9ae4 commit a074b85
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 82 deletions.
18 changes: 9 additions & 9 deletions package.json
Expand Up @@ -40,21 +40,21 @@
"ci.coverage": "nyc report --reporter=text-lcov | coveralls"
},
"devDependencies": {
"@hapi/hapi": ">=18.4",
"ava": "^3.4.0",
"coveralls": "^3.0.9",
"@hapi/hapi": ">=19.1",
"ava": "^3.7.0",
"coveralls": "^3.0.11",
"fixture-stdout": "^0.2.1",
"nyc": "^15.0.0",
"standard": "^14.3.1"
"nyc": "^15.0.1",
"standard": "^14.3.3"
},
"dependencies": {
"@hapi/joi": "^17.0.0",
"chalk": "^3.0.0",
"@hapi/joi": "^17.1.1",
"colors": "^1.4.0",
"error-stack-parser": "^2.0.6",
"hapi-pino": "^6.3.0",
"hapi-pino": "^8.0.0",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
"pino": "^5.15.0"
"pino": "^6.2.0"
},
"peerDependencies": {
"@hapi/hapi": ">=18.4"
Expand Down
4 changes: 2 additions & 2 deletions src/colors.js
@@ -1,4 +1,4 @@
const chalk = require('chalk')
const chalk = require('colors')
const utils = require('./utils')

/**
Expand Down Expand Up @@ -65,7 +65,7 @@ function getStatusColor (data, basic = false) {
* @returns {number | Function} The index or the related builder function
*/
function getLevelColor ({ level }, basic = false) {
const range = parseInt((level / 10) - 1)
const range = parseInt(level / 10 - 1)

return basic ? range : levelColors[range] || utils.noop
}
Expand Down
33 changes: 28 additions & 5 deletions src/logger.js
@@ -1,5 +1,6 @@
const hapiPino = require('hapi-pino')
const pino = require('pino')
const chalk = require('colors')
const colors = require('./colors')
const formats = require('./formats')
const tokens = require('./tokens')
Expand Down Expand Up @@ -38,7 +39,9 @@ function compile (format, tokens, isJSON, space, data) {

const js = format.replace(re, (m, name, arg) => {
const tokenArgs = 'Array.prototype.slice.call(arguments).slice(4)'
const output = `tokens[${utils.stringify(name)}](...(${tokenArgs}), ${utils.stringify(arg)})`
const output = `tokens[${utils.stringify(
name
)}](...(${tokenArgs}), ${utils.stringify(arg)})`
const template = `${output} == null ? '-' : ${output}`
const token = tokens[name]

Expand Down Expand Up @@ -78,11 +81,31 @@ function getLoggerConfig (options) {
prettifier: () => (data) => {
const format = formats.get(data)
const isJSON = utils.isJSON(format)
const pictor = colors.get(data, isJSON || !options.colored)
const disableColor = isJSON || !options.colored

const preprocessed = validator('preformatterOutput', options.preformatter(data, options))
const processed = compile(format, tokens, isJSON, options.indent, preprocessed, pictor)
const postprocessed = validator('postformatterOutput', options.postformatter(processed, options))
if (disableColor) {
chalk.disable()
} else {
chalk.enable()
}

const pictor = colors.get(data, disableColor)
const preprocessed = validator(
'preformatterOutput',
options.preformatter(data, options)
)
const processed = compile(
format,
tokens,
isJSON,
options.indent,
preprocessed,
pictor
)
const postprocessed = validator(
'postformatterOutput',
options.postformatter(processed, options)
)

return `${postprocessed}\n`
}
Expand Down
98 changes: 43 additions & 55 deletions src/validator.js
Expand Up @@ -7,67 +7,55 @@ const joi = require('@hapi/joi')
* Collection of `Joi` validators
*/
const validators = {
formatLabel: joi.string().valid('log', 'request', 'response', 'request-error', 'onPostStart', 'onPostStop', 'uncaught'),
formatLabel: joi
.string()
.valid(
'log',
'request',
'response',
'request-error',
'onPostStart',
'onPostStop',
'uncaught'
),
tokenLabel: joi.string(),
format: joi.alternatives().try(joi.string(), joi.any().valid(false)),
token: joi.function().maxArity(3),
preformatterOutput: joi.object(),
postformatterOutput: joi.string(),
options: joi.object({
formats: joi.object().allow(null),
presets: joi.object().allow(null),
tokens: joi.object().allow(null),
colored: joi.boolean().default(false),
override: joi.boolean().default(false),
indent: joi.alternatives().try(joi.number(), joi.string()).allow('').default(2),
preformatter: joi.function().maxArity(2),
postformatter: joi.function().maxArity(2),
handleUncaught: joi.boolean().default(false),
stream: joi.object().allow(null),
hapiPino: joi.object({
options: joi
.object({
formats: joi.object().allow(null),
presets: joi.object().allow(null),
tokens: joi.object().allow(null),
colored: joi.boolean().default(false),
override: joi.boolean().default(false),
indent: joi
.alternatives()
.try(joi.number(), joi.string())
.allow('')
.default(2),
preformatter: joi.function().maxArity(2),
postformatter: joi.function().maxArity(2),
handleUncaught: joi.boolean().default(false),
stream: joi.object().allow(null),
prettyPrint: joi.boolean(),
tags: joi.object(),
allTags: joi.string(),
instance: joi.object(),
logEvents: joi.alternatives().try(joi.array().items(joi.string()), joi.any().allow(false, null)),
mergeHapiLogData: joi.boolean().default(false),
serializers: joi.object(),
logRouteTags: joi.boolean(),
logRequestStart: joi.boolean(),
logRequestComplete: joi.boolean(),
ignorePaths: joi.array().items(joi.string()),
ignoreTags: joi.array().items(joi.string()),
redact: joi.array().items(joi.string()),
logPayload: joi.boolean().default(true),
getChildBindings: joi.function(),
level: joi.string()
}).default(),
pino: joi.object({
safe: joi.boolean(),
name: joi.string(),
serializers: joi.object(),
timestamp: joi.alternatives().try(joi.function(), joi.boolean()),
slowtime: joi.boolean(),
extreme: joi.boolean(),
level: joi.string(),
levelVal: joi.number(),
customLevels: joi.object(),
redact: joi.alternatives().try(joi.array(), joi.object()),
prettyPrint: joi.any(),
messageKey: joi.string().default('msg'),
changeLevelName: joi.string(),
useLevelLabels: joi.boolean(),
useOnlyCustomLevels: joi.boolean(),
onTerminated: joi.function(),
enabled: joi.boolean(),
browser: joi.any(),
nestedKey: joi.any(),
mixin: joi.any(),
base: joi.object(),
crlf: joi.boolean()
}).default()
}).default()
hapiPino: joi
.object({
stream: joi.object().allow(null),
logEvents: joi
.alternatives()
.try(joi.array().items(joi.string()), joi.any().allow(false, null)),
mergeHapiLogData: joi.boolean().default(false),
logPayload: joi.boolean().default(true)
})
.default(),
pino: joi
.object({
messageKey: joi.string().default('msg')
})
.default()
})
.default()
}

/**
Expand Down
28 changes: 17 additions & 11 deletions test/colors.spec.js
@@ -1,5 +1,5 @@
const test = require('ava')
const chalk = require('chalk')
const chalk = require('colors')
const helpers = require('./_helpers')
const colors = require('../src/colors')

Expand Down Expand Up @@ -33,15 +33,21 @@ test('calculate all colors correctly', (t) => {
}
}

t.is(JSON.stringify(colors.get(mock, true)), JSON.stringify({
dim: helpers.noop,
level: helpers.noop,
status: helpers.noop
}))
t.is(
JSON.stringify(colors.get(mock, true)),
JSON.stringify({
dim: helpers.noop,
level: helpers.noop,
status: helpers.noop
})
)

t.is(JSON.stringify(colors.get(mock)), JSON.stringify({
dim: chalk.grey,
level: chalk.yellow,
status: chalk.red
}))
t.is(
JSON.stringify(colors.get(mock)),
JSON.stringify({
dim: chalk.grey,
level: chalk.yellow,
status: chalk.red
})
)
})

0 comments on commit a074b85

Please sign in to comment.