CommandLineToolkit

Форк
0
/
NSLogLikeLogEntryTextFormatter.swift 
56 строк · 1.6 Кб
1
/*
2
 * Copyright (c) Avito Tech LLC
3
 */
4

5
import Foundation
6

7
public final class NSLogLikeLogEntryTextFormatter: LogEntryTextFormatter {
8
    
9
    // 2018-03-29 19:05:01.994
10
    public static let logDateFormatter: DateFormatter = {
11
        let logFormatter = DateFormatter()
12
        logFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
13
        logFormatter.timeZone = TimeZone.autoupdatingCurrent
14
        return logFormatter
15
    }()
16
    
17
    private let logLocation: Bool
18
    private let logCoordinates: Bool
19
    
20
    public init(
21
        logLocation: Bool,
22
        logCoordinates: Bool
23
    ) {
24
        self.logLocation = logLocation
25
        self.logCoordinates = logCoordinates
26
    }
27
    
28
    public func format(logEntry: LogEntry) -> String {
29
        let timeStamp = NSLogLikeLogEntryTextFormatter.logDateFormatter.string(from: logEntry.timestamp)
30
        
31
        let filename = (logEntry.file as NSString).lastPathComponent
32
        
33
        // [LEVEL] 2018-03-29 19:05:01.994 <file:line> <coordinate1> [<coordinate2> [...]]: <mesage>
34
        var result = "[\(logEntry.verbosity.stringCode)] \(timeStamp)"
35
        
36
        if logLocation {
37
            result = result + " \(filename):\(logEntry.line)"
38
        }
39
        
40
        if logCoordinates, !logEntry.coordinates.isEmpty {
41
            result += " " + logEntry.coordinates.map { "\($0.stringValue)" }.joined(separator: " ")
42
        }
43
        result += ": " + logEntry.message
44
        return result
45
    }
46
}
47

48
extension LogEntryCoordinate {
49
    public var stringValue: String {
50
        var result = name
51
        if let value = value {
52
            result += ":\(value)"
53
        }
54
        return result
55
    }
56
}
57

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.