メインコンテンツまでスキップ

期限切れログの削除

指定した日数を超えたログ、または特定の基準日時より前のログを削除します。

前提条件

  • 現在のログサービスが永続保存に対応する出力先を使用していること。
  • 保存日数で削除する場合は、maxAge が 0 より大きいこと。

API Reference

フレームワーク

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 の変更は保存期間を設定するだけで、新しいクリーンアップを直ちに実行しません。すぐに削除する必要がある場合は purge メソッドを呼び出してください。
  • purge 操作は現在のログサービスが保持するデータにだけ適用されます。コンソールと OS Logger の出力は、アプリが管理する保存領域ではありません。
  • バックグラウンドの削除が成功したと仮定せず、完了コールバックを確認してください。