aigoncharov / cls-proxify

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

Recommended way to log with hapi-pino #15

Closed josh803316 closed 4 years ago

josh803316 commented 4 years ago

I read this article about adding tracing to logs and it inspired me to want to enable this inside my hapi/hapi-pino project.

I registered hapi-pino as a hapi plugin just like the docs recommend (below). I'm wondering if it would make more sense to register a separate cls-pino-logger plugin or if it would be recommended to do it somehow inside of hapi-pino? Thanks in advance for any guidance.

await server.register({
    plugin: require('hapi-pino'),
    options: {
      prettyPrint: process.env.NODE_ENV !== 'production',
      // Redact Authorization headers, see https://getpino.io/#/docs/redaction
      redact: ['req.headers.authorization']
    }
  })

I created a plugin per the hapi plugin docs:

import { clsProxify, clsProxifyNamespace, setClsProxyValue } from 'cls-proxify'
import * as Pino from "pino";

const logger = Pino();
const loggerCls = clsProxify('clsKeyLogger', logger)

const handler = function (request, h) {
    clsProxifyNamespace.bindEmitter(request);
    clsProxifyNamespace.bindEmitter(request.response);

    clsProxifyNamespace.run(() => {
        const headerRequestID = request.headers.Traceparent
        // this value will be accesible in CLS by key 'clsKeyLogger'
        // it will be used as a proxy for `loggerCls`
        const loggerProxy = {
            info: (msg: string) => `${headerRequestID}: ${msg}`,
        }
        setClsProxyValue('clsKeyLogger', loggerProxy)
    })
};

exports.plugin = {
    name: 'cls-trace-logger',
    register: function (server, options) {
        server.route({ method: 'GET', path: '/test/cls', handler });

        loggerCls.info('My message!');
    }
};

But I get this error: [``` 1597365833550] ERROR (55306 on ip-192-168-2-50.ec2.internal): request error err: { "type": "AssertionError", "message": "can only bind real EEs", "stack": AssertionError [ERR_ASSERTION]: can only bind real EEs

josh803316 commented 4 years ago

Duplicate of #14