본문으로 건너뛰기

로그 내보내기

File 또는 XLFacility에 저장된 로그를 내보내고, 앱에서 생성된 파일 URL을 공유합니다.

사전 요구 사항

  • 현재 출력 대상이 .file 또는 .xlfacility여야 합니다.
  • .xlfacility을 선택했다면 XLFacility 플러그인이 설치되어 있어야 합니다.
  • 저장된 로그가 하나 이상 있어야 합니다.

API Reference

프레임워크

AIBudsLog.xcframework

선언

Swift
/// Exports all retained logs to one file.
/// - Parameter completion: Called with the exported path, optional beginning
///   and ending timestamps, and an error.
func exportLogs(_ completion: ((
                    _ filePath: String?,
                    _ logStartFrom: Date?,
                    _ logEndAt: Date?,
                    _ error: NSError?
                ) -> Void)?)

/// Exports retained logs to size-limited files.
/// - Parameters:
///   - maxFileSize: Maximum size of each exported file, in bytes.
///   - completion: Called with exported paths, optional beginning and ending
///     timestamps, and an error.
func exportLogs(_ maxFileSize: UInt64,
                   completion: ((
                       _ filePaths: [String]?,
                       _ logStartFrom: Date?,
                       _ logEndAt: Date?,
                       _ error: NSError?
                   ) -> Void)?)

두 작업은 모두 LogService에 정의되어 있으며, logService에서 호출합니다.

사용 예제

단일 파일로 내보내기

Swift
AIBudsLogSDK.logService.exportLogs { filePath, startDate, endDate, error in
    DispatchQueue.main.async {
        if let error {
            print("Export failed: \(error.localizedDescription)")
            return
        }

        guard let filePath else {
            print("Export completed without a file")
            return
        }

        print("Log range: \(String(describing: startDate)) — \(String(describing: endDate))")
        presentShareSheet(items: [URL(fileURLWithPath: filePath)])
    }
}

크기가 제한된 여러 파일로 내보내기

Swift
let fiveMegabytes: UInt64 = 5 * 1_000_000
AIBudsLogSDK.logService.exportLogs(fiveMegabytes) { paths, _, _, error in
    guard error == nil, let paths, !paths.isEmpty else { return }
    let URLs = paths.map(URL.init(fileURLWithPath:))
    DispatchQueue.main.async { presentShareSheet(items: URLs) }
}

참고

  • maxFileSize의 단위는 byte입니다. Demo에서는 1 MB를 1_000_000 byte로 계산합니다.
  • .console.oslogger는 내보내기를 지원하지 않습니다. 로그를 수집하기 전에 영구 저장 출력 대상으로 변경하세요.
  • 경로와 오류가 함께 반환되면 실패로 처리하고, 경로 목록이 비어 있으면 내보낸 파일이 없는 것으로 처리하세요.
  • UIActivityViewController는 메인 스레드에서 표시하고, iPad에서는 popover의 source를 설정하세요.