From ea6d2bec7a0f4dca76e1e3d67f423fac1f4558df Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Tue, 11 Feb 2025 13:49:42 -0600 Subject: [PATCH] Move colorizing out of console transport body --- src/logger/transports/console.ts | 64 ++++++++++++++++---------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/logger/transports/console.ts b/src/logger/transports/console.ts index 072d86288e..6a687c69bb 100644 --- a/src/logger/transports/console.ts +++ b/src/logger/transports/console.ts @@ -14,38 +14,6 @@ export const consoleTransport: Transport = ( metadata, timestamp, ) => { - /** - * Color handling copied from Kleur - * - * @see https://github.com/lukeed/kleur/blob/fa3454483899ddab550d08c18c028e6db1aab0e5/colors.mjs#L13 - */ - const colors: { - [key: string]: [number, number] - } = { - default: [0, 0], - blue: [36, 39], - green: [32, 39], - magenta: [35, 39], - red: [31, 39], - yellow: [33, 39], - } - - function withColor([x, y]: [number, number]) { - const rgx = new RegExp(`\\x1b\\[${y}m`, 'g') - const open = `\x1b[${x}m`, - close = `\x1b[${y}m` - - return function (txt: string) { - if (txt == null) return txt - - return ( - open + - (~('' + txt).indexOf(close) ? txt.replace(rgx, close + open) : txt) + - close - ) - } - } - const hasMetadata = Object.keys(metadata).length const colorize = withColor( { @@ -88,3 +56,35 @@ export const consoleTransport: Transport = ( } } } + +/** + * Color handling copied from Kleur + * + * @see https://github.com/lukeed/kleur/blob/fa3454483899ddab550d08c18c028e6db1aab0e5/colors.mjs#L13 + */ +const colors: { + [key: string]: [number, number] +} = { + default: [0, 0], + blue: [36, 39], + green: [32, 39], + magenta: [35, 39], + red: [31, 39], + yellow: [33, 39], +} + +function withColor([x, y]: [number, number]) { + const rgx = new RegExp(`\\x1b\\[${y}m`, 'g') + const open = `\x1b[${x}m`, + close = `\x1b[${y}m` + + return function (txt: string) { + if (txt == null) return txt + + return ( + open + + (~('' + txt).indexOf(close) ? txt.replace(rgx, close + open) : txt) + + close + ) + } +}