Basic Usage of Python's Loguru Logging Library

  • Installlation

    pip install loguru
    
  • Basic Usage

    from loguru import logger
    
    logger.trace("This is a trace level log")  # Most detailed information, typically used for debugging issues
    logger.debug("This is a debug level log")  # Detailed information useful for debugging the program
    logger.info("This is an info level log")  # General information confirming the program works as expected
    logger.success("This is a success level log")  # Indicates a successful event (specific to loguru)
    logger.warning("This is a warning level log")  # Indicates an unexpected situation that still runs normally
    logger.error("This is an error level log")  # Indicates a serious problem causing a function failure
    logger.critical("This is a critical level log")  # Severe error that may cause program termination
    
  • Logging Excpetion Stack Traces

    logger.exception("This is an exception level log")
    # logger.exception() should be used within an except block because it needs the current exception context
    # Compared to logger.error(), it automatically records the exception stack trace
    
  • Logger Handler

    # By default, the logger has one handler that outputs logs to the console
    
    # Add a handler; the same logger can have multiple handlers that work simultaneously
    handler_id = logger.add(sys.stderr)  # sys.stderr is standard error output
    # Remove a handler
    logger.remove(handler_id)
    # Remove all handlers
    logger.remove()
    # Add a file logging handler
    logger.add("test.log")
    # Additional parameters for add method
    logger.add(
        "test.log",
        rotation="1 MB",  # Creates a new file when the log reaches 1MB, generating a new name like test.log.1
        compression="zip",  # Compresses the log files
        enqueue=True,  # Enables asynchronous queue to avoid blocking threads using the logger, improving concurrent write performance, recommended when using coroutines
        encoding="utf-8",  # Sets the encoding for the log file
        format="{time} {level} {message}",  # Sets the log format
        filter="my_module",  # Filters logs from specific modules, recording only logs from my_module module. More filtering options are available for flexible configuration.
        level="INFO",  # Sets the log level
    )
    # Other examples for rotation parameter:
    # "12:00"  # Creates a new log file every day at noon
    # "1 week"  # Creates a new log file every week
    
    # Filename placeholders:
    # "test_{time}.log", the logger replaces this with the current time when creating the file, e.g., test_2021-01-01.log
    
  • Logger.catch Decoraotr

    @logger.catch
    def my_function():
        pass
    
    
    # Using this decorator, exceptions in the function are caught, not propagated upwards, and detailed stack traces are printed/logged
    
  • Notes

    # The logger object can be used across multiple modules and threads, and it is thread-safe.
    # If the file specified by the logger handler is deleted, the handler's file handle no longer points to a file. Even if a file with the same name is created, it cannot continue writing. This handler becomes useless but occupies memory and needs to be removed.
    

Tags: python Loguru logging Tutorial

Posted on Tue, 15 Sep 2026 16:16:37 +0000 by eojlin