diff --git a/lib/utils/prettify-metadata.js b/lib/utils/prettify-metadata.js index 5fb4a3b0..72483b14 100644 --- a/lib/utils/prettify-metadata.js +++ b/lib/utils/prettify-metadata.js @@ -21,18 +21,22 @@ module.exports = prettifyMetadata * returned. Otherwise, a string of prettified metadata is returned. */ function prettifyMetadata ({ log, context }) { - const prettifiers = context.customPrettifiers + const { customPrettifiers: prettifiers, colorizer } = context let line = '' if (log.name || log.pid || log.hostname) { line += '(' if (log.name) { - line += prettifiers.name ? prettifiers.name(log.name) : log.name + line += prettifiers.name + ? prettifiers.name(log.name, 'name', log, { colors: colorizer.colors }) + : log.name } if (log.pid) { - const prettyPid = prettifiers.pid ? prettifiers.pid(log.pid) : log.pid + const prettyPid = prettifiers.pid + ? prettifiers.pid(log.pid, 'pid', log, { colors: colorizer.colors }) + : log.pid if (log.name && log.pid) { line += '/' + prettyPid } else { @@ -43,14 +47,22 @@ function prettifyMetadata ({ log, context }) { if (log.hostname) { // If `pid` and `name` were in the ignore keys list then we don't need // the leading space. - line += `${line === '(' ? 'on' : ' on'} ${prettifiers.hostname ? prettifiers.hostname(log.hostname) : log.hostname}` + const prettyHostname = prettifiers.hostname + ? prettifiers.hostname(log.hostname, 'hostname', log, { colors: colorizer.colors }) + : log.hostname + + line += `${line === '(' ? 'on' : ' on'} ${prettyHostname}` } line += ')' } if (log.caller) { - line += `${line === '' ? '' : ' '}<${prettifiers.caller ? prettifiers.caller(log.caller) : log.caller}>` + const prettyCaller = prettifiers.caller + ? prettifiers.caller(log.caller, 'caller', log, { colors: colorizer.colors }) + : log.caller + + line += `${line === '' ? '' : ' '}<${prettyCaller}>` } if (line === '') { diff --git a/lib/utils/prettify-metadata.test.js b/lib/utils/prettify-metadata.test.js index 91281a96..cf2f892c 100644 --- a/lib/utils/prettify-metadata.test.js +++ b/lib/utils/prettify-metadata.test.js @@ -2,8 +2,12 @@ const tap = require('tap') const prettifyMetadata = require('./prettify-metadata') +const getColorizer = require('../colors') const context = { - customPrettifiers: {} + customPrettifiers: {}, + colorizer: { + colors: {} + } } tap.test('returns `undefined` if no metadata present', async t => { @@ -104,8 +108,42 @@ tap.test('uses prettifiers from passed prettifiers object', async t => { const str = prettifyMetadata({ log: { pid: '1234', hostname: 'bar', caller: 'baz', name: 'joe' }, context: { - customPrettifiers: prettifiers + customPrettifiers: prettifiers, + colorizer: { colors: {} } } }) t.equal(str, '(JOE/1234__ on BAR) ') }) + +tap.test('uses colorizer from passed context to colorize metadata', async t => { + const prettifiers = { + name (input, _key, _log, { colors }) { + return colors.blue(input) + }, + pid (input, _key, _log, { colors }) { + return colors.red(input) + }, + hostname (input, _key, _log, { colors }) { + return colors.green(input) + }, + caller (input, _key, _log, { colors }) { + return colors.cyan(input) + } + } + const log = { name: 'foo', pid: '1234', hostname: 'bar', caller: 'baz' } + const colorizer = getColorizer(true) + const context = { + customPrettifiers: prettifiers, + colorizer + } + + const result = prettifyMetadata({ log, context }) + + const colorizedName = colorizer.colors.blue(log.name) + const colorizedPid = colorizer.colors.red(log.pid) + const colorizedHostname = colorizer.colors.green(log.hostname) + const colorizedCaller = colorizer.colors.cyan(log.caller) + const expected = `(${colorizedName}/${colorizedPid} on ${colorizedHostname}) <${colorizedCaller}>` + + t.equal(result, expected) +})