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

妖精的绣舞 提交于 2019-12-10 06:14:57

问题


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!!


回答1:


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!




回答2:


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




回答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 ?? "")"
    }
}



回答4:


#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


来源:https://stackoverflow.com/questions/31415913/cocoalumberjack-in-swift-how-to-print-line-number-and-filne-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!