Khaino
Khaino

Reputation: 4149

how to add filter in python logging config file (logging.conf)

Is it possible to add/use filter in logging config file? For example, is there any equivalent setting for the following code?

import logging

# Set up loggers and handlers.
# ...

    class LevelFilter(logging.Filter):
        def __init__(self, level):
            self.level = level

        def filter(self, record):
            return record.levelno == self.level

    logFileHandler.addFilter(LevelFilter(logging.DEBUG))

for handler in logging.conf

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=format_01
args=(sys.stdout,)

Can we write logging filter in config file (logging.conf) for above python code as for handler? (The python code is just for example)

Upvotes: 5

Views: 10333

Answers (2)

Laurent Babin
Laurent Babin

Reputation: 71

I did an example in Json if you want. Should be easily possible to switch to your format by following the logic :)

{
  "version": 1,
  "disable_existing_loggers": true,
  "filters": {
    "skipDebug": {
      "()": "__main__.RemoveLevelFilter",
      "levelToSkip": "DEBUG"
    }
  },
  "formatters": {
    "simple": {
      "format": "%(asctime)s|%(name)s [%(levelname)s] - %(message)s"
    }
  },
  "handlers": {
    "console":{
      "level": "DEBUG",
      "class": "logging.StreamHandler",
      "formatter": "simple",
      "stream" : "ext://sys.stdout"
    },
    "file": {
      "level": "DEBUG",
      "class": "logging.handlers.RotatingFileHandler",
      "maxBytes": 5242880,
      "backupCount": 3,
      "formatter": "simple",
      "filename": "log.log",
      "mode": "a",
      "encoding": "utf-8",
      "filters": ["skipDebug"]
    }
  },
  "loggers": { },
  "root": {
    "handlers": ["console", "file"],
    "level": "DEBUG"
  }
}

And when you initialize your logger something like:

class RemoveLevelFilter(object):
    def __init__(self, levelToSkip):
        self.level = levelToSkip

    def filter(self, record):
        return self.getLogLevelName(record.levelno) != self.level
    enter code here
    def getLogLevelName(self, levelno):
        switcher = {
            10: "DEBUG",
            20: "INFO",
            30: "WARNING",
            40: "ERROR",
            50: "CRITICAL"
        }
        return switcher.get(levelno, "INVALID")

with open("logging.json", "r", encoding="utf-8") as fd:
        logging.config.dictConfig(json.load(fd))

logger = logging.getLogger(__name__)

logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

P.S.: Strange my code like that I know. It was for something a little bit more complex that I truncated.

References:

Upvotes: 7

Khaino
Khaino

Reputation: 4149

After searching, I found the answer in official docs which says

For example, you cannot configure Filter objects, which provide for filtering of messages beyond simple integer levels, using fileConfig(). If you need to have instances of Filter in your logging configuration, you will need to use dictConfig().

The logging.conf format is

[loggers]
keys=root,log02,log03,log04,log05,log06,log07

[handlers]
keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09

[formatters]
keys=form01,form02,form03,form04,form05,form06,form07,form08,form09

There is no config for filter such as below

[filters]
keys=filter01,filter02

Upvotes: 6

Related Questions