Python logging configuration file

前端 未结 4 678
悲哀的现实
悲哀的现实 2020-12-24 01:26

I seem to be having some issues while attempting to implement logging into my python project.

I\'m simply attempting to mimic the following configuration:

Py

相关标签:
4条回答
  • 2020-12-24 01:52
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import logging
    import logging.handlers
    from logging.config import dictConfig
    
    logger = logging.getLogger(__name__)
    
    DEFAULT_LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
    }
    def configure_logging(logfile_path):
        """
        Initialize logging defaults for Project.
    
        :param logfile_path: logfile used to the logfile
        :type logfile_path: string
    
        This function does:
    
        - Assign INFO and DEBUG level to logger file handler and console handler
    
        """
        dictConfig(DEFAULT_LOGGING)
    
        default_formatter = logging.Formatter(
            "[%(asctime)s] [%(levelname)s] [%(name)s] [%(funcName)s():%(lineno)s] [PID:%(process)d TID:%(thread)d] %(message)s",
            "%d/%m/%Y %H:%M:%S")
    
        file_handler = logging.handlers.RotatingFileHandler(logfile_path, maxBytes=10485760,backupCount=300, encoding='utf-8')
        file_handler.setLevel(logging.INFO)
    
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.DEBUG)
    
        file_handler.setFormatter(default_formatter)
        console_handler.setFormatter(default_formatter)
    
        logging.root.setLevel(logging.DEBUG)
        logging.root.addHandler(file_handler)
        logging.root.addHandler(console_handler)
    
    
    
    [31/10/2015 22:00:33] [DEBUG] [yourmodulename] [yourfunction_name():9] [PID:61314 TID:140735248744448] this is logger infomation from hello module
    

    I think you should add the disable_existing_loggers to false.

    0 讨论(0)
  • 2020-12-24 02:01

    Just add log level in [logger_root]. It is worked.

    [logger_root]
    level=DEBUG
    handlers=screen,file
    
    0 讨论(0)
  • 2020-12-24 02:03

    Adding the following line to the root logger took care of my problem:

    level=NOTSET
    
    0 讨论(0)
  • 2020-12-24 02:12

    It looks like you've set the levels for your handlers, but not your logger. The logger's level filters every message before it can reach its handlers and the default is WARNING and above (as you can see). Setting the root logger's level to NOTSET as you have, as well as setting it to DEBUG (or whatever is the lowest level you wish to log) should solve your issue.

    0 讨论(0)
提交回复
热议问题