question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to implement structured logging through indentation?

See original GitHub issue

I’d like to implement a context manager that will add some indentation to the log messages sent from within its scope.

Something like this:

from loguru import logger

# ... configure the logger in any way

logger.info('this is the "root" level')
logger.info('and so is this')

with indent_logs(logger, indent_size=4):
    logger.info("I'm at the first level")

    with indent_logs(logger, indent_size=2):
        logger.info("I'm on the second level")

    logger.info('back on level 1')

logger.info('back at root')

Which would output:

2021-04-09 14:04:14.335 | INFO     | __main__:<module>:29 - this is the "root" level
2021-04-09 14:04:14.335 | INFO     | __main__:<module>:30 - and so is this
2021-04-09 14:04:14.336 | INFO     | __main__:<module>:33 -     I'm at the first level
2021-04-09 14:04:14.336 | INFO     | __main__:<module>:36 -       I'm on the second level
2021-04-09 14:04:14.336 | INFO     | __main__:<module>:38 -     back on level 1
2021-04-09 14:04:14.336 | INFO     | __main__:<module>:40 - back at root

But I’m unsure of what is the safest way to modify the logger so that all handlers and configurations are affected properly.

Any advice?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
Delgancommented, Mar 15, 2022

Hi @edagnin. 😃

Have you thought about using a dynamic formatter with Loguru?

By doing a quick test, I get the following result:

import sys

from loguru import logger


def formatter(record):
    base_format = "{time} {level} {name} {message} " + " " * 10
    base = base_format.format_map(record)
    lines = str(record["extra"].get("data", "")).splitlines()
    indent = "\n" + " " * len(base)
    reformatted = base + indent.join(lines)
    record["extra"]["reformatted"] = reformatted
    return "{extra[reformatted]}\n{exception}"


logger.remove()
logger.add(sys.stderr, format=formatter)


data = """---------------- Request ----------------
Headers       :   {"Accept": "*/*",
                   "Accept-Encoding": "gzip, deflate",
                   "Connection": "keep-alive",
                   "Content-Length": "20",
                   "User-Agent": "python-requests/2.27.1",
                   "cookie": "foo=bar; bar=baz",
                   "x-pretty-print": "2"}
URL           :   http://mockbin.com/request/test/1/mock?foo=bar&foo=baz
Method        :   POST
Body          :   {"foo": "bar"}"""

logger.info("Default message")
logger.bind(data=data).info("Message with data")
2022-03-15T21:48:51.694763+0100 INFO __main__ Default message           
2022-03-15T21:48:51.695066+0100 INFO __main__ Message with data           ---------------- Request ----------------
                                                                          Headers       :   {"Accept": "*/*",
                                                                                             "Accept-Encoding": "gzip, deflate",
                                                                                             "Connection": "keep-alive",
                                                                                             "Content-Length": "20",
                                                                                             "User-Agent": "python-requests/2.27.1",
                                                                                             "cookie": "foo=bar; bar=baz",
                                                                                             "x-pretty-print": "2"}
                                                                          URL           :   http://mockbin.com/request/test/1/mock?foo=bar&foo=baz
                                                                          Method        :   POST
                                                                          Body          :   {"foo": "bar"}
1reaction
edagnincommented, Mar 18, 2022

@Delgan Thank you for your help!

I have done a bit of a cleanup and everything is working as expected.

I didn’t end using your last suggestions, since after I did the cleanup it was working AND I am not using the PropagateHandler since it’s working without that too.

So, with that said! Thank you for your help, genuinely appreciated! I hope this issue with all of its replies helps the next person too! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is Structured Logging and How to Use It - Loggly
First, to learn what structured logging is, you must take a step back and understand what exactly is unstructured logging. With unstructured logging,...
Read more >
How to Use NLog for Structured Logging - Honeycomb
Structured logging makes log entries machine-readable & easier to track over time. Learn how to use NLog for structured logging!
Read more >
Serialized data – structured logging concepts in .NET (6)
What does serialization have to do with logging? We've already explored the way that structured logs carry event data in machine-readable ...
Read more >
Is there a way to enforce indentation in the .log file? Or a script ...
This simple code is working for me. #include<string.h> #include<iostream> #include <fstream> using namespace std; int main(int argc, ...
Read more >
How to use structured logs in Python
How can we improve our logging, make it more readable? My favourite python package is Structlog, which brings structure into our logs. I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found