Swift中的CocoaLumberjack,如何打印行号和filne名称

时间:2015-07-14 19:44:32

标签: ios swift cocoalumberjack

我是CocoaLumberjack的新手,我在this之后让它在Swift上工作。如果我尝试打印日志:

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

一切正常,我打印了所有级别,因为defaultDebugLevel是DDLogLevel.Verbose。

但是我找不到/找出如何用日志打印行或文件名。有什么想法吗?

非常感谢!!

4 个答案:

答案 0 :(得分:13)

经过一些研究后,我发现你必须创建一个日志格式化程序,这个日志格式化程序是DDDispatchQueueLogFormatter的子类,你必须覆盖一个接收DDLogMessage的函数并返回一个{{ 1}},例如:

String

然后你只需在你的AppDelegate或你设置CocaLumberJack的地方添加这样的格式化程序:

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)"
  }
}

希望它有所帮助!

答案 1 :(得分:4)

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

答案 2 :(得分:1)

Andres的回答更新了Swift 3(通过is here对日期时间格式进行了小调整)

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

答案 3 :(得分:1)

#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