客户错误处理
本模块提供自定义日志记录、错误和警告处理功能,允许用户记录消息、清理日志文件以及向UI发送错误和警告信号。
日志管理
记录日志消息
- log_message(message, logger='default', include_timestamp=False)
Log a message to a user log file.
Log files are automatically named with timestamp: {logger}_{YYYYMMDD_HHMMSS}.log
- Parameters:
- Returns:
True if logging succeeded
- Return type:
Examples:
log_message("Task started") log_message("Processing item 1", "process_log", True)
清理日志文件
- clear_log(logger_name='', log_dir='/root/data/daystar_api/user_logs/')
Clear user log files.
This function works independently and doesn’t require initialization.
- Parameters:
- Returns:
Number of log files deleted
- Return type:
Examples:
# Delete all log files deleted = clear_log() print(f"Deleted {deleted} log files") # Delete only 'my_task' logs deleted = clear_log("my_task") print(f"Deleted {deleted} my_task log files")
错误和警告处理
触发错误信号
- raise_error(message)
Raise an error signal. The error will be displayed in the UI.
- Parameters:
message (
str) – Error message- Returns:
Response containing state and service response
- Return type:
Examples:
response = raise_error("Failed to connect to sensor") if response.state.code == StateCode.success: print("Error raised successfully")
触发警告信号
- raise_warn(message)
Raise a warning signal. The warning will be displayed in the UI.
- Parameters:
message (
str) – Warning message- Returns:
Response containing state and service response
- Return type:
Examples:
response = raise_warn("Battery level is low") if response.state.code == StateCode.success: print("Warning raised successfully")