CocoaLumberjack in Swift, how to print line number and filne name
Asked Answered
D

4

8

I'm new to CocoaLumberjack and I get it to work on Swift following this. If I try to print logs doing:

DDLogDebug("Debug")
DDLogInfo("Info")
DDLogWarn("Warning")
DDLogVerbose("Verbose")
DDLogError("Error")

Everything works fine and I get all the levels printed as my defaultDebugLevel is DDLogLevel.Verbose.

But I can't find/find out how to print the line or the filename with the log. Any idea?

Thanks a lot!!

Dialyze answered 14/7, 2015 at 19:44 Comment(3)
Maybe this will help linkRotherham
mmm not helping me haha. Thanks :)Dialyze
Recommend another nice Logger library: github.com/zhihuitang/SwiftMagicRecrystallize
D
17

After some more research I found that you have to create a log formatter, this log formatter is a subclass of DDDispatchQueueLogFormatter and you have to override a function that receives a DDLogMessage and returns a String, for example:

import Foundation
import CocoaLumberjack.DDDispatchQueueLogFormatter

class LogFormatter: DDDispatchQueueLogFormatter {
  let dateFormatter: NSDateFormatter

  override init() {
    dateFormatter = NSDateFormatter()
    dateFormatter.formatterBehavior = .Behavior10_4
    dateFormatter.dateFormat = "HH:mm"

    super.init()
  }

  override func formatLogMessage(logMessage: DDLogMessage!) -> String {
    let dateAndTime = dateFormatter.stringFromDate(logMessage.timestamp)
    return "\(dateAndTime) [\(logMessage.fileName):\(logMessage.line)]: \(logMessage.message)"
  }
}

Then you just have to add your formatter like this in your AppDelegate or wherever you are setting CocaLumberJack:

DDTTYLogger.sharedInstance().logFormatter = LogFormatter()

Hope it helps someone!

Dialyze answered 15/7, 2015 at 13:14 Comment(1)
@Hugh please do not edit existing answers to add new code. We don't want to put words into the original author's mouth. Instead, add a new answer.Nominee
E
7

Update for CocoaLumberjack (3.1.0):

import CocoaLumberjack.DDLog

class LogFormatter: NSObject, DDLogFormatter {
    let dateFormatter: DateFormatter

    override init() {
        dateFormatter = DateFormatter()
        dateFormatter.formatterBehavior = .behavior10_4
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss:SSS"

        super.init()
    }
    func format(message logMessage: DDLogMessage) -> String? {
        let dateAndTime = dateFormatter.string(from: logMessage.timestamp)
        return "\(dateAndTime) [\(logMessage.fileName):\(logMessage.line)]: \(logMessage.message)"
    }
}

DDTTYLogger.sharedInstance.logFormatter = LogFormatter()

Extraterritorial answered 21/4, 2017 at 15:23 Comment(0)
N
3

Andres's answer Updated for Swift 3 (with minor tweak of datetime format by Hugh)

import Foundation
import CocoaLumberjack.DDDispatchQueueLogFormatter

class LogFormatter: DDDispatchQueueLogFormatter {
    let dateFormatter: DateFormatter

    override init() {
        dateFormatter = DateFormatter()
        dateFormatter.formatterBehavior = .behavior10_4
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss:SSS"

        super.init()
    }

    override func format(message: DDLogMessage!) -> String {
        let dateAndTime = dateFormatter.string(from: message.timestamp)
        return "\(dateAndTime) [\(message.fileName!) \(message.function!):\(message.line)] \(message.message ?? "")"
    }
}
Nominee answered 14/7, 2015 at 19:44 Comment(0)
H
2
#import "CustomFormatter.h"

@implementation CustomFormatter : NSObject

- (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
    NSString *logLevel;
    switch (logMessage->_flag) {
        case DDLogFlagError    : logLevel = @"[E]"; break;
        case DDLogFlagWarning  : logLevel = @"[W]"; break;
        case DDLogFlagInfo     : logLevel = @"[I]"; break;
        case DDLogFlagDebug    : logLevel = @"[D]"; break;
        default                : logLevel = @"[V]"; break;
    }

    return [NSString stringWithFormat:@"%@:%lu %@ | %@ ", logMessage->_fileName, (unsigned long)logMessage->_line, logLevel, logMessage->_message];
}

@end
Huck answered 17/4, 2018 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.