How to implement java-like MDC for logging contextual information using winston logger.
See original GitHub issueI am using winston - 3.0.0-rc5 with operating system as Windows.
Using winston logger, I have formatted log statement to have timestamp, level and message.
module.exports = {
namespace: "",
nodeID: null,
logger: bindings => winston.createLogger({
level: "debug",
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(log => {
return `${log.timestamp} ${log.level} ------ ${log.message}`;
})
),
transports: [
new (winston.transports.Console) ({
timestamp: true,
colorize: true,
prettyPrint: true,
}) /*,
new fluentTransport('xxx.xxxxxx', {
host: 'xx.xx.xx.xxx',
port: 'xxxx',
timeout: 3.0,
reconnectInterval: 60000
})*/
]
}),
On the same line, I need to know the way to implement java-like Mapped Diagnostic Context (MDC) for logging contextual information in a moleculer framework node js microservice using winston logger. Eg.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- Log message format -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%mdc] - %msg%n
</pattern>
</encoder>
</appender>
Any help on this?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Implementing a Java-Like Mapped Diagnostic Context (MDC ...
This allows for storing contextual information to be logged together with whatever message is emitted by the application. Typically one will ...
Read more >Improved NodeJs Logging with Mapped Diagnostic ...
Mapped Diagnostic Context (MDC) provides a way to enrich log messages with information that could be unavailable at the place where logging actually...
Read more >Java Logging with Mapped Diagnostic Context (MDC)
In this tutorial, we will explore the use of Mapped Diagnostic Context (MDC) to improve the application logging. Mapped Diagnostic Context ...
Read more >Heroku Logs - The Complete Guide
However, problems with Winston include a lack of important details in its default log formatting. An example of missing details is log entry ......
Read more >Chapter 8: Mapped Diagnostic Context - Logback - QOS.ch
Logback components, if appropriately configured, will automatically include this information in each log entry. Please note that MDC as implemented by ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is a much-needed feature.
Meanwhile, we followed a similar approach as @breznik suggested. We pass requestMeta object from our controller to service and service to as an argument of every log statement. But this takes your ability to pass an object as a second argument.
@indexzero creating child loggers is not solving the issue of sharing a context (which probably varies per request, not per logger). Do you suggest a child logger be created with each change of context ?