|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Logging API open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Apple Inc. and the Swift Logging API project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift Logging API project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +public struct LoggerWithSource { |
| 16 | + public var logger: Logger |
| 17 | + |
| 18 | + public var source: String |
| 19 | + |
| 20 | + @inlinable |
| 21 | + public init(_ logger: Logger, source: String) { |
| 22 | + self.logger = logger |
| 23 | + self.source = source |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +extension LoggerWithSource { |
| 28 | + /// Log a message passing the log level as a parameter. |
| 29 | + /// |
| 30 | + /// If the `logLevel` passed to this method is more severe than the `Logger`'s `logLevel`, it will be logged, |
| 31 | + /// otherwise nothing will happen. |
| 32 | + /// |
| 33 | + /// - parameters: |
| 34 | + /// - level: The log level to log `message` at. For the available log levels, see `Logger.Level`. |
| 35 | + /// - message: The message to be logged. `message` can be used with any string interpolation literal. |
| 36 | + /// - metadata: One-off metadata to attach to this log message |
| 37 | + /// - file: The file this log message originates from (there's usually no need to pass it explicitly as it |
| 38 | + /// defaults to `#file`). |
| 39 | + /// - function: The function this log message originates from (there's usually no need to pass it explicitly as |
| 40 | + /// it defaults to `#function`). |
| 41 | + /// - line: The line this log message originates from (there's usually no need to pass it explicitly as it |
| 42 | + /// defaults to `#line`). |
| 43 | + @inlinable |
| 44 | + public func log(level: Logger.Level, |
| 45 | + _ message: @autoclosure () -> Logger.Message, |
| 46 | + metadata: @autoclosure () -> Logger.Metadata? = nil, |
| 47 | + file: String = #file, function: String = #function, line: UInt = #line) { |
| 48 | + self.logger.log(level: level, |
| 49 | + message(), |
| 50 | + metadata: metadata(), |
| 51 | + source: self.source, |
| 52 | + file: file, function: function, line: line) |
| 53 | + } |
| 54 | + |
| 55 | + /// Add, change, or remove a logging metadata item. |
| 56 | + /// |
| 57 | + /// - note: Logging metadata behaves as a value that means a change to the logging metadata will only affect the |
| 58 | + /// very `Logger` it was changed on. |
| 59 | + @inlinable |
| 60 | + public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? { |
| 61 | + get { |
| 62 | + return self.logger[metadataKey: metadataKey] |
| 63 | + } |
| 64 | + set { |
| 65 | + self.logger[metadataKey: metadataKey] = newValue |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Get or set the log level configured for this `Logger`. |
| 70 | + /// |
| 71 | + /// - note: `Logger`s treat `logLevel` as a value. This means that a change in `logLevel` will only affect this |
| 72 | + /// very `Logger`. It it acceptable for logging backends to have some form of global log level override |
| 73 | + /// that affects multiple or even all loggers. This means a change in `logLevel` to one `Logger` might in |
| 74 | + /// certain cases have no effect. |
| 75 | + @inlinable |
| 76 | + public var logLevel: Logger.Level { |
| 77 | + get { |
| 78 | + return self.logger.logLevel |
| 79 | + } |
| 80 | + set { |
| 81 | + self.logger.logLevel = newValue |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +extension LoggerWithSource { |
| 87 | + /// Log a message passing with the `Logger.Level.trace` log level. |
| 88 | + /// |
| 89 | + /// If `.trace` is at least as severe as the `Logger`'s `logLevel`, it will be logged, |
| 90 | + /// otherwise nothing will happen. |
| 91 | + /// |
| 92 | + /// - parameters: |
| 93 | + /// - message: The message to be logged. `message` can be used with any string interpolation literal. |
| 94 | + /// - metadata: One-off metadata to attach to this log message |
| 95 | + /// - file: The file this log message originates from (there's usually no need to pass it explicitly as it |
| 96 | + /// defaults to `#file`). |
| 97 | + /// - function: The function this log message originates from (there's usually no need to pass it explicitly as |
| 98 | + /// it defaults to `#function`). |
| 99 | + /// - line: The line this log message originates from (there's usually no need to pass it explicitly as it |
| 100 | + /// defaults to `#line`). |
| 101 | + @inlinable |
| 102 | + public func trace(_ message: @autoclosure () -> Logger.Message, |
| 103 | + metadata: @autoclosure () -> Logger.Metadata? = nil, |
| 104 | + file: String = #file, function: String = #function, line: UInt = #line) { |
| 105 | + self.logger.trace(message(), |
| 106 | + metadata: metadata(), |
| 107 | + source: self.source, |
| 108 | + file: file, |
| 109 | + function: function, |
| 110 | + line: line) |
| 111 | + } |
| 112 | + |
| 113 | + /// Log a message passing with the `Logger.Level.debug` log level. |
| 114 | + /// |
| 115 | + /// If `.debug` is at least as severe as the `Logger`'s `logLevel`, it will be logged, |
| 116 | + /// otherwise nothing will happen. |
| 117 | + /// |
| 118 | + /// - parameters: |
| 119 | + /// - message: The message to be logged. `message` can be used with any string interpolation literal. |
| 120 | + /// - metadata: One-off metadata to attach to this log message |
| 121 | + /// - file: The file this log message originates from (there's usually no need to pass it explicitly as it |
| 122 | + /// defaults to `#file`). |
| 123 | + /// - function: The function this log message originates from (there's usually no need to pass it explicitly as |
| 124 | + /// it defaults to `#function`). |
| 125 | + /// - line: The line this log message originates from (there's usually no need to pass it explicitly as it |
| 126 | + /// defaults to `#line`). |
| 127 | + @inlinable |
| 128 | + public func debug(_ message: @autoclosure () -> Logger.Message, |
| 129 | + metadata: @autoclosure () -> Logger.Metadata? = nil, |
| 130 | + file: String = #file, function: String = #function, line: UInt = #line) { |
| 131 | + self.logger.debug(message(), |
| 132 | + metadata: metadata(), |
| 133 | + source: self.source, |
| 134 | + file: file, |
| 135 | + function: function, |
| 136 | + line: line) |
| 137 | + } |
| 138 | + |
| 139 | + /// Log a message passing with the `Logger.Level.info` log level. |
| 140 | + /// |
| 141 | + /// If `.info` is at least as severe as the `Logger`'s `logLevel`, it will be logged, |
| 142 | + /// otherwise nothing will happen. |
| 143 | + /// |
| 144 | + /// - parameters: |
| 145 | + /// - message: The message to be logged. `message` can be used with any string interpolation literal. |
| 146 | + /// - metadata: One-off metadata to attach to this log message |
| 147 | + /// - file: The file this log message originates from (there's usually no need to pass it explicitly as it |
| 148 | + /// defaults to `#file`). |
| 149 | + /// - function: The function this log message originates from (there's usually no need to pass it explicitly as |
| 150 | + /// it defaults to `#function`). |
| 151 | + /// - line: The line this log message originates from (there's usually no need to pass it explicitly as it |
| 152 | + /// defaults to `#line`). |
| 153 | + @inlinable |
| 154 | + public func info(_ message: @autoclosure () -> Logger.Message, |
| 155 | + metadata: @autoclosure () -> Logger.Metadata? = nil, |
| 156 | + file: String = #file, function: String = #function, line: UInt = #line) { |
| 157 | + self.logger.info(message(), |
| 158 | + metadata: metadata(), |
| 159 | + source: self.source, |
| 160 | + file: file, |
| 161 | + function: function, |
| 162 | + line: line) |
| 163 | + } |
| 164 | + |
| 165 | + /// Log a message passing with the `Logger.Level.notice` log level. |
| 166 | + /// |
| 167 | + /// If `.notice` is at least as severe as the `Logger`'s `logLevel`, it will be logged, |
| 168 | + /// otherwise nothing will happen. |
| 169 | + /// |
| 170 | + /// - parameters: |
| 171 | + /// - message: The message to be logged. `message` can be used with any string interpolation literal. |
| 172 | + /// - metadata: One-off metadata to attach to this log message |
| 173 | + /// - file: The file this log message originates from (there's usually no need to pass it explicitly as it |
| 174 | + /// defaults to `#file`). |
| 175 | + /// - function: The function this log message originates from (there's usually no need to pass it explicitly as |
| 176 | + /// it defaults to `#function`). |
| 177 | + /// - line: The line this log message originates from (there's usually no need to pass it explicitly as it |
| 178 | + /// defaults to `#line`). |
| 179 | + @inlinable |
| 180 | + public func notice(_ message: @autoclosure () -> Logger.Message, |
| 181 | + metadata: @autoclosure () -> Logger.Metadata? = nil, |
| 182 | + file: String = #file, function: String = #function, line: UInt = #line) { |
| 183 | + self.logger.notice(message(), |
| 184 | + metadata: metadata(), |
| 185 | + source: self.source, |
| 186 | + file: file, |
| 187 | + function: function, |
| 188 | + line: line) |
| 189 | + } |
| 190 | + |
| 191 | + /// Log a message passing with the `Logger.Level.warning` log level. |
| 192 | + /// |
| 193 | + /// If `.warning` is at least as severe as the `Logger`'s `logLevel`, it will be logged, |
| 194 | + /// otherwise nothing will happen. |
| 195 | + /// |
| 196 | + /// - parameters: |
| 197 | + /// - message: The message to be logged. `message` can be used with any string interpolation literal. |
| 198 | + /// - metadata: One-off metadata to attach to this log message |
| 199 | + /// - file: The file this log message originates from (there's usually no need to pass it explicitly as it |
| 200 | + /// defaults to `#file`). |
| 201 | + /// - function: The function this log message originates from (there's usually no need to pass it explicitly as |
| 202 | + /// it defaults to `#function`). |
| 203 | + /// - line: The line this log message originates from (there's usually no need to pass it explicitly as it |
| 204 | + /// defaults to `#line`). |
| 205 | + @inlinable |
| 206 | + public func warning(_ message: @autoclosure () -> Logger.Message, |
| 207 | + metadata: @autoclosure () -> Logger.Metadata? = nil, |
| 208 | + file: String = #file, function: String = #function, line: UInt = #line) { |
| 209 | + self.logger.warning(message(), |
| 210 | + metadata: metadata(), |
| 211 | + source: self.source, |
| 212 | + file: file, |
| 213 | + function: function, |
| 214 | + line: line) |
| 215 | + } |
| 216 | + |
| 217 | + /// Log a message passing with the `Logger.Level.error` log level. |
| 218 | + /// |
| 219 | + /// If `.error` is at least as severe as the `Logger`'s `logLevel`, it will be logged, |
| 220 | + /// otherwise nothing will happen. |
| 221 | + /// |
| 222 | + /// - parameters: |
| 223 | + /// - message: The message to be logged. `message` can be used with any string interpolation literal. |
| 224 | + /// - metadata: One-off metadata to attach to this log message |
| 225 | + /// - file: The file this log message originates from (there's usually no need to pass it explicitly as it |
| 226 | + /// defaults to `#file`). |
| 227 | + /// - function: The function this log message originates from (there's usually no need to pass it explicitly as |
| 228 | + /// it defaults to `#function`). |
| 229 | + /// - line: The line this log message originates from (there's usually no need to pass it explicitly as it |
| 230 | + /// defaults to `#line`). |
| 231 | + @inlinable |
| 232 | + public func error(_ message: @autoclosure () -> Logger.Message, |
| 233 | + metadata: @autoclosure () -> Logger.Metadata? = nil, |
| 234 | + file: String = #file, function: String = #function, line: UInt = #line) { |
| 235 | + self.logger.error(message(), |
| 236 | + metadata: metadata(), |
| 237 | + source: self.source, |
| 238 | + file: file, |
| 239 | + function: function, |
| 240 | + line: line) |
| 241 | + } |
| 242 | + |
| 243 | + /// Log a message passing with the `Logger.Level.critical` log level. |
| 244 | + /// |
| 245 | + /// `.critical` messages will always be logged. |
| 246 | + /// |
| 247 | + /// - parameters: |
| 248 | + /// - message: The message to be logged. `message` can be used with any string interpolation literal. |
| 249 | + /// - metadata: One-off metadata to attach to this log message |
| 250 | + /// - file: The file this log message originates from (there's usually no need to pass it explicitly as it |
| 251 | + /// defaults to `#file`). |
| 252 | + /// - function: The function this log message originates from (there's usually no need to pass it explicitly as |
| 253 | + /// it defaults to `#function`). |
| 254 | + /// - line: The line this log message originates from (there's usually no need to pass it explicitly as it |
| 255 | + /// defaults to `#line`). |
| 256 | + @inlinable |
| 257 | + public func critical(_ message: @autoclosure () -> Logger.Message, |
| 258 | + metadata: @autoclosure () -> Logger.Metadata? = nil, |
| 259 | + file: String = #file, function: String = #function, line: UInt = #line) { |
| 260 | + self.logger.critical(message(), |
| 261 | + metadata: metadata(), |
| 262 | + source: self.source, |
| 263 | + file: file, |
| 264 | + function: function, |
| 265 | + line: line) |
| 266 | + } |
| 267 | +} |
0 commit comments