mirror of
https://github.com/apple/swift-log
synced 2026-11-22 03:40:39 +00:00
56 lines
1.9 KiB
Swift
56 lines
1.9 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Logging API open source project
|
|
//
|
|
// Copyright (c) 2018-2019 Apple Inc. and the Swift Logging API project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// THIS IS NOT PART OF THE PITCH, JUST AN EXAMPLE HOW A LOGGER IMPLEMENTATION LOOKS LIKE
|
|
//
|
|
|
|
import Foundation
|
|
import Logging
|
|
|
|
// this is a contrived example of a logging library implementation that writes logs to stdout
|
|
public struct SimpleLogHandler: LogHandler {
|
|
private var handler: CommonLogHandler
|
|
private let defaultLogLevel: Logger.Level
|
|
|
|
public init(label: String) {
|
|
self.init(label: label, defaultLogLevel: .info)
|
|
}
|
|
|
|
public init(label: String, defaultLogLevel: Logger.Level) {
|
|
self.handler = CommonLogHandler(label: label)
|
|
self.defaultLogLevel = defaultLogLevel
|
|
}
|
|
|
|
public func log(level: Logger.Level, message: String, metadata: Logger.Metadata?, file: StaticString, function: StaticString, line: UInt) {
|
|
self.handler.log(level: level, message: message, metadata: metadata) { text in
|
|
print(text)
|
|
}
|
|
}
|
|
|
|
public var logLevel: Logger.Level {
|
|
get { return self.handler.logLevel ?? self.defaultLogLevel }
|
|
set { self.handler.logLevel = newValue }
|
|
}
|
|
|
|
public var metadata: Logger.Metadata {
|
|
get { return self.handler.metadata }
|
|
set { self.handler.metadata = newValue }
|
|
}
|
|
|
|
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
|
|
get { return self.handler[metadataKey: metadataKey] }
|
|
set { self.handler[metadataKey: metadataKey] = newValue }
|
|
}
|
|
}
|