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

Callable custom context serialized on object creation instead of logging call #673

Open
tback opened this issue Aug 5, 2021 · 3 comments

Comments

@tback
Copy link

tback commented Aug 5, 2021

In #457 you recommend passing a callable to log the request id.

Is this limited to pure javascript? In typescript it seems I can pass custom strings only. I assume genRequestId in your example is a function, right? in @types/bunyan custom is an array of strings.

import bunyan from 'bunyan';
const genRequestId = () => {return 'an-id'}

const logger = bunyan.createLogger({name: 'app', requestId: genRequestId
});
logger.info('hi')

logs this:

{"name":"app","hostname":"xxxx.local","pid":19383,"level":30,"msg":"hi","time":"2021-08-05T14:58:44.999Z","v":0}

(Notice the request id is missing).

Node 12.22.4
Bunyan: 1.8.15 or 2.0.5
macos bigsur

This was initially a comment on #457, but I think it's rather a bug, than a request for support. Also, my comment didn't reopen the issue, so I feared it wouldn't be noticed.

@tback
Copy link
Author

tback commented Aug 7, 2021

Found out that it's not typescript specific.

Defining a serializer for the field will work around the issue:

var logger = bunyan.createLogger({
    name:'app',
    serializers: {
        requestId: function(f) {
            return f()
        }
    },
    requestId: getRequestId
})

@tback tback closed this as completed Aug 7, 2021
@tback
Copy link
Author

tback commented Aug 9, 2021

Sorry, I've got to reopen this. Changing the title so it matches the problem.
Old title: Callable custom context broken in typescript?
New title: Callable custom context serialized on object creation instead of logging call.

I'm still trying to make bunyan log a request with an id.

Extra fields are serialized only once: When the logger is created. This makes it impossible to use an extra field for my purpose.
You sugessting exactly this strategy in #457 make me question this is an explicit design decision? What do you suggest instead?

@tback tback reopened this Aug 9, 2021
@tback tback changed the title Callable custom context broken in typescript? Callable custom context serialized on object creation instead of logging call Aug 9, 2021
@siakc
Copy link

siakc commented Mar 26, 2023

I had to make such wrapper to make it work:
{ trace (arg0, ...args) { const ctx = ctxLocalStorage.getStore() if (typeof (arg0) === 'object' && !Array.isArray(arg0)) { arg0.logId = ctx?.logId _log.trace(arg0, ...args) } else { _log.trace({logId: ctx?.logId}, arg0, ...args) } }, debug (arg0, ...args) { const ctx = ctxLocalStorage.getStore() if (typeof (arg0) === 'object' && !Array.isArray(arg0)) { arg0.logId = ctx?.logId _log.debug(arg0, ...args) } else { _log.debug({logId: ctx?.logId}, arg0, ...args) } }, info (arg0, ...args) { const ctx = ctxLocalStorage.getStore() if (typeof (arg0) === 'object' && !Array.isArray(arg0)) { arg0.logId = ctx?.logId _log.info(arg0, ...args) } else { _log.info({logId: ctx?.logId}, arg0, ...args) } }, warn (arg0, ...args) { const ctx = ctxLocalStorage.getStore() if (typeof (arg0) === 'object' && !Array.isArray(arg0)) { arg0.logId = ctx?.logId _log.warn(arg0, ...args) } else { _log.warn({logId: ctx?.logId}, arg0, ...args) } }, error (arg0, ...args) { const ctx = ctxLocalStorage.getStore() if (typeof (arg0) === 'object' && !Array.isArray(arg0)) { arg0.logId = ctx?.logId _log.error(arg0, ...args) } else { _log.error({logId: ctx?.logId}, arg0, ...args) } }, fatal (arg0, ...args) { const ctx = ctxLocalStorage.getStore() if (typeof (arg0) === 'object' && !Array.isArray(arg0)) { arg0.logId = ctx?.logId _log.fatal(arg0, ...args) } else { _log.fatal({logId: ctx?.logId}, arg0, ...args) } }, getLoggerInstance () { return _log // The Bunyan instance } }

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