structlog icon indicating copy to clipboard operation
structlog copied to clipboard

Is there any way to accomplish indentation?

Open NeilGirdhar opened this issue 2 years ago • 7 comments

Is it possible to have indentation like in https://github.com/Gbps/fastlog?

NeilGirdhar avatar Nov 14 '21 10:11 NeilGirdhar

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 avatar Nov 15 '21 05:11 hynek

@hynek That sounds like a great idea. Would you be interested in reviewing a patch?

NeilGirdhar avatar Nov 15 '21 07:11 NeilGirdhar

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?

hynek avatar Nov 17 '21 06:11 hynek

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.

NeilGirdhar avatar Nov 17 '21 10:11 NeilGirdhar

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 avatar Nov 17 '21 12:11 hynek

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

NeilGirdhar avatar Jul 06 '22 14:07 NeilGirdhar

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)

public-daniel avatar May 24 '23 21:05 public-daniel