105 lines
2.2 KiB
TypeScript
105 lines
2.2 KiB
TypeScript
/**
|
|
* Thing 日志系统 —— 前端 API
|
|
*
|
|
* 用法:
|
|
* import { createLogger, getLogs, clearLogs, getLogInfo } from '@/lib/logger'
|
|
*
|
|
* const logger = createLogger('proxy')
|
|
* logger.info('mihomo 内核启动成功')
|
|
* logger.error('连接失败: timeout')
|
|
*
|
|
* // 查询日志
|
|
* const entries = await getLogs('proxy', 'error', 50)
|
|
*/
|
|
|
|
import { invoke } from '@tauri-apps/api/core'
|
|
|
|
export type LogLevel = 'debug' | 'info' | 'warn' | 'error'
|
|
|
|
export interface LogEntry {
|
|
timestamp: string
|
|
level: LogLevel
|
|
module: string
|
|
message: string
|
|
}
|
|
|
|
export interface LogInfo {
|
|
log_dir: string
|
|
log_files: string[]
|
|
total_size_bytes: number
|
|
max_file_size_bytes: number
|
|
max_files: number
|
|
}
|
|
|
|
// ===== 每个模块一个 Logger 实例 =====
|
|
|
|
class Logger {
|
|
private module: string
|
|
|
|
constructor(module: string) {
|
|
this.module = module
|
|
}
|
|
|
|
private write(level: LogLevel, message: string): void {
|
|
invoke('log_message', { level, module: this.module, message }).catch(() => {
|
|
// 日志写入失败不应阻塞业务逻辑
|
|
})
|
|
}
|
|
|
|
debug(message: string): void {
|
|
this.write('debug', message)
|
|
}
|
|
|
|
info(message: string): void {
|
|
this.write('info', message)
|
|
}
|
|
|
|
warn(message: string): void {
|
|
this.write('warn', message)
|
|
}
|
|
|
|
error(message: string): void {
|
|
this.write('error', message)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建一个绑定到指定模块的日志记录器。
|
|
*
|
|
* @param module 模块标识,如 'proxy'、'clipboard'、'app' 等
|
|
*/
|
|
export function createLogger(module: string): Logger {
|
|
return new Logger(module)
|
|
}
|
|
|
|
// ===== 静态查询/管理方法 =====
|
|
|
|
/**
|
|
* 查询日志。
|
|
*
|
|
* @param module 按模块过滤(可选)
|
|
* @param level 按级别过滤(可选)
|
|
* @param limit 返回条数上限(可选,默认全部)
|
|
*/
|
|
export async function getLogs(
|
|
module?: string,
|
|
level?: LogLevel,
|
|
limit?: number,
|
|
): Promise<LogEntry[]> {
|
|
return invoke('log_list', { module, level, limit })
|
|
}
|
|
|
|
/**
|
|
* 清空所有日志文件。
|
|
*/
|
|
export async function clearLogs(): Promise<void> {
|
|
return invoke('log_clear')
|
|
}
|
|
|
|
/**
|
|
* 获取日志系统信息(目录、文件列表、空间占用)。
|
|
*/
|
|
export async function getLogInfo(): Promise<LogInfo> {
|
|
return invoke('log_info_state')
|
|
}
|