0

This is how I generate a log file on device so that every NSLog statement will be logged this file:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:@"%@.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);

Now I am integrating Cocoalumberjack framework into my iOS app:

# Set a ddLogLevel
static const DDLogLevel ddLogLevel = DDLogLevelAll;

# Add logger for > iOS 10.0 and < iOS 10.0
if (@available(iOS 10.0, *)) {
    [DDLog addLogger:DDOSLogger.sharedInstance];
} else {
    [DDLog addLogger:DDTTYLogger.sharedInstance];
    [DDLog addLogger:DDASLLogger.sharedInstance];
}
...
# I use DDLogDebug to log a debug message...
DDLogDebug(@"%@", message);

However, now it does not log to the file any more. The device that I used for testing is an iPhone 7 with iOS 12.0. So DDOSLogger is actually added. What's the problem here?

zfgo
  • 195
  • 11
  • What does the first set of code have to do with setting up CocoaLumberjack? Why not use `DDFileLogger` if you want to log to a file? – rmaddy Jan 28 '19 at 15:34
  • @rmaddy The first set of code does not have anything to do with setting up CocoaLumberjack. It's just how I used to log to a file on device. – zfgo Jan 28 '19 at 16:43

1 Answers1

0

You need to create a fileLogger instance then add it to DDLog

let fileLogger: DDFileLogger = DDFileLogger() 
//Some config here
DDLog.add(fileLogger)
batphonghan
  • 487
  • 3
  • 10