[Python] How to change filehandle with Python logging on the fly with different classes and...

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I cannot perform an on-the-fly logging fileHandle change.

    For example, I have 3 classes

    one.py

    import logging
    class One():
    def __init__(self,txt="?"):
    logging.debug("Hey, I'm the class One and I say: %s" % txt)


    two.py

    import logging
    class Two():
    def __init__(self,txt="?"):
    logging.debug("Hey, I'm the class Two and I say: %s" % txt)


    config.py

    import logging
    class Config():
    def __init__(self,logfile=None):
    logging.debug("Reading config")
    self.logfile(logfile)


    myapp

    from one import One
    from two import Two
    from config import Config
    import logging

    #Set default logging
    logging.basicConfig(
    level=logging.getLevelName(DEBUG),
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    filename=None
    )

    logging.info("Starting with stdout")

    o=One(txt="STDOUT")
    c=Config(logfile="/tmp/logfile")

    # Here must be the code that change the logging configuration and set the filehandler

    t=One(txt="This must be on the file, not STDOUT")


    If I try loggin.basicConfig() again, it doesn't work.

    Continue reading...

Compartilhe esta Página