期限切れログの削除
指定した日数を超えたログ、または特定の基準日時より前のログを削除します。
前提条件
- 現在のログサービスが永続保存に対応する出力先を使用していること。
- 保存日数で削除する場合は、
maxAgeが 0 より大きいこと。
API Reference
フレームワーク
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の変更は保存期間を設定するだけで、新しいクリーンアップを直ちに実行しません。すぐに削除する必要がある場合は purge メソッドを呼び出してください。- purge 操作は現在のログサービスが保持するデータにだけ適用されます。コンソールと OS Logger の出力は、アプリが管理する保存領域ではありません。
- バックグラウンドの削除が成功したと仮定せず、完了コールバックを確認してください。