Random Log Generation with Loguru

  • Share this:

Code introduction


This function uses the Loguru library to generate a log with a random level. The function randomly selects a log level and then prints out a log message that includes this level.


Technology Stack : Loguru

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
import random
import loguru

def generate_random_log():
    log_level = random.choice(['INFO', 'DEBUG', 'WARNING', 'ERROR', 'CRITICAL'])
    log_message = f"Random log with level {log_level}"
    loguru.logger.add(lambda m: print(f"[{m.level.name}] {m.message}"), level=log_level)

generate_random_log()                
              
Tags: