question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to implement java-like MDC for logging contextual information using winston logger.

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pethaniakshaycommented, Apr 7, 2021

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.

1reaction
logic-1commented, Jan 29, 2019

@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 ?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found