CommandLineToolkit
56 строк · 1.6 Кб
1/*
2* Copyright (c) Avito Tech LLC
3*/
4
5import Foundation
6
7public final class NSLogLikeLogEntryTextFormatter: LogEntryTextFormatter {
8
9// 2018-03-29 19:05:01.994
10public static let logDateFormatter: DateFormatter = {
11let logFormatter = DateFormatter()
12logFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
13logFormatter.timeZone = TimeZone.autoupdatingCurrent
14return logFormatter
15}()
16
17private let logLocation: Bool
18private let logCoordinates: Bool
19
20public init(
21logLocation: Bool,
22logCoordinates: Bool
23) {
24self.logLocation = logLocation
25self.logCoordinates = logCoordinates
26}
27
28public func format(logEntry: LogEntry) -> String {
29let timeStamp = NSLogLikeLogEntryTextFormatter.logDateFormatter.string(from: logEntry.timestamp)
30
31let filename = (logEntry.file as NSString).lastPathComponent
32
33// [LEVEL] 2018-03-29 19:05:01.994 <file:line> <coordinate1> [<coordinate2> [...]]: <mesage>
34var result = "[\(logEntry.verbosity.stringCode)] \(timeStamp)"
35
36if logLocation {
37result = result + " \(filename):\(logEntry.line)"
38}
39
40if logCoordinates, !logEntry.coordinates.isEmpty {
41result += " " + logEntry.coordinates.map { "\($0.stringValue)" }.joined(separator: " ")
42}
43result += ": " + logEntry.message
44return result
45}
46}
47
48extension LogEntryCoordinate {
49public var stringValue: String {
50var result = name
51if let value = value {
52result += ":\(value)"
53}
54return result
55}
56}
57