Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to accomplish indentation? #367

Open
NeilGirdhar opened this issue Nov 14, 2021 · 7 comments
Open

Is there any way to accomplish indentation? #367

NeilGirdhar opened this issue Nov 14, 2021 · 7 comments

Comments

@NeilGirdhar
Copy link

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

@hynek
Copy link
Owner

hynek commented Nov 15, 2021

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.

@NeilGirdhar
Copy link
Author

NeilGirdhar commented Nov 15, 2021

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

@hynek
Copy link
Owner

hynek commented Nov 17, 2021

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?

@NeilGirdhar
Copy link
Author

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.

@hynek
Copy link
Owner

hynek commented Nov 17, 2021

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 hynek closed this as completed Jul 6, 2022
@NeilGirdhar
Copy link
Author

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

@hynek hynek reopened this Jul 11, 2022
@public-daniel
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants