[Python] Two distinct logging configurations in one program

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

  1. Stack

    Stack Membro Participativo

    I am trying to have two separate loggers. One that writes to a file and another that writes to a stdout. The file logger, needs to be passed onwards to a separate module. While the stdout logs the main program.

    import logging

    logging.basicConfig(filename='log.log', filemode='a', format="%(asctime)s - %(lineno)d - %(message)s", level=logging.DEBUG)
    log_file = logging.getLogger('another_program')

    logging.basicConfig(format="%(asctime)s - %(lineno)d - %(message)s", level=logging.ERROR)
    log_stdout = logging.getLogger('main')

    log_stdout.info('This should be stdout, from main program')
    another_program(logger=log_file) # Logs from inside should be written to log.log


    However, everyone just seems to be going to stdout. Its possibly because logging.basicConfig cant have multiple configs for the same program? If yes, how else can I get two separate loggers as I want?

    Continue reading...

Compartilhe esta Página