Random Log Message Generation with Loguru

  • Share this:

Code introduction


This custom function uses the Loguru library to generate random log messages with specified log levels. The function takes the log level and message as arguments and uses the logger object from Loguru to log these messages.


Technology Stack : Loguru

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
import random
from loguru import logger

def random_log_message(level, message):
    """
    Generate a random log message with a specified log level.
    
    Args:
        level (str): The log level (e.g., "INFO", "DEBUG", "ERROR").
        message (str): The message to log.
    """
    # Use the logger with the specified level to log the message
    logger.log(level, message)

# Example usage
random_log_message("DEBUG", "This is a debug message.")
random_log_message("INFO", "This is an info message.")
random_log_message("ERROR", "This is an error message.")                
              
Tags: