hapijs / good

hapi process monitoring
Other
525 stars 160 forks source link

Request headers and payload not logged in 'request' events, only 'response' events #520

Closed snorremd closed 7 years ago

snorremd commented 7 years ago

Hello,

I'm trying to log requests via request.log() and want the log output to include the headers and payload. I use the options as described in the documentation, but to no avail. It seems only the response event includes this data. The documentation states that:

[request] - array of extra hapi request object fields to supply to reporters on "request" events. Valid values ['headers', 'payload']. Defaults to [].

Therefore this seems like an unintended behavior to me, but perhaps I'm misunderstanding the documentation?

I use the following versions of hapi, good, and good-squeeze.

hapi 15.0.3 good 7.0.2 good-squeeze 5.0.0

Minimal example:

var Hapi = require('hapi');
var server = new Hapi.Server();

server.connection({
    host: "localhost",
    port: 9876
});

server.route([
    {
        method: 'POST',
        path: '/example',
        handler: function (request, reply) {
            request.log(["test"]);
            reply('done');
        }
    }
]);

var options = {
    includes: {
        request: ["headers", "payload"]
    },
    ops: {
        interval: 1000
    },
    reporters: {
        myConsoleReporter: [{
                module: 'good-squeeze',
                name: 'Squeeze',
                args: [{ log: '*', response: '*', request: '*' }]
            }, {
                module: 'good-squeeze',
                name: 'SafeJson'
            }, "stdout"]
    }
};

server.register({
    register: require('good'),
    options: options,
}, function (err) {
    if (err) {
        return console.error(err);
    }

    server.start(function () {
        console.info("Server started at " + server.info.uri);
        var options = {
            method: 'POST',
            url: '/example',
            headers: {
                "X-REQUEST-ID": "asdasd"
            },
            payload: {
                "aprop": "avalue"
            }
        };

        server.inject(options, function (res) {
            // Success
        });
    });
});

Output from request.log():

{
    "event": "request",
    "timestamp": 1474899373955,
    "tags": [
        "test"
    ],
    "pid": 26428,
    "id": "1474899373947:<hostname-redacted>:26428:itk508c4:10000",
    "method": "post",
    "path": "/example",
    "config": {}
}

Output from response logging includes the request payload and header.

{
    "event": "response",
    "timestamp": 1474899373947,
    "id": "1474899373947:<hostname redacted>:26428:itk508c4:10000",
    "instance": "http://localhost:9876",
    "labels": [],
    "method": "post",
    "path": "/example",
    "query": {},
    "responseTime": 21,
    "statusCode": 200,
    "pid": 26428,
    "httpVersion": "1.1",
    "source": {
        "remoteAddress": "127.0.0.1",
        "userAgent": "shot"
    },
    "route": "/example",
    "log": [],
    "headers": {
        "x-request-id": "asdasd",
        "user-agent": "shot",
        "host": "localhost:9876",
        "content-type": "application/json",
        "content-length": "18"
    },
    "requestPayload": {
        "aprop": "avalue"
    },
    "config": {}
}
arb commented 7 years ago

Per https://github.com/hapijs/good/blob/master/lib/utils.js#L88-L94

The includes options are only used when logging responses, not request.log.

snorremd commented 7 years ago

Thank you for your speedy answer.

My question then becomes: is it not a valid use case to log the request headers and request payload for the request events? In a scenario where you receive a Hapi request, but your route handler fails before returning a response, you may want to know what the request headers and payload were in order to debug the failure.

At the very least the documentation could be clearer about the type of log event the includes option operate.

arb commented 7 years ago

I'd certainly take a PR for the documentation update if it is not clear.

jainank commented 5 years ago

Can we log the request or not ? I am sorry its still not clear.

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.