How to implement structured logging through indentation?
See original GitHub issueI’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:
- Created 2 years ago
- Comments:19 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @edagnin. 😃
Have you thought about using a dynamic formatter with Loguru?
By doing a quick test, I get the following result:
@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! 😃