[Python] Loguru for multiple python file

Discussão em 'Python' iniciado por Stack, Abril 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm new with loguru, so now have problem with logging multiple file. I have used the original python logging, and only need define the logger one time only, then it can catch the logging on multiple file, but the loguru does not. Specifically my project has a tree as

    myProject/
    |- pythonFile_1.py
    |- src/
    |--- function_1.py
    |--- function_2.py
    |--- ...


    As before when using the python logging, the log is defined in pythonFile_1.py

    import logging
    import logging.config
    from src.function_1 import *

    def defineLogger(savingPath, fileName, fileTime):
    logger = logging.getlogger()

    ... (add some handle)
    return

    defineLogger(...)


    logging.info("SOME CONTENT OF MY LOG")
    result = function_1_1(...)
    ....


    and in other file function_n.py in folder src/ , I only need import logging

    import logging

    def function_1_1():
    ...
    logging.info("SOME OTHER LOG CONTENT")
    ...
    return


    Then, it will handles automatically catch the log into stdout as well as log file

    But when I change into loguru, I define the same logging using loguru with same folder constructions, but it only prints the log that call in pythonFile_1.py but not in any function_n.py

    Known that the defineLogger now is defineLoguru function is follow tutorial of docs, such as the code below:

    from loguru import logger

    def defineLoguru(savingPath, fileName, fileTime):
    logger.add(<add some needed handle>)

    return logger

    logging = defineLoguru()

    logging.info("LOG CONTENT")

    Continue reading...

Compartilhe esta Página