How to log everything into a file using RotatingFileHandler by using logging.conf file?

前端 未结 2 2056
攒了一身酷
攒了一身酷 2021-02-19 02:24

I am trying to use RotatingHandler for our logging purpose in Python. I have kept backup files as 500 which means it will create maximum of 500 files I guess and th

相关标签:
2条回答
  • 2021-02-19 02:45

    I know, it is very late ,but I just got same error, and while searching that are I got your problem. I am able to resolve my problem, and I thought it might be helpful for some other user also :

    you have created a logger object and trying to access my_logger.config.fileConfig('log.conf') which is wrong you should use logger.config.fileConfig('log.conf') as I mention below and need to import logging.config as well :

    #!/usr/bin/python
    
    import logging
    import logging.handlers
    import logging.config
    
    logging.config.fileConfig('log.config',disable_existing_loggers=0)
    my_logger = logging.getLogger('you logger name as you mention in your conf file')
    
    my_logger.debug('debug message')
    my_logger.info('info message')
    my_logger.warn('warn message')
    my_logger.error('error message')
    my_logger.critical('critical message')
    

    after doing these changes, AttributeError: 'Logger' object has no attribute 'config' error must be gone.

    0 讨论(0)
  • 2021-02-19 02:48

    It doesn't print out INFO, DEBUG message into the file somehow.. Any thoughts why it is not working out?

    you don't seem to set a loglevel, so the default (warning) is used

    from http://docs.python.org/2/library/logging.html :

    Note that the root logger is created with level WARNING.

    as for your second question, something like this should do the trick (I haven't tested it, just adapted from my config which is using the TimedRotatingFileHandler):

    [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=NOTSET
    args=('testing.log','a',2000,100)
    formatter=logfileformatter
    
    0 讨论(0)
提交回复
热议问题