加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – XCGLogger 3.0不会将logfile写入文件系统(iOS 9)

发布时间:2020-12-14 04:29:11 所属栏目:百科 来源:网络整理
导读:我想使用XCGLogger 3.0在用 Swift 2编写的iOS9应用程序中进行日志记录.在AppDelegate中,我定义 let log = XCGLogger.defaultInstance() 并在应用程序中:didFinishLaunchingWithOptions我使用以下命令进行设置: log.setup(.Debug,showThreadName: true,show
我想使用XCGLogger 3.0在用 Swift 2编写的iOS9应用程序中进行日志记录.在AppDelegate中,我定义

let log = XCGLogger.defaultInstance()

并在应用程序中:didFinishLaunchingWithOptions我使用以下命令进行设置:

log.setup(.Debug,showThreadName: true,showLogLevel: true,showFileNames: true,showLineNumbers: true,writeToFile: "/tmp/app.log",fileLogLevel: .Debug)

当我在模拟器中启动应用程序时,我在XCode 7的控制台中看到了XCGLogger的输出.输出是:

2015-11-04 18:28:40.798 [Info] > myapp Version: 1.0 Build: 1 PID: 49243

2015-11-04 18:28:40.798 [Info] > XCGLogger Version: 3.0 – LogLevel: Debug

2015-11-04 18:28:40.801 [Info] > XCGLogger writing to log to: file:///tmp/app.log

但是,当我查看正确的模拟器实例的沙箱文件系统(使用SimPholders2)时,没有日志文件app.log.

当我在我的iPhone 6上启动应用程序时更糟糕.XCode控制台中的输出是:

2015-11-04 18:36:14.692 [Error] > Attempt to open log file for writing failed: The operation couldn’t be completed. (Cocoa error 2.)

我也尝试了不同的方法,如“/tmp/app.log”,“Library / Caches / de.myidentifier.myapp / app.log”,“/ Library / Cache / de.myidentifier.myapp / app.log”等.没有成功…

我究竟做错了什么?

解决方法

在iOS上,您不能只写入/ tmp文件夹.您需要确保路径位于应用程序的沙箱中.

为此,您需要向系统询问缓存目录. XCGLogger中的示例应用程序包含执行此操作的代码,但我也将其包含在此处.

试试这个:

let cacheDirectory: NSURL = {
    let urls = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory,inDomains: .UserDomainMask)
    return urls[urls.endIndex - 1] 
}()
let logPath: NSURL = cacheDirectory.URLByAppendingPathComponent("app.log")
log.setup(.Debug,writeToFile: logPath,fileLogLevel: .Debug)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读