Closed budarin closed 5 years ago
@budarin the issue here is that you assign an object with info
property that does not log anything.
export const middleware = () =>
clsProxifyKoaMiddleware('clsKeyLogger', () => {
const headerRequestID = uuidv4();
// This is the actual object that's going to be put in CLS and used instead of your logger
const loggerProxy = {
// As you can see this method doesn't log anything it constructs a string, but doesn't do anything with it
info: (msg: string) => `${headerRequestID}: ${msg}`,
};
return loggerProxy;
});
You probably wanted to do something like this:
import pino, { Logger } from 'pino';
import { clsProxify } from 'cls-proxify';
import { clsProxifyKoaMiddleware } from 'cls-proxify/integration/koa';
import uuidv4 from '../../common/utils/uuid';
let logger: Logger;
switch (process.env.NODE_ENV) {
case 'development':
logger = pino({ prettyPrint: { colorize: true } });
break;
case 'production':
const dest = pino.extreme(); // logs to stdout with no args
logger = pino(dest);
break;
default:
throw new Error('Logger must be initialized! Set up process.env.NODE_ENV');
}
const loggerCls = clsProxify('clsKeyLogger', logger);
export const middleware = () =>
clsProxifyKoaMiddleware('clsKeyLogger', () => {
const headerRequestID = uuidv4();
const loggerProxy = logger.child({ headerRequestID })
return loggerProxy;
});
setInterval(() => loggerCls && process.env.NODE_ENV === 'production' && loggerCls.flush(), 10000).unref();
export default loggerCls;
thanks!
node: 11.7.0 OS: MacOS
I've created a logger module:
and then use it in app
there is no above log item in log - everything else is present but not the item above
what is wrong in the code?