nest
nest copied to clipboard
Add support for JSON format in LoggerService
Is there an existing issue that is already proposing this?
- [X] I have searched the existing issues
Is your feature request related to a problem? Please describe it
Not related to a problem
Describe the solution you'd like
When instantiating a new LoggerService, a new option property for format can be added. Based on the setting either an ConsoleLogger or possibly a new JsonConsoleLogger will be used.
the resulting log message would be splitted up into an object looking like this:
{
"pid": 1,
"timestamp": "XXX",
"level": "info",
"message": "hello",
...otherProps
}
Teachability, documentation, adoption, migration strategy
No migration neccesary, since regular text logger should be default
What is the motivation / use case for changing the behavior?
External log collection tools (like elasticsearch) have the ability to parse objects and split them into separate data rows for better filtration and querying.
I can contribute with an implementation if feature request is ok 😸
I can contribute with an implementation if feature request is ok 😸
That would be great! PRs are more than welcome
For future questions, which channel in discord is most suitable for questions related to core nest features?
For future questions, which channel in discord is most suitable for questions related to core nest features?
Hi @MiniMarker use nestjs forum (see screenshot) that what regarding NestJS framework topics such as core, cli, express, fastify, etc.

I created my own JsonConsoleLogger by extending ConsoleLogger
import { ConsoleLogger, LogLevel } from '@nestjs/common';
export class JsonConsoleLogger extends ConsoleLogger {
protected formatPid(pid: number) {
return `${pid}`;
}
protected colorize(message: string, logLevel: LogLevel) {
return message;
}
protected formatMessage(logLevel: LogLevel, message: unknown, pidMessage: string, formattedLogLevel: string, contextMessage: string, timestampDiff: string): string {
const output = this.stringifyMessage(message, logLevel);
return `${JSON.stringify({
pidMessage,
timestamp: this.getTimestamp(),
logLevel,
contextMessage,
output,
timestampDiff,
})}\n`
}
}
Output:
{"pidMessage":"43457","timestamp":"10/05/2022, 4:07:52 PM","logLevel":"log","contextMessage":"\u001b[38;5;3m[NestFactory] \u001b[39m","output":"Starting Nest application...","timestampDiff":""}
Goal was to get json without any colorize formating. Issue is currently we cannot override contextMessage
and timestampDiff
formatting. That's why I have created two PRs (already linked with this issue) to resolve this issue.
Hi @armujahid great job! You beat me to it 😅
One small detail is the naming of the keys which can be shortened a bit?
pidMessage -> pid
contextMessage -> context
output -> message
Also what do you do with optional context objects provided as the second parameter in the log functions?
@MiniMarker yes I will use short keys in final implementation.
Also what do you do with optional context objects provided as the second parameter in the log functions?
I didn't change context behavior so it will propagate as it is via 2nd string argument or via global setting of context via constructor.
logger.error('Hello world', "2nd string argument");
{"pid":"16973","timestamp":"10/06/2022, 10:56:03 AM","logLevel":"error","context":"\u001b[38;5;3m[2nd string argument] \u001b[39m","message":"Hello world","timestampDiff":""}
Thanks for sharing @armujahid!
@armujahid have you finished the final implementation? I can't find the pull request for the json logger