structlog
structlog copied to clipboard
Is there any way to accomplish indentation?
Is it possible to have indentation like in https://github.com/Gbps/fastlog?
It would be possible to implement for the same use cases (e.g. add a _structlog_indent
key/value pair to the event dict, add helpers that manipulate it, then support it in a logger), but currently isn't.
@hynek That sounds like a great idea. Would you be interested in reviewing a patch?
I'm afraid this is a little bit more complicated!
I mean you can easily implement it for yourself by wrapping dev.ConsoleRenderer
and a special key as outlined before. Then you'd just prepend the output of ConsoleRenderer with the indentation you've saved. This is why structlog is designed as it is: maximum flexibility for its users.
For structlog
as a project, it's a lot more complicated, because while we've moved towards cool CLI/console output lately, at it's core it's a server package that assumes that log entries can come in any order, from multiple threads or asyncio tasks.
In other words: there is no assumption about in which order log entries arrive and indenting would make no sense. E.g.
event1 thread=1
event2 thread=2
event3 thread=1
Now it looks like event3 is related to event1/thread 1.
The question here is: should structlog move more into the CLI space and how? Should we maybe add structlog.cli
module that does some fancy rich
- or even textual
-based magic?
Thanks for the clear and detailed reply.
Regarding indentation and threads. That's a good point. Of course, indentation would ideally be associated with each thread. But I can see your point about how that would be visually confusing. Still seems superior to having begin/end log messages though.
I'm not sure what you mean by CLI space.
By CLI space I mean providing tools that are specifically meant to be used in CLI tools with respective assumptions and limitations, like the fastlog you linked before.
@hynek Did you mean to close this as completed? If so, how should we use this new feature? I can't find a related pull request.
I mean you can easily implement it for yourself by wrapping
dev.ConsoleRenderer
and a special key as outlined before. Then you'd just prepend the output of ConsoleRenderer with the indentation you've saved. This is why structlog is designed as it is: maximum flexibility for its users.
@NeilGirdhar , I ran into this just now and here's what I ended up doing in case it helps.
class PrettyJSONRenderer(structlog.dev.ConsoleRenderer):
"""
Custom Renderer for Structlog which pretty-prints JSON for 'extra' field.
Inherits the ConsoleRenderer from structlog.dev and replaces the value of 'extra'
key in event dictionary with a pretty-printed JSON string.
"""
def __call__(self, _, __, event_dict):
if "extra" in event_dict:
event_dict["extra"] = json.dumps(event_dict["extra"], indent=2, default=str)
return super().__call__(_, __, event_dict)