Files
shields/services/trace.js
Paul Melnikow 12be1bd747 Service logging tweaks (#1929)
- Log the response when using `test:services:trace`
- Fully log large nested objects

Since the `badgeData` is in a different format from the JSON response, and also doesn't include the title, including this output is helpful. It makes it clearer what the Joi matchers are trying to match.

Sometimes when there's a deep nested structure, it's helpful or necessary to see the entire thing.
2018-08-18 11:25:40 -04:00

42 lines
1.2 KiB
JavaScript

'use strict'
const chalk = require('chalk')
// Config is loaded globally but it would be better to inject it. To do that,
// there needs to be one instance of the service created at registration time,
// which gets the config injected into it, instead of one instance per request.
// That way most of the current static methods could become instance methods,
// thereby gaining access to the injected config.
const {
services: { trace: enableTraceLogging },
} = require('../lib/server-config')
function _formatLabelForStage(stage, label) {
const colorFn = {
inbound: chalk.black.bgBlue,
fetch: chalk.black.bgYellow,
validate: chalk.black.bgGreen,
unhandledError: chalk.white.bgRed,
outbound: chalk.black.bgBlue,
}[stage]
return colorFn(` ${label} `)
}
function logTrace(stage, symbol, label, content, { deep = false } = {}) {
if (enableTraceLogging) {
if (deep) {
console.log(_formatLabelForStage(stage, label), symbol)
console.dir(content, { depth: null })
} else {
console.log(_formatLabelForStage(stage, label), symbol, '\n', content)
}
return true
} else {
return false
}
}
module.exports = {
logTrace,
}