adobe / helix-log

Log framework designed for use with project helix
Apache License 2.0
3 stars 9 forks source link

Provide context local/promise local logging configs with cls-hooked #71

Open koraa opened 4 years ago

koraa commented 4 years ago

https://www.npmjs.com/package/cls-hooked can be used to provide variables local to the current promise/async function context.

Current/Possible use case:

"Async awareness" could serve as another really nice unique selling point compared to other logging frameworks if we get this right!

Multiple API designs spring to my mind:

To be honest I don't really like any of those ideas…

tripodsan commented 4 years ago

I don't think that helix-log needs to provide continuation context (thread local) support out of the box.

Making rootLogger context dependent (or making it a getter so it can be customized by the library user)

it's not the logger that needs to be context dependent, but invoking the logger needs to be. eg:

// doesn't work!
const contextLogger = rootLoger.getContextAware();
contextLogger.log()

// works:
const contextLogger = rootLoger.getContextAware();
contextLogger.run(() => {
  contextLogger.log()
});

another approach could be to make the rootLogger a cls-context, eg:

// ---- helix-log
const rootLogger = createContext('helix-root-logger');
rootLogger.log = ....

// ---- test.js
rootLogger.run(() => {
   rootLogger.set('id', 42);
   info('should work!');
});

also see the writer example in cls-hooked: https://github.com/jeff-lewis/cls-hooked


all in all, I think the complexity of using cls-hooked is too big and might be too opinionated to provide out of the box. maybe adding some examples of how this could be achieved is enough.