How to use Python's RotatingFileHandler

前端 未结 4 1595
一个人的身影
一个人的身影 2020-12-08 06:51

I\'m trying to do a test run of the logging module\'s RotatingFileHandler as follows:

import logging
from logging.handlers import R         


        
4条回答
  •  -上瘾入骨i
    2020-12-08 07:35

    All previous answers are correct, here another way of doing the same thing except we use logging config file instead.

    logging_config.ini

    Here is the config file :

    [loggers]
    keys=root
    
    [handlers]
    keys=logfile
    
    [formatters]
    keys=logfileformatter
    
    [logger_root]
    level=DEBUG
    handlers=logfile
    
    [formatter_logfileformatter]
    format=%(asctime)s %(name)-12s: %(levelname)s %(message)s
    
    [handler_logfile]
    class=handlers.RotatingFileHandler
    level=DEBUG
    args=('testing.log','a',10,100)
    formatter=logfileformatter
    

    myScrypt.py

    here is simple logging script that uses the above config file

    import logging
    from logging.config import fileConfig
    
    fileConfig('logging_config.ini')
    logger = logging.getLogger()
    logger.debug('the best scripting language is python in the world')
    

    RESULT

    here is the result, notice maxBytes is set to 10 but in real life, that's clearly too small. (args=('testing.log','a',10,100)

提交回复
热议问题