0

I have created TestProject in Swift and added Cocoalumberjack using CocoaPod. Initialized Cocoalumberjack logger to add logs to console and file. Please find the initializer code snippet below.

    DDLog.add(DDTTYLogger.sharedInstance, with: DDLogLevel.verbose) // TTY = Xcode console

    let fileLogger: DDFileLogger = DDFileLogger() // File Logger
    fileLogger.rollingFrequency = 60 * 60 * 24 // 24 hours
    fileLogger.logFileManager.maximumNumberOfLogFiles = 7
    DDLog.add(fileLogger)

When i printed test message, Thread id and process id is missing in logs added in file but present in console.

DDLogDebug("Test message")

Console log: 2019-03-03 13:28:00:427 TestProject[81343:2525521] Test message

File Log: 2019/03/03 13:28:00:427 Test message

I need thread id and process id (81343:2525521) also to be added in file logs. Could someone help me in fixing this?

Cocoapod version : 1.5.3

Cocoalumberjack: CocoaLumberjack/Swift (3.4.2)

Anand
  • 1,820
  • 2
  • 18
  • 25

1 Answers1

2

You need a custom format for it. Here a sample for threadID and timestamp

class MyCustom: NSObject, DDLogFormatter { 
    func format(message logMessage: DDLogMessage) -> String? { 
       return "\(logMessage.threadID) - \(logMessage.timestamp)" 
    } 
}

Then adding to your file logger: fileLogger.logFormatter = MyCustom()

batphonghan
  • 487
  • 3
  • 10
  • Thanks @donmichael. I removed level as you suggested. But still issue is not resolved. – Anand Mar 03 '19 at 12:33
  • Could you show the code how you log `thread id and process id ` – batphonghan Mar 04 '19 at 13:43
  • Thread id and process is logged by Cocoalumberjack. This is the code to print test message. "DDLogDebug("Test message")". It prints these id's in console alone but not in file. – Anand Mar 04 '19 at 14:38
  • Okay, I understood now. You need a custom format for it. Here a sample for `threadID` and `timestamp` `class MyCustom: NSObject, DDLogFormatter { func format(message logMessage: DDLogMessage) -> String? { return "\(logMessage.threadID) - \(logMessage.timestamp)" } }` Then adding to your file logger: `fileLogger.logFormatter = MyCustom()` – batphonghan Mar 04 '19 at 15:03
  • Btw, in here you could find the whole information related to the log message: information related to the log message including: https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/Documentation/CustomFormatters.md – batphonghan Mar 04 '19 at 15:06
  • thanks @donmicael. DDLogFormatter did the magic. I am able to print the thread id in the file logger. Please post ur solution in the answer section instead of in comments. – Anand Mar 06 '19 at 04:04
  • Great to hear that mate. – batphonghan Mar 06 '19 at 05:04