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, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: logPath, fileLogLevel: .Debug)