aigoncharov / cls-proxify

Logging on steroids with CLS and Proxy. Integrated with express, koa, fastify.
MIT License
160 stars 7 forks source link

Koa & pino - not work #1

Closed budarin closed 5 years ago

budarin commented 5 years ago

node: 11.7.0 OS: MacOS

I've created a logger module:

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 = {
            info: (msg: string) => {`${headerRequestID}: ${msg}`,
        };
        return loggerProxy;
    });

setInterval(() => loggerCls && process.env.NODE_ENV === 'production' && loggerCls.flush(), 10000).unref();

export default loggerCls;

and then use it in app

import http from 'http';
import Koa from 'koa';
import send from 'koa-send';
import serve from 'koa-static';
import body from 'koa-body';
import logger, { middleware } from './utils/getServerLogger';

const app = new Koa();
const server = http.createServer(app.callback());

app.use(middleware());
app.use(body());

app.use(async (ctx, next) => {
   logger.info('Now', Date.now());
   await nex();
})

......

there is no above log item in log - everything else is present but not the item above

what is wrong in the code?

aigoncharov commented 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;
budarin commented 5 years ago

thanks!