应用程序中的 ios 记录器和应用程序扩展问题

ios Logger in app and app extension problem

提问人:alex king 提问时间:11/17/2023 更新时间:11/17/2023 访问量:23

问:

尝试在应用和扩展 PacketTunnelProvider 中连接操作。使用 CocoaLumberjack 在应用程序中:

func setupLogger() {
    let appGroup = "group.some"
    
    let containerUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
    let logsDirectory = containerUrl?.path
    
    let logFileManager = DDLogFileManagerDefault(logsDirectory: logsDirectory)
    let fileLogger = DDFileLogger(logFileManager: logFileManager)
    fileLogger.rollingFrequency = 60 * 60 * 24 // 24 hours
    fileLogger.logFileManager.maximumNumberOfLogFiles = 7
    
    DDLog.add(DDOSLogger.sharedInstance)
    DDLog.add(fileLogger)
    
    DDLogInfo("Run application \(Date())")
}

在扩展中:

- (void) setupLogger {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *containerUrl =
        [fileManager containerURLForSecurityApplicationGroupIdentifier:@"group.some"];
    id<DDLogFileManager> logFileManager =
        [[DDLogFileManagerDefault alloc] initWithLogsDirectory:containerUrl.path];
    
    _fileLogger = [[DDFileLogger alloc] initWithLogFileManager:logFileManager];
    _fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
    _fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
    [DDLog addLogger:_fileLogger];
    [DDLog addLogger:[DDOSLogger sharedInstance]];
}

我如何阅读日志:

let appGroup = "group.some"

let containerUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
let logsDirectory = containerUrl?.path

let logFileManager = DDLogFileManagerDefault(logsDirectory: logsDirectory)
let ddFileLogger = DDFileLogger(logFileManager: logFileManager)
let logFilePaths = ddFileLogger.logFileManager.sortedLogFilePaths

print("paths=\(logFilePaths)")

guard let logPath = logFilePaths.first  else { return }
        
let url = NSURL.fileURL(withPath: logPath)

let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: [])
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true) {
    ProgressHUD.dismiss()
}

问题:不保存来自扩展的日志,读取日志时只有应用程序日志,不保存扩展日志。我调试了扩展,检查了没有错误(例如,如果我写了错误的应用程序组,控制台中有错误),控制台中的日志正常显示。我不明白问题出在哪里。你能帮我解决这个问题吗?

Swift Objective-C Cocoalumberjack

评论


答: 暂无答案