SWIFT – Xcglogger 3.0 Write logfile to file system (iOS 9)

I want to use XCGLogger 3.0 to log in an iOS9 application written in Swift 2. In AppDelegate, I define

let log = XCGLogger.defaultInstance()

And in the application: didFinishLaunchingWithOptions I use the following command to set:

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

When I start the application in the simulator During the program, I saw the output of XCGLogger in the console of XCode 7. The output is:

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

But when I check the sandbox of the correct emulator instance When the file system (using SimPholders2), there is no log file app.log.

When I launch the application on my iPhone 6, it is even worse. The output in the XCode console is:

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.)

I also tried different methods, such as “/tmp/app.log”, “Library / Caches / de.myidentifier.myapp / app.log” , “/Library/Cache/de.myidentifier.myapp/app.log” etc. No success…

What am I doing wrong?

On iOS, you can’t just write to the /tmp folder. You need to make sure the path is in the app In the sandbox of the program.

For this, you need to ask the system for the cache directory. The sample application in XCGLogger contains the code to do this, but I also include it here.

Try this:

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)

I want to use XCGLogger 3.0 in Logging in an iOS9 application written in Swift 2. In AppDelegate, I define

let log = XCGLogger.defaultInstance()

And in the application: didFinishLaunchingWithOptions I use the following command to set:

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

When I start the application in the simulator, I see the output of XCGLogger in the console of XCode 7. The output is:

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

However, when I check the sandbox file system of the correct simulator instance (using SimPholders2), there is no log file app.log.

When I am on my iPhone 6 It’s even worse when starting the application. The output in the XCode console is:

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.)

I also tried different methods, such as “/tmp/app.log”, “Library / Caches/de.myidentifier.myapp/app.log”,”/Library/Cache/de.myidentifier.myapp/app.log” etc. No success…

What am I doing wrong?

On iOS, you can’t just write to the /tmp folder. You need to make sure the path is in the sandbox of the app.

For this, you need to ask the system for the cache directory. The sample application in XCGLogger contains the code to do this, but I also include it here.

Try this:< /p>

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)

Leave a Comment

Your email address will not be published.