Skip to content
The Chat Archive
mistral-medium-2505· Mistral AI LMArena Human Preference 140K

How would you write a subclass of logurus Logger class? So that i can still follow the…

  1. User

    How would you write a subclass of logurus Logger class? So that i can still follow the "only use from loguru import logger" but from my custom package

  2. Assistant

    To create a subclass of the loguru.Logger class while still adhering to the "only use from loguru import logger" convention, you can follow these steps:

    1. Create a Custom Logger Class

    First, define your custom logger class that inherits from loguru.Logger. You can add custom methods or override existing ones as needed.

    2. Replace the Default Logger

    Use loguru's logger.bind() or logger.patch() to modify the default logger instance, or replace it entirely with your custom logger instance.

    Example Implementation

    Here’s how you can do it:

    Step 1: Define Your Custom Logger Class

    from loguru import logger as default_logger
    
    class CustomLogger(default_logger.__class__):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
        def custom_method(self, message):
            """Add a custom logging method."""
            self.info(f"Custom: {message}")
    
        def debug(self, message):
            """Override the debug method to add custom behavior."""
            super().debug(f"DEBUG OVERRIDE: {message}")
    

    Step 2: Replace the Default Logger

    You can replace the default logger with an instance of your CustomLogger:

    # Replace the default logger with your custom logger
    logger = CustomLogger()
    
    # Now you can use `from loguru import logger` as usual, but it will be your custom logger
    from loguru import logger
    
    logger.custom_method("This is a custom log message!")
    logger.debug("This debug message is overridden.")
    

    Alternative: Monkey-Patch the Default Logger

    If you don’t want to replace the logger entirely, you can monkey-patch the default logger to add your custom methods:

    from loguru import logger
    
    def custom_method(self, message):
        self.info(f"Custom: {message}")
    
    # Add the custom method to the default logger
    logger.__class__.custom_method = custom_method
    
    # Now you can use it like this:
    logger.custom_method("This is a custom log message!")
    

    Best Practice: Use a Separate Module

    To keep things clean, you can define your custom logger in a separate module and then import it where needed. For example:

    custom_logger.py

    from loguru import logger as default_logger
    
    class CustomLogger(default_logger.__class__):
        def custom_method(self, message):
            self.info(f"Custom: {message}")
    
    # Replace the default logger
    logger = CustomLogger()
    

    main.py

    from custom_logger import logger
    
    logger.custom_method("This is a custom log message!")
    

    Summary

    • Subclass loguru.Logger to create CustomLogger.
    • Replace the default logger with an instance of CustomLogger.
    • Use from loguru import logger as usual, but it will now be your custom logger.

    This way, you maintain the simplicity of from loguru import logger while extending its functionality.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.