0

I know that if I set the level to e.g. logging.DEBUG it will dump everything up to critical. But I want to capture exactly only debug messages. Is this possible? I already read this article:

python logging specific level only

But I don't get it. Adding filters like handler1.addFilter(MyFilter(logging.DEBUG)) or using IsEnabledFor() hasn't worked. :(

I hope, you can help me.

colidyre
  • 4,170
  • 12
  • 37
  • 53
NumeroUnoDE
  • 37
  • 1
  • 8
  • Could you provide your code snippet please. – Bro From Space Jul 04 '23 at 18:12
  • 1
    Yes, please provide a bit more context here, next time (the code, the expectation and what exactly went wrong). I found this question interesting, so I played a bit with the filter and provided it as an answer. I hope it solves your problem, but it is more a guess, because you haven't provided much information and we cannot see where you actually didn't understand something. – colidyre Jul 04 '23 at 18:39
  • 1
    Additionally, I can see that you have asked quite some questions on StackOverflow, which is good. But please consider also to honor the work from the authors of the answers by upvoting or accepting. [See also this helpful information](https://stackoverflow.com/help/someone-answers). – colidyre Jul 04 '23 at 23:25
  • @colidyre : Thank you for your efforts and your hints concerning open threads. I will mark the open posts as "solved". :) – NumeroUnoDE Jul 05 '23 at 08:51

1 Answers1

1

The code example from given url shows return logRecord.levelno <= self.__level. I think, you need a == for your use case (or to allow only one level to be more exact, i.e. also works for info level only):

import logging

# A simple formatting configuration with output to a file
logging.basicConfig(
    format="%(asctime)s :: %(name)s :: %(levelname)s :: %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S", filename="output.log", filemode="a",
    level=logging.DEBUG  # allow everything and filter later
)

logger = logging.getLogger(name="Filtered")


class MyFilter:
    def __init__(self, level):
        self.__level = level

    def filter(self, logRecord):
        return logRecord.levelno == self.__level


logger.debug("This should be written (before applying filter)")
logger.info("This should be written (before applying filter)")
logger.warning("This should be written (before applying filter)")
logger.error("This should be written (before applying filter)")
logger.critical("This should be written (before applying filter)")

logger.addFilter(MyFilter(logging.INFO))

logger.debug("This should not be written, because levelno != debug")
logger.info("This should be written, because levelno == info")  # <--- !
logger.warning("This should not be written, because levelno != warning")
logger.error("This should not be written, because levelno != error")
logger.critical("This should not be written, because levelno != critical")

outputs as configured to file output.log with this content:

2023-07-04 20:21:10 :: Filtered :: DEBUG :: This should be written (before applying filter)
2023-07-04 20:21:10 :: Filtered :: INFO :: This should be written (before applying filter)
2023-07-04 20:21:10 :: Filtered :: WARNING :: This should be written (before applying filter)
2023-07-04 20:21:10 :: Filtered :: ERROR :: This should be written (before applying filter)
2023-07-04 20:21:10 :: Filtered :: CRITICAL :: This should be written (before applying filter)
2023-07-04 20:21:10 :: Filtered :: INFO :: This should be written, because levelno == info

In your case it will be logger.addFilter(MyFilter(logging.DEBUG)) of course.

colidyre
  • 4,170
  • 12
  • 37
  • 53