删除过期日志
删除超过指定天数或早于特定截止日期的保留日志。
前提条件
- 当前日志服务使用持久化输出目标。
- 按期限删除时,
maxAge大于零。
API 参考
框架
AIBudsLog.xcframework
声明
- Swift
- Objective-C
/// 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)?)/// Purges log messages older than the specified maximum age.
- (void)purgeOldLogsWithMaxAge:(NSInteger)maxAge
completion:(void (^ _Nullable)(NSError * _Nullable))completion;
/// Purges log messages earlier than the specified timestamp.
- (void)purgeOldLogsBefore:(NSDate * _Nonnull)timestamp
completion:(void (^ _Nullable)(NSError * _Nullable))completion;请参阅 purgeOldLogsWithMaxAge 和 purgeOldLogsBefore。
使用示例
- Swift
- Objective-C
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"
}
}使用明确的截止日期删除:let cutoff = Calendar.current.date(byAdding: .day, value: -7, to: Date())!
AIBudsLogSDK.logService.purgeOldLogsBefore(cutoff) { error in
if let error { print(error.localizedDescription) }
}
NSInteger retentionDays = 7;
[AIBudsLogSDK.logService
purgeOldLogsWithMaxAge:retentionDays
completion:^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error != nil) {
self.statusLabel.text = [NSString
stringWithFormat:@"Cleanup failed: %@", error.localizedDescription];
return;
}
self.statusLabel.text = @"Expired logs were deleted";
});
}];使用明确的截止日期删除:NSDate *cutoff = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
value:-7
toDate:[NSDate date]
options:0];
[AIBudsLogSDK.logService purgeOldLogsBefore:cutoff
completion:^(NSError *error) {
if (error != nil) {
NSLog(@"%@", error.localizedDescription);
}
}];
注意事项
AIBudsLogSDK.setXLFacilityPlugin(_:)在插件加载时使用当前logFileMaxAge自动清理 XLFacility 日志。- 更改
logFileMaxAge仅配置保留期限,不会立即执行清理。需要立即删除时应调用清理方法。 - 清理操作影响当前日志服务保留的数据;Console 和 OS Logger 输出并非 App 管理的保留存储。
- 应使用完成回调确认结果,不要假定后台清理已经成功。