Loguru-Based Message Logging with User Activities and Data Processing

  • Share this:

Code introduction


This Python function uses the Loguru library to log different types of messages, including user activities, errors, warnings, and completion of data processing.


Technology Stack : Loguru

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
import random
from loguru import logger

def random_log_message():
    messages = [
        "This is a simple log message",
        "An error occurred: {error}",
        "User {user_id} logged in successfully",
        "Processing data for {data_type} completed",
        "Warning: {warning_message}"
    ]
    return random.choice(messages)

def log_user_activity(user_id, action):
    message = f"User {user_id} {action}"
    logger.info(message)

def log_error(error):
    logger.error(f"An error occurred: {error}")

def log_warning(warning_message):
    logger.warning(f"Warning: {warning_message}")

def log_data_processing(data_type):
    logger.info(f"Processing data for {data_type} completed")                
              
Tags: