Emcee
38 строк · 1.3 Кб
1import Foundation
2
3public final class NSLogLikeLogEntryTextFormatter: LogEntryTextFormatter {
4
5// 2018-03-29 19:05:01.994
6public static let logDateFormatter: DateFormatter = {
7let logFormatter = DateFormatter()
8logFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
9logFormatter.timeZone = TimeZone.autoupdatingCurrent
10return logFormatter
11}()
12
13public init() {}
14
15public func format(logEntry: LogEntry) -> String {
16let timeStamp = NSLogLikeLogEntryTextFormatter.logDateFormatter.string(from: logEntry.timestamp)
17
18let filename = (logEntry.file as NSString).lastPathComponent
19
20// [LEVEL] 2018-03-29 19:05:01.994 <file:line> <coordinate1> [<coordinate2> [...]]: <mesage>
21var result = "[\(logEntry.verbosity.stringCode)] \(timeStamp) \(filename):\(logEntry.line)"
22if !logEntry.coordinates.isEmpty {
23result += " " + logEntry.coordinates.map { "\($0.stringValue)" }.joined(separator: " ")
24}
25result += ": " + logEntry.message
26return result
27}
28}
29
30extension LogEntryCoordinate {
31public var stringValue: String {
32var result = name
33if let value = value {
34result += ":\(value)"
35}
36return result
37}
38}
39