Random Log Generation with Loguru

  • Share this:

Code introduction


This function utilizes the Loguru library to create a log entry with a randomly selected log level and sets the output format for the logs.


Technology Stack : The code uses the Loguru package to generate logs with random severity levels and configures the output format for the logs.

Code Type : Function

Code Difficulty : Advanced


                
                    
import random
import loguru
from loguru import logger

def generate_random_log():
    log_level = random.choice(["INFO", "WARNING", "ERROR"])
    message = f"Random message at {log_level.lower()} level"
    logger.log(log_level, message)

# Custom logger configuration
logger.remove()
logger.add(
    sink="console",
    level="DEBUG",
    format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}",
    enqueue=True,
)