#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from loguru import logger
import sys

# 创建安全流处理器
class SafeStreamHandler:
    def __init__(self, stream):
        self.stream = stream

    def write(self, message):
        try:
            self.stream.write(message)
        except UnicodeEncodeError:
            clean_message = message.encode('utf-8', errors='ignore').decode('utf-8')
            self.stream.write(clean_message)

    def flush(self):
        self.stream.flush()

# 配置loguru
logger.remove()
logger.add(
    SafeStreamHandler(sys.stdout),
    format="{time:HH:mm:ss} | {level: <8} | {message}",
    level="INFO"
)

print("=== Loguru Simple Test ===")
logger.info("Server starting...")
logger.info("Test upload file")
logger.success("Upload completed")
logger.warning("File already exists")
logger.error("Download failed")
print("Test completed!")