Random Log Message Generation with Loguru

  • Share this:

Code introduction


This function uses the Loguru library to randomly generate a log message and record it with different log levels.


Technology Stack : Loguru

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import loguru
from loguru import logger

def generate_random_log_message():
    # Define a list of log levels
    log_levels = ["INFO", "DEBUG", "WARNING", "ERROR", "CRITICAL"]
    
    # Define a list of random messages
    random_messages = [
        "This is an informational message.",
        "An error has occurred.",
        "A warning has been issued.",
        "A critical error has been detected.",
        "Debugging information."
    ]
    
    # Select a random log level and message
    log_level = random.choice(log_levels)
    random_message = random.choice(random_messages)
    
    # Log the message with the selected log level
    logger.log(log_level, random_message)                
              
Tags: