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 ?

2
  • See also : stackoverflow.com/questions/1343227/… Commented Aug 26, 2021 at 7:18
  • Why don't you use the solution from the answer in your comment? Commented Aug 26, 2021 at 8:05

1 Answer 1

-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'
)
Sign up to request clarification or add additional context in comments.

2 Comments

Out of scope. In your solution, the message in logging.info() is as rich as the one of logging.debug().
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()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.