Configuring a Loguru Logger with Custom Name and Level

  • Share this:

Code introduction


This function configures a logger named `name` with a specified level. It uses the Loguru library to save logs to a specified file with rotation and retention policies.


Technology Stack : Loguru, logging

Code Type : Configure the logger

Code Difficulty : Intermediate


                
                    
def loguru_config_logger(name, level):
    import logging
    from loguru import logger

    # Set up a logger with a custom name and level
    logger.remove()  # Remove all handlers to prevent duplicates
    logger.add(sink=f"logs/{name}.log", level=level, rotation="1 week", retention="1 month")

    # Get the configured logger
    return logger                
              
Tags: