跳到主要内容

删除过期日志

删除超过指定天数或早于特定截止日期的保留日志。

前提条件

  • 当前日志服务使用持久化输出目标。
  • 按期限删除时,maxAge 大于零。

API 参考

框架

AIBudsLog.xcframework

声明

Swift
/// Purges log messages older than the specified maximum age.
/// - Parameters:
///   - maxAge: Retention age in days.
///   - completion: Called with an error, or `nil` on success.
func purgeOldLogsWithMaxAge(_ maxAge: Int, completion: ((_ error: NSError?) -> Void)?)

/// Purges log messages earlier than the specified timestamp.
/// - Parameters:
///   - timestamp: The exclusive cutoff date for retained logs.
///   - completion: Called with an error, or `nil` on success.
func purgeOldLogsBefore(_ timestamp: Date, completion: ((_ error: NSError?) -> Void)?)

请参阅 purgeOldLogsWithMaxAgepurgeOldLogsBefore

使用示例

Swift
let retentionDays = 7

AIBudsLogSDK.logService.purgeOldLogsWithMaxAge(retentionDays) { error in
    DispatchQueue.main.async {
        if let error {
            statusLabel.text = "Cleanup failed: \(error.localizedDescription)"
            return
        }
        statusLabel.text = "Logs older than \(retentionDays) days were deleted"
    }
}

使用明确的截止日期删除:

Swift
let cutoff = Calendar.current.date(byAdding: .day, value: -7, to: Date())!
AIBudsLogSDK.logService.purgeOldLogsBefore(cutoff) { error in
    if let error { print(error.localizedDescription) }
}

注意事项

  • AIBudsLogSDK.setXLFacilityPlugin(_:) 在插件加载时使用当前 logFileMaxAge 自动清理 XLFacility 日志。
  • 更改 logFileMaxAge 仅配置保留期限,不会立即执行清理。需要立即删除时应调用清理方法。
  • 清理操作影响当前日志服务保留的数据;Console 和 OS Logger 输出并非 App 管理的保留存储。
  • 应使用完成回调确认结果,不要假定后台清理已经成功。