8000 any MDC so we do not have to pass a logger around? · Issue #1431 · log4js-node/log4js-node · GitHub
[go: up one dir, main page]

Skip to content

any MDC so we do not have to pass a logger around? #1431

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

Open
deanbiltup opened this issue Aug 8, 2024 · 1 comment
Open

any MDC so we do not have to pass a logger around? #1431

deanbiltup opened this issue Aug 8, 2024 · 1 comment
8C77

Comments

@deanbiltup
Copy link

Is there any request context? In java, it was ThreadLocal until async programming...then Twitter invented Context.scala and Promise.scala so MDC in logback could be swapped.

Best with example.

Filter has MDC.put("requestId", generateUniqueReqId());

in the plain old typescript code without passing loggers around all the place(bad smell if we have to pass our logger all around) ->

class SomeBizClass {
    private _logger = createLogger();
    public someMethod(request: FetchPhotosRequest): FetchPhotosResponse {
            _logger.info("My log here"");
           return ...
    }
}

In this case, the output is msg="My log here" requestId="xxxsomeReqIdxxx"

Is this possible? This was an amazing thing about logback and log4j so in GCP, AWS, you simply filtered on requestId and developers never had to remember to

  1. pass a logger around
  2. type in requestId in every single log statement(nor pass requestId around).

It made things 1. extremely efficient and 2. error-prone. (no one every made any errors forgetting to pass a logger or requestID to log it).

@nomiddlename
Copy link
Collaborator

You don’t need to pass loggers around, when you call log4js.getLogger(“some category”) you’ll get a logger with the same context as any other call with the same category. Logger has a method called addContext which lets you store key value pairs, and the pattern layout lets you output those values in your logs. Should do what you want, give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0