elastic/ecs-logging-python

how to prefix structlog log value with "custom." or route them to a certain ecs field

mxab opened this issue · 3 comments

mxab commented

Hi,
according to the ecs documentation non ecs standard fields should go under "custom."
Also I might log data that should go to a certain ecs field

but i'm not sure how to configure something like this in structlog/ecs-logging.

for example this:

req_id = uuid()
...
logger.info("something happend", foo="123", request_id=req_id)

should end up being something like this:

{
 "http.request.id" : "<generated_req_uuid>",
 "message": "something happend",
 "custom.foo" : 123
...
}

You can use the **{...} construction with logger.bind() and logger.info() to auto-magically get what you want here:

logger.info(**{
 "http.request.id" : "<generated_req_uuid>",
 "message": "something happend",
 "custom.foo" : 123
})

also works with nested objects:

logger.info(**{
    "http": {
        "version": "2",
        "request": {
            "method": "get",
            "bytes": 1337,
        }
    }
})

Does this answer your question?

mxab commented

Hi thanks for the response.
yes specifing the full keys would work.

But looking more from a application developer side using structlog, I'm not sure I want to log that ecs aware.

I guess I need to add another processor infront that maybe adds all the non ecs field kwargs to the custom dict

It would be really awesome if user_id or http_request_id would be translated to user.id or http.request.id