From 3ebc2ab34ce8057dbc03a2ebb141d0bc6adf1ea7 Mon Sep 17 00:00:00 2001 From: Cristian Barlutiu Date: Mon, 11 Mar 2024 15:41:50 +0100 Subject: [PATCH 1/3] enabled custom colors per level --- lib/messageFormatFactory.js | 4 ++-- test/index.test.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/messageFormatFactory.js b/lib/messageFormatFactory.js index 01af8ef..6de29d0 100644 --- a/lib/messageFormatFactory.js +++ b/lib/messageFormatFactory.js @@ -2,8 +2,8 @@ const formatDate = require('./formatDate') const colorizerFactory = require('pino-pretty').colorizerFactory -const messageFormatFactory = (colorize, levels) => { - const colorizer = colorizerFactory(colorize === true) +const messageFormatFactory = (colorize, levels, customColors) => { + const colorizer = colorizerFactory(colorize === true, customColors) const levelLookUp = { 60: colorizer('fatal').toLowerCase(), diff --git a/test/index.test.js b/test/index.test.js index c8b7396..0b6fba3 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -113,3 +113,38 @@ logDescriptorColorizedLogPairs.forEach(([logDescriptor, logColorized]) => { }) }) } + +{ + const levels = { + foo: 35, + bar: 45 + } + const messageFormat = messageFormatFactory(true, levels, [[35, "bgCyanBright"], [45, "yellow"]]) + + const logCustomLevelsLogPairs = [ + [ + { time: EPOCH, level: 35, [MESSAGE_KEY]: 'basic foo log' }, + `${TIME} - \u001B[106mfoo\u001B[49m - \u001B[36mbasic foo log\u001B[39m` + ], + [ + { + time: EPOCH, + level: 45, + [MESSAGE_KEY]: 'basic incoming request bar log', + req: { + method: 'GET', + url: '/bar' + } + }, + `${TIME} - \u001B[33mbar\u001B[39m - GET /bar - \u001B[36mbasic incoming request bar log\u001B[39m` + ] + ] + logCustomLevelsLogPairs.forEach(([logDescriptor, expectedLog]) => { + test('format log correctly with custom colors per level', (t) => { + const log = messageFormat(logDescriptor, MESSAGE_KEY) + + t.equal(log, expectedLog) + t.end() + }) + }) +} \ No newline at end of file From 1650893ac3cf38ce4d9e9c69aedbb78976732d34 Mon Sep 17 00:00:00 2001 From: Cristian Barlutiu Date: Mon, 11 Mar 2024 17:20:20 +0100 Subject: [PATCH 2/3] added option to accept colors when setting up the transport --- README.md | 30 ++++++++++++++++++++++++++++++ index.js | 4 ++-- lib/messageFormatFactory.js | 5 ++++- test/index.test.js | 2 +- types/index.d.ts | 7 ++++++- types/index.test-d.ts | 9 +++++---- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e2566f4..8115333 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,36 @@ server.get("/", (request, reply) => { }); ``` +## Custom level colors + +Custom levels colors could be used by passing it into logger opts. They can also overwrite the default level's colors. Check all the supported colors [here](https://github.com/jorgebucaran/colorette?tab=readme-ov-file#supported-colors). + +```js +const server = fastify({ + logger: { + transport: { + target: "@fastify/one-line-logger", + colors: { + 35: "bgYellow", + 45: "magenta", + 60: "bgRedBright" // overwriting the `fatal` log color + } + }, + customLevels: { + foo: 35, + bar: 45, + }, + }, +}); + +server.get("/", (request, reply) => { + request.log.fatal("An error occured"); + request.log.foo("FOO!"); + request.log.bar("BAR!"); + reply.send({ foobar: true }); +}); +``` + ## License diff --git a/index.js b/index.js index ce357e2..f6f3ea1 100644 --- a/index.js +++ b/index.js @@ -4,9 +4,9 @@ const pretty = require('pino-pretty') const messageFormatFactory = require('./lib/messageFormatFactory') const oneLineLogger = (opts = {}) => { - const { colorize, levels, ...rest } = opts + const { colorize, levels, colors, ...rest } = opts - const messageFormat = messageFormatFactory(colorize, levels) + const messageFormat = messageFormatFactory(colorize, levels, colors) return pretty({ messageFormat, diff --git a/lib/messageFormatFactory.js b/lib/messageFormatFactory.js index 6de29d0..6f228f2 100644 --- a/lib/messageFormatFactory.js +++ b/lib/messageFormatFactory.js @@ -2,7 +2,10 @@ const formatDate = require('./formatDate') const colorizerFactory = require('pino-pretty').colorizerFactory -const messageFormatFactory = (colorize, levels, customColors) => { +const messageFormatFactory = (colorize, levels, colors) => { + const customColors = colors != null ? Object.entries(colors).reduce((colors, [level, color]) => { + return [...colors, [level, color]] + }, []) : undefined; const colorizer = colorizerFactory(colorize === true, customColors) const levelLookUp = { diff --git a/test/index.test.js b/test/index.test.js index 0b6fba3..47cdf32 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -119,7 +119,7 @@ logDescriptorColorizedLogPairs.forEach(([logDescriptor, logColorized]) => { foo: 35, bar: 45 } - const messageFormat = messageFormatFactory(true, levels, [[35, "bgCyanBright"], [45, "yellow"]]) + const messageFormat = messageFormatFactory(true, levels, {35: "bgCyanBright", 45: "yellow"}) const logCustomLevelsLogPairs = [ [ diff --git a/types/index.d.ts b/types/index.d.ts index 5d57050..1db4b64 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -8,12 +8,17 @@ declare namespace oneLineLogger { url: string; } + export interface CustomColor { + [key: number]: string; + } + export type LogDescriptor = Record & { time: number; level: number; + colors?: CustomColor; req?: Request; } - export const messageFormatFactory: (colorize: boolean, levels: Record) => (log: LogDescriptor, messageKey: string) => string + export const messageFormatFactory: (colorize: boolean, levels: Record, colors?: CustomColor) => (log: LogDescriptor, messageKey: string) => string export const oneLineLogger: OneLineLogger export { oneLineLogger as default} diff --git a/types/index.test-d.ts b/types/index.test-d.ts index ec78282..a584afb 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,11 +1,12 @@ +import pretty from 'pino-pretty'; +import { expectType } from "tsd"; import oneLineLogger, { - Request, + CustomColor, LogDescriptor, + Request, messageFormatFactory, oneLineLogger as oneLineLoggerNamed } from ".."; -import { expectType } from "tsd" -import pretty from 'pino-pretty' expectType(({} as Request).method) expectType(({} as Request).url) @@ -14,7 +15,7 @@ expectType(({} as LogDescriptor).level) expectType(({} as LogDescriptor).time) expectType(({} as LogDescriptor).req) -expectType<(colorize: boolean, levels: Record) => (log: LogDescriptor, messageKey: string) => string>(messageFormatFactory) +expectType<(colorize: boolean, levels: Record, colors?: CustomColor) => (log: LogDescriptor, messageKey: string) => string>(messageFormatFactory) expectType(oneLineLoggerNamed) expectType<(opts?: pretty.PrettyOptions) => pretty.PrettyStream>(oneLineLogger) From 081cbaa65bb9513380a31b3ebb0836a09f83b3bf Mon Sep 17 00:00:00 2001 From: Cristian Barlutiu Date: Tue, 12 Mar 2024 08:17:11 +0100 Subject: [PATCH 3/3] fixed linting issues --- lib/messageFormatFactory.js | 8 +++++--- test/index.test.js | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/messageFormatFactory.js b/lib/messageFormatFactory.js index 6f228f2..b5e31c8 100644 --- a/lib/messageFormatFactory.js +++ b/lib/messageFormatFactory.js @@ -3,9 +3,11 @@ const formatDate = require('./formatDate') const colorizerFactory = require('pino-pretty').colorizerFactory const messageFormatFactory = (colorize, levels, colors) => { - const customColors = colors != null ? Object.entries(colors).reduce((colors, [level, color]) => { - return [...colors, [level, color]] - }, []) : undefined; + const customColors = colors != null + ? Object.entries(colors).reduce((colors, [level, color]) => { + return [...colors, [level, color]] + }, []) + : undefined const colorizer = colorizerFactory(colorize === true, customColors) const levelLookUp = { diff --git a/test/index.test.js b/test/index.test.js index 47cdf32..904fbf2 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -119,7 +119,7 @@ logDescriptorColorizedLogPairs.forEach(([logDescriptor, logColorized]) => { foo: 35, bar: 45 } - const messageFormat = messageFormatFactory(true, levels, {35: "bgCyanBright", 45: "yellow"}) + const messageFormat = messageFormatFactory(true, levels, { 35: 'bgCyanBright', 45: 'yellow' }) const logCustomLevelsLogPairs = [ [ @@ -147,4 +147,4 @@ logDescriptorColorizedLogPairs.forEach(([logDescriptor, logColorized]) => { t.end() }) }) -} \ No newline at end of file +}