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

前端 未结 2 2073
攒了一身酷
攒了一身酷 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条回答
  •  Happy的楠姐
    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.

提交回复
热议问题