[Python] How can I subclass logging.Logger without breaking %(filename) in logging.Formatter's...

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to write a custom logging.Logger subclass which is mostly working, but I run into issues when trying to use a logging.Formatter that includes the interpolated value %(filename) in the custom format, it prints the filename where my custom subclass is, rather than the filename of the code that called the logging function.

    I've found a number of tutorials for subclassing Logger but none of them address the effects this has on the filename interpolation. Is there a straightforward solution to this without having to override large sections of logging.Logger?

    Sample code defining my custom logger:

    #-------------------------------------
    # custom_logger.py
    #-------------------------------------
    import logging
    import io
    class CustomLogger(logging.Logger):

    def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.setLevel(logging.DEBUG)

    # create the record format
    formatter = logging.Formatter(fmt = "%(filename)s - %(message)s")

    # create the handler
    self.stream = io.StringIO()
    handler = logging.StreamHandler(self.stream)
    handler.setFormatter(formatter)
    self.addHandler(handler)

    def debug(self, msg, *args, **kwargs):
    super().debug(msg, *args, **kwargs)

    # do some other stuff
    ...


    #-------------------------------------
    # test.py
    #-------------------------------------
    from custom_logger import CustomLogger
    import logging

    logging.setLoggerClass(CustomLogger)
    myLog = logging.getLogger("myLog")
    myLog.debug("hello world")
    print(myLog.stream.getvalue())


    Expected output: >>> test.py - hello world

    Actual output: >>> custom_logger.py - hello world

    Continue reading...

Compartilhe esta Página