Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for custom level colors #36

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading