tintoy / seqlog

Python logging integration for Seq (https://getseq.net)
https://seqlog.readthedocs.io/
MIT License
16 stars 11 forks source link

How to use seqlog without seq server #18

Closed gilzig closed 5 years ago

gilzig commented 5 years ago

Description

I am using seq server as docker container. Sometimes, when someone in the group wants to debug locally, he does not have the seq container running. If I try to use the standard logger instead, I get format error, since the structured logging format is not the same as the standard logging one.

What I Did

I tried using the ConsoleStructuredLogHandler in the case where seq is not available:

root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
root_logger.handlers = []
root_logger.addHandler(seqlog.structured_logging.ConsoleStructuredLogHandler())
logging.error("Test {id}", id=5)

but I still get the error saying:

TypeError: _log() got an unexpected keyword argument 'id'

Is there a way to bypass seq for local debug purposes?

Thank you very much, Gil

tintoy commented 5 years ago

Hi - should be possible, will have a look when I get home.

gilzig commented 5 years ago

Thanks so much! Any update on this?

tintoy commented 5 years ago

Ah, sorry, haven’t had time to look yet - will try to have a look this afternoon!

tintoy commented 5 years ago

This is the code you need:

https://github.com/tintoy/seqlog/blob/development/v0.1/seqlog/__init__.py#L120

tintoy commented 5 years ago

See also: _override_root_logger function in the same file.

gilzig commented 5 years ago

Thanks!

The following single line from your link actually did the trick:

logging.root = StructuredRootLogger(logging.DEBUG)

The only issue is that the console log is always displayed in red color since (I assume) the logs output is always redirected to stderr... To solve this, I added the lines as before (after the new code):

root_logger = logging.getLogger()
root_logger.addHandler(seqlog.structured_logging.ConsoleStructuredLogHandler())

Now there is no color per log type (error/warning/etc.) but at least everything is white and not red...

Thank you for your help!!

Gil

tintoy commented 5 years ago

You’re welcome! Sorry for the delay in getting back to you - have been moving house and it’s been a little hectic :)