From 9d7c0026debafb6c4da0d52cb45c648ea1079436 Mon Sep 17 00:00:00 2001 From: pavlo-vuiko Date: Sun, 24 Mar 2024 19:42:56 +0200 Subject: [PATCH 1/4] fix: add injection lacking arguments of customPrettifiers --- lib/utils/prettify-metadata.js | 22 +++++++++++++++++----- lib/utils/prettify-metadata.test.js | 8 ++++++-- 2 files changed, 23 insertions(+), 7 deletions(-) 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..e81ff490 100644 --- a/lib/utils/prettify-metadata.test.js +++ b/lib/utils/prettify-metadata.test.js @@ -3,7 +3,10 @@ const tap = require('tap') const prettifyMetadata = require('./prettify-metadata') const context = { - customPrettifiers: {} + customPrettifiers: {}, + colorizer: { + colors: {} + } } tap.test('returns `undefined` if no metadata present', async t => { @@ -104,7 +107,8 @@ 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) ') From 8a4d736913d344663176ea1396c60c03fa0d023e Mon Sep 17 00:00:00 2001 From: pavlo-vuiko Date: Mon, 25 Mar 2024 18:28:42 +0200 Subject: [PATCH 2/4] test: add unit test for log metadata colorizing --- lib/utils/prettify-metadata.test.js | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/utils/prettify-metadata.test.js b/lib/utils/prettify-metadata.test.js index e81ff490..70d09bf1 100644 --- a/lib/utils/prettify-metadata.test.js +++ b/lib/utils/prettify-metadata.test.js @@ -1,5 +1,6 @@ 'use strict' +const getColorizer = require('../colors') const tap = require('tap') const prettifyMetadata = require('./prettify-metadata') const context = { @@ -113,3 +114,36 @@ tap.test('uses prettifiers from passed prettifiers object', async t => { }) 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) +}) From e30e22cce39b15dd04cd563163219a2c0032991b Mon Sep 17 00:00:00 2001 From: pavlo-vuiko Date: Sun, 31 Mar 2024 16:55:14 +0300 Subject: [PATCH 3/4] refactor: fix import order in test --- lib/utils/prettify-metadata.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/prettify-metadata.test.js b/lib/utils/prettify-metadata.test.js index 70d09bf1..8186b67a 100644 --- a/lib/utils/prettify-metadata.test.js +++ b/lib/utils/prettify-metadata.test.js @@ -1,8 +1,8 @@ 'use strict' -const getColorizer = require('../colors') const tap = require('tap') const prettifyMetadata = require('./prettify-metadata') +const getColorizer = require('../colors') const context = { customPrettifiers: {}, colorizer: { From b6ef2ae2909fae05ff8051628d4b2679e56de1ec Mon Sep 17 00:00:00 2001 From: pavlo-vuiko Date: Sun, 31 Mar 2024 16:55:35 +0300 Subject: [PATCH 4/4] refactor: add underscores to unused function arguments --- lib/utils/prettify-metadata.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils/prettify-metadata.test.js b/lib/utils/prettify-metadata.test.js index 8186b67a..cf2f892c 100644 --- a/lib/utils/prettify-metadata.test.js +++ b/lib/utils/prettify-metadata.test.js @@ -117,16 +117,16 @@ tap.test('uses prettifiers from passed prettifiers object', async t => { tap.test('uses colorizer from passed context to colorize metadata', async t => { const prettifiers = { - name (input, _key, log, { colors }) { + name (input, _key, _log, { colors }) { return colors.blue(input) }, - pid (input, _key, log, { colors }) { + pid (input, _key, _log, { colors }) { return colors.red(input) }, - hostname (input, _key, log, { colors }) { + hostname (input, _key, _log, { colors }) { return colors.green(input) }, - caller (input, _key, log, { colors }) { + caller (input, _key, _log, { colors }) { return colors.cyan(input) } }