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

大憨熊 提交于 2019-12-05 12:40:24
Andres

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!

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

JAL

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