hapijs / hapi-pino

🌲 Hapi plugin for the Pino logger
MIT License
148 stars 61 forks source link

Include tags when hapi error events are logged #150

Closed jonathansamines closed 2 years ago

jonathansamines commented 2 years ago

What problem are you trying to solve? When a hapi event containing an error is logged by hapi-pino, then those errors are logged with very few context. This is specially true for server logs, where usually the context is provided by the plugin itself through the use of tags:

'use strict';

const Hapi = require('@hapi/hapi');
const Pino = require('hapi-pino');

const PluginA = {
    name: 'plugin-a',
    register(server) {
        const log = () => server.log(['error', 'plugin-a', 'operation-a'], new Error('Operation failed with error'));
        server.ext('onPreStart', log);
    }
};

const PluginB = {
    name: 'plugin-b',
    register(server) {
        const log = () => server.log(['error', 'plugin-b', 'operation-b'], new Error('Operation failed with error'));

        server.ext('onPreStart', log);
    }
};

async function run() {
    const server = Hapi.Server();

    await server.register([Pino, PluginA, PluginB]);

    return server.initialize();
}

run();

What did you get?

When the code above is executed, the additional information provided through the use of tags is missing from the log.

{"level":50,"time":1638469266007,"pid":6306,"hostname":"codespaces_02ef33","err":{"type":"Error","message":"Operation failed with error","stack":"Error: Operation failed with error\n    at log (/workspaces/hapi-pino-test-sandbox/server-logs-error-test.js:9:76)\n    at module.exports.internals.Core._invoke (/workspaces/hapi-pino-test-sandbox/node_modules/@hapi/hapi/lib/core.js:490:40)\n    at module.exports.internals.Core._initialize (/workspaces/hapi-pino-test-sandbox/node_modules/@hapi/hapi/lib/core.js:368:24)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)"},"msg":"Operation failed with error"}
{"level":50,"time":1638469266008,"pid":6306,"hostname":"codespaces_02ef33","err":{"type":"Error","message":"Operation failed with error","stack":"Error: Operation failed with error\n    at log (/workspaces/hapi-pino-test-sandbox/server-logs-error-test.js:17:76)\n    at module.exports.internals.Core._invoke (/workspaces/hapi-pino-test-sandbox/node_modules/@hapi/hapi/lib/core.js:490:40)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n    at async module.exports.internals.Core._initialize (/workspaces/hapi-pino-test-sandbox/node_modules/@hapi/hapi/lib/core.js:368:13)"},"msg":"Operation failed with error"}

What did you expected?

I expected the tags information to be preserved on server logs.

jonathansamines commented 2 years ago

Closing as this is now resolved by #151