pythonlogging

Python logging filter works with console but still writes to file


I am saving the logs to a text file and displaying them to the console at the same time. I would like to apply a filter on the logs, so that some logs neither make it to the text file nor the console output. However, with this code, the logs that I would like to filter out are still being saved to the text file. The filter only seems to work on the console output.

How can I apply the filter to the text file and the console output? Thank you very much

import logging

class applyFilter(logging.Filter):
    def filter(self, record):
        return not record.getMessage().startswith('Hello')

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s',
                    filename='log_file.txt',
                    filemode='a')

console = logging.StreamHandler()
console.addFilter(applyFilter())
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

logging.info('Hello world')

Solution

  • basicConfig created a FileHandler and a StreamHandler was also created and added to the logger. The filter was only applied to the StreamHandler. To filter both handlers, add the filter to the logger instead:

    import logging
    
    class applyFilter(logging.Filter):
        def filter(self, record):
            return not record.getMessage().startswith('Hello')
    
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s',
                        filename='log_file.txt',
                        filemode='a')
    
    console = logging.StreamHandler()
    # console.addFilter(applyFilter())  # not here
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    console.setFormatter(formatter)
    logger = logging.getLogger('')
    logger.addFilter(applyFilter())  # here
    logger.addHandler(console)
    
    logging.info('Hello world')
    logging.info('world only')
    

    Output (console):

    2024-12-30 00:44:31,702 - INFO - world only
    

    Output (log_file.txt):

    2024-12-30 00:44:31,702 - INFO - world only