forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.swift
More file actions
138 lines (118 loc) · 3.37 KB
/
Logger.swift
File metadata and controls
138 lines (118 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftSyntax
import ArgumentParser
import JavaKitConfigurationShared
// Placeholder for some better logger, we could depend on swift-log
public struct Logger {
public var label: String
public var logLevel: Logger.Level
public init(label: String, logLevel: Logger.Level) {
self.label = label
self.logLevel = logLevel
}
public func warning(
_ message: @autoclosure () -> String,
metadata: [String: Any] = [:],
file: String = #fileID,
line: UInt = #line,
function: String = #function
) {
guard logLevel <= .warning else {
return
}
let metadataString: String =
if metadata.isEmpty { "" } else { "\(metadata)" }
print("[warning][\(file):\(line)](\(function)) \(message()) \(metadataString)")
}
public func info(
_ message: @autoclosure () -> String,
metadata: [String: Any] = [:],
file: String = #fileID,
line: UInt = #line,
function: String = #function
) {
guard logLevel <= .info else {
return
}
let metadataString: String =
if metadata.isEmpty { "" } else { "\(metadata)" }
print("[info][\(file):\(line)](\(function)) \(message()) \(metadataString)")
}
public func debug(
_ message: @autoclosure () -> String,
metadata: [String: Any] = [:],
file: String = #fileID,
line: UInt = #line,
function: String = #function
) {
guard logLevel <= .debug else {
return
}
let metadataString: String =
if metadata.isEmpty { "" } else { "\(metadata)" }
print("[debug][\(file):\(line)](\(function)) \(message()) \(metadataString)")
}
public func trace(
_ message: @autoclosure () -> String,
metadata: [String: Any] = [:],
file: String = #fileID,
line: UInt = #line,
function: String = #function
) {
guard logLevel <= .trace else {
return
}
let metadataString: String =
metadata.isEmpty ? "" : "\(metadata)"
print("[trace][\(file):\(line)](\(function)) \(message()) \(metadataString)")
}
}
extension Logger {
public typealias Level = JavaKitConfigurationShared.LogLevel
}
extension Logger.Level: ExpressibleByArgument {
public var defaultValueDescription: String {
"log level"
}
public private(set) static var allValueStrings: [String] =
["trace", "debug", "info", "notice", "warning", "error", "critical"]
public private(set) static var defaultCompletionKind: CompletionKind = .default
}
extension Logger.Level {
var naturalIntegralValue: Int {
switch self {
case .trace:
return 0
case .debug:
return 1
case .info:
return 2
case .notice:
return 3
case .warning:
return 4
case .error:
return 5
case .critical:
return 6
}
}
}
extension Logger.Level: Comparable {
public static func < (lhs: Logger.Level, rhs: Logger.Level) -> Bool {
return lhs.naturalIntegralValue < rhs.naturalIntegralValue
}
}