0

Using python 3.8, I'd like to use logging, and record with different formats according to level.

With logging.DEBUG, my output format would be "%(asctime)s %(filename)s, line %(lineno)d : %(name)s %(levelname)s %(message)s", and above this level, it would be "%(asctime)s %(levelname)s %(message)s".

logger.debug("debug message")
logger.info("info message")

I've browsed through the doc. with LoggerAdapter, ContextFilter, ... I could not find any solution. Any idea ?

Eric H.
  • 2,152
  • 4
  • 22
  • 34
  • See also : https://stackoverflow.com/questions/1343227/can-pythons-logging-format-be-modified-depending-on-the-message-log-level – Eric H. Aug 26 '21 at 07:18
  • Why don't you use the solution from the answer in your comment? – blues Aug 26 '21 at 08:05

1 Answers1

-1

Use can use these code to set format of the logging and level parameter will determine the min level logged

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s | %(name)s | %(levelname)s | %(message)s',
    datefmt='%d.%m.%Y %H:%M:%S'
)
  • Out of scope. In your solution, the message in logging.info() is as rich as the one of logging.debug(). – Eric H. Aug 26 '21 at 07:22
  • If want that, you must create many loggers, for example: debug_logger and info_logger and set separated config for each logger. Use info_logger.info() and debug_logger() instead of logging.info() and logging.debug() – Luu Duc Anh Aug 26 '21 at 08:22