using Python logger class to generate multiple logs for different log levels

前端 未结 1 833
面向向阳花
面向向阳花 2020-11-29 09:49

I looked through the tutorials for the python logging class here and didnt see anything that would let me make multiple logs of different levels for the same output. In the

相关标签:
1条回答
  • 2020-11-29 10:30

    Create multiple Handlers, each for one output file (INFO.log, DEBUG.log etc.).

    Add a filter to each handler that only allows the specific level.

    For example:

    import logging
    
    # Set up loggers and handlers.
    # ...
    
    class LevelFilter(logging.Filter):
        def __init__(self, level):
            self.level = level
    
        def filter(self, record):
            return record.levelno == self.level
    
    debugLogFileHandler.addFilter(LevelFilter(logging.DEBUG))
    infoLogFileHandler.addFilter(LevelFilter(logging.INFO))
    
    0 讨论(0)
提交回复
热议问题