ecs-logging-python icon indicating copy to clipboard operation
ecs-logging-python copied to clipboard

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

Open mxab opened this issue 4 years ago • 3 comments

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
...
}

mxab avatar Jul 29 '21 07:07 mxab

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?

sethmlarson avatar Jul 29 '21 20:07 sethmlarson

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

mxab avatar Jul 30 '21 08:07 mxab

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

as42sl avatar Dec 02 '21 07:12 as42sl