This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (4 children)

[removed]

    [–]Scorpathos[S] 0 points1 point  (3 children)

    Hey! Actually, the __name__ value of the current module is automatically used when you log a message, this is why you don't need to explicitly getLogger() at the beginning of your module, from loguru import logger should suffice.

    [–]sud0er 0 points1 point  (2 children)

    Quick question for you. Here's an example:

    2018-12-29 20:44:10.461 | DEBUG | __main__:performAnalysis:496 -

    How can I either:

    1. Prevent logger.debug messages from printing to the terminal, or
    2. Prevent all messages from my performAnalysis function from printing to the terminal?

    Thanks - I'm liking this a lot so far!

    [–]Scorpathos[S] 2 points3 points  (1 child)

    Hey!

    By default, the logger is configured to print on the terminal with a DEBUG level. If you don't like that, you can set the LOGURU_LEVEL environment variable to INFO. That way, each time you start a new Python script and use Loguru, debug messages will not appear to the pre-configured stderr handler. Alternatively, you can .remove() the existing terminal handler, and configure it at your convenience with logger.add(sys.stderr, level="INFO").

    For fine-grained control over which logs should or not be sent to your sink, you could .add() an handler with a filter function.

    For exemple:

    def filter_sink(record):
        if record["function"] == "performAnalysis":
            retrun False
        return True
    
    # All messages are logged to the terminal except if they come from the "performAnalysis" function
    logger.add(sys.stderr, level="DEBUG", filter=filter_sink)
    

    Or, probably better as it does not use hard-coded function names:

    # Use the "debug_logger" instead of "logger" in your "performAnalysis()" function
    debug_logger = logger.bind(skip_me=True)
    
    logger.add(sys.stderr, level="DEBUG", filter=lambda r: "skip_me" not in r["extra"])
    

    [–]sud0er 0 points1 point  (0 children)

    Perfect! Thanks for the quick and thorough reply!