hapijs / good

hapi process monitoring
Other
525 stars 161 forks source link

Feature Request: Allow logging of custom request parameters #529

Closed micahhausler closed 8 years ago

micahhausler commented 8 years ago

I'd like to use Good to create an audit log on a service, but currently I can only add request parameters that are predefined and hard-coded and only the payload from the response.

What I'd like to have is a way to get custom user-defined parameters off the request object to inject into the log. It might look something like this on the RequestSent class's constructor method:

if (reqOptions.extras.len > 0) {
    _.forEach(resOptions.extras, function(value, key) {
        if (request[key]){
            this[key] = request[key]
        }
     });
}

That way, I could inject user and group information to each request, say in an extension

module.exports = {
    type: "onPostAuth",
    method: function(req, res){
        req.user_id = req.session.user_id;
        req.group_id = req.session.group_id;
        res.continue();
    }
};

And then add the parameter names to my logging options

const options = {
    includes: {
        "request": [
            "headers",
            {"extras": ["user_id", "group_id"]}
        ]
    },
    reporters: {
        consoleReporter: [{
            "module": "good-console"
        }, 
        "stdout"]
    }
};

I'm not asking for a critique of this snippet's implementation, just if a patch with this type of logic would be accepted.

arb commented 8 years ago

What about just including request.state in the log? You could put your extra stuff in there and then you'd only need small change to good to have them show up?

micahhausler commented 8 years ago

Thats what I ended up doing.

module.exports = {
    type: "onPostAuth",
    method: function(req, res){
        req.log(["audit"], {user_id: req.session.user_id, ...});
        res.continue();
    }
};

and then I could filter on that log tag.

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.