Roman Vinogradov
Roman Vinogradov

Reputation: 145

Python: How to log debug+ messages only when an error occures, info+ otherwise

My objective:

I found logging.handlers.MemoryHandler class: https://docs.python.org/2/library/logging.handlers.html#memoryhandler

It partly fits my purpose. It buffers all messages and flushes them whenever an error-level message appears. But if there is no error-message, it won't log anything at all.

I want my app to still log INFOs and WARNINGs if there were no error.

What's the best way to implement that?

Upvotes: 0

Views: 85

Answers (1)

Saif Asif
Saif Asif

Reputation: 5658

The way I see it you can achieve something similar by configuring two loggers.

  1. INFO-Logger to log only INFO level logging
  2. DETAIL-Logger to log in DEBUG mode

As an example see the documentation

You can decide how to log them, same file (maybe) or separate files (my-simple.log and my-detailed.log)

Upvotes: 1

Related Questions