简体   繁体   中英

Python - Logging from multiple modules to rotating files without printing to console

I am trying to add logging to a medium size Python project with minimal disruption. I would like to log from multiple modules to rotating files silently (without printing messages to the terminal). I have tried to modify this example and I almost have the functionality I need except for one issue.

Here is how I am attempting to configure things in my main script:

import logging
import logging.handlers
import my_module

LOG_FILE = 'logs\\logging_example_new.out'

#logging.basicConfig(level=logging.DEBUG)

# Define a Handler which writes messages to rotating log files.
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=100000, backupCount=1)
handler.setLevel(logging.DEBUG)     # Set logging level.
# Create message formatter.
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
# Tell the handler to use this format
handler.setFormatter(formatter)

# Add the handler to the root logger
logging.getLogger('').addHandler(handler)

# Now, we can log to the root logger, or any other logger. First the root...
logging.debug('Root debug message.')
logging.info('Root info message.')
logging.warning('Root warning message.')
logging.error('Root error message.')
logging.critical('Root critical message.')

# Use a Logger in another module.
my_module.do_something()                     # Call function which logs a message.

Here is a sample of what I am trying to do in modules:

import logging

def do_something():
    logger = logging.getLogger(__name__)
    logger.debug('Module debug message.')
    logger.info('Module info message.')
    logger.warning('Module warning message.')
    logger.error('Module error message.')
    logger.critical('Module critical message.')

Now, here is my problem. I currently get the messages logged into rotating files silently. But I only get warning, error, and critical messages. Despite setting handler.setLevel(logging.DEBUG) .

If I uncomment logging.basicConfig(level=logging.DEBUG) , then I get all the messages in the log files but I also get the messages printed to the terminal.

How do I get all messages above the specified threshold to my log files without outputing them to the terminal?

Thanks!

Update: Based on this answer , it appears that calling logging.basicConfig(level=logging.DEBUG) automatically adds a StreamHandler to the Root logger and you can remove it. When I did remove it leaving only my RotatingFileHandler , messages no longer printed to the terminal. I am still wondering why I have to use logging.basicConfig(level=logging.DEBUG) to set the message level threshold, when I am setting handler.setLevel(logging.DEBUG) . If anyone can shed a little more light on these issues it would still be appreciated.

You need to call set the logging level on the logger itself as well. I believe by default, the logging level on the logger is logging.WARNING

Ex.

root_logger = logging.getLogger('')
root_logger.setLevel(logging.DEBUG)

# Add the handler to the root logger
root_logger.addHandler(handler)

The loggers log level determines what the logger will actually log (ie what messages will actually get handed to the handlers). The handlers log level determines what it will actually handle (ie what messages actually are output to file, stream, etc). So you could potentially have multiple handlers attached to a logger each handling a different log level.

Here's an SO answer that explains the way this works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM