Skip to content

Commit

Permalink
Added option for custom level colors (#36)
Browse files Browse the repository at this point in the history
* enabled custom colors per level

* added option to accept colors when setting up the transport

* fixed linting issues
  • Loading branch information
synapse authored Mar 12, 2024
1 parent 84f9988 commit 0191658
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
```

<a id="license"></a>
## License

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions lib/messageFormatFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
const formatDate = require('./formatDate')
const colorizerFactory = require('pino-pretty').colorizerFactory

const messageFormatFactory = (colorize, levels) => {
const colorizer = colorizerFactory(colorize === true)
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 = {
60: colorizer('fatal').toLowerCase(),
Expand Down
35 changes: 35 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
}
7 changes: 6 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ declare namespace oneLineLogger {
url: string;
}

export interface CustomColor {
[key: number]: string;
}

export type LogDescriptor = Record<string, unknown> & {
time: number;
level: number;
colors?: CustomColor;
req?: Request;
}
export const messageFormatFactory: (colorize: boolean, levels: Record<string, number>) => (log: LogDescriptor, messageKey: string) => string
export const messageFormatFactory: (colorize: boolean, levels: Record<string, number>, colors?: CustomColor) => (log: LogDescriptor, messageKey: string) => string

export const oneLineLogger: OneLineLogger
export { oneLineLogger as default}
Expand Down
9 changes: 5 additions & 4 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<string>(({} as Request).method)
expectType<string>(({} as Request).url)
Expand All @@ -14,7 +15,7 @@ expectType<number>(({} as LogDescriptor).level)
expectType<number>(({} as LogDescriptor).time)
expectType<Request | undefined>(({} as LogDescriptor).req)

expectType<(colorize: boolean, levels: Record<string, number>) => (log: LogDescriptor, messageKey: string) => string>(messageFormatFactory)
expectType<(colorize: boolean, levels: Record<string, number>, colors?: CustomColor) => (log: LogDescriptor, messageKey: string) => string>(messageFormatFactory)

expectType<typeof oneLineLogger>(oneLineLoggerNamed)
expectType<(opts?: pretty.PrettyOptions) => pretty.PrettyStream>(oneLineLogger)

0 comments on commit 0191658

Please sign in to comment.