-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from tkausch/InsertLogging
Use "swift-log" to introduce platform independent logging support
- Loading branch information
Showing
12 changed files
with
210 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// | ||
// OSLogHandler.swift | ||
// | ||
// This File belongs to SwiftRestRequests | ||
// Copyright © 2024 Thomas Kausch. | ||
// All Rights Reserved. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
|
||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
import Foundation | ||
import Logging | ||
import struct Logging.Logger | ||
|
||
#if os(Linux) | ||
// this package is not available for Linux - as it is using Apple os module | ||
#else | ||
|
||
import os | ||
|
||
public struct OSLogHandler: LogHandler { | ||
public var logLevel: Logger.Level = .info | ||
public let label: String | ||
private let oslogger: OSLog | ||
|
||
public init(label: String) { | ||
self.label = label | ||
self.oslogger = OSLog(subsystem: label, category: "") | ||
} | ||
|
||
public init(label: String, log: OSLog) { | ||
self.label = label | ||
self.oslogger = log | ||
} | ||
|
||
public func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) { | ||
var combinedPrettyMetadata = self.prettyMetadata | ||
if let metadataOverride = metadata, !metadataOverride.isEmpty { | ||
combinedPrettyMetadata = self.prettify( | ||
self.metadata.merging(metadataOverride) { | ||
return $1 | ||
} | ||
) | ||
} | ||
|
||
var formedMessage = message.description | ||
if combinedPrettyMetadata != nil { | ||
formedMessage += " -- " + combinedPrettyMetadata! | ||
} | ||
os_log("%{public}@", log: self.oslogger, type: OSLogType.from(loggerLevel: level), formedMessage as NSString) | ||
} | ||
|
||
private var prettyMetadata: String? | ||
public var metadata = Logger.Metadata() { | ||
didSet { | ||
self.prettyMetadata = self.prettify(self.metadata) | ||
} | ||
} | ||
|
||
/// Add, remove, or change the logging metadata. | ||
/// - parameters: | ||
/// - metadataKey: the key for the metadata item. | ||
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? { | ||
get { | ||
return self.metadata[metadataKey] | ||
} | ||
set { | ||
self.metadata[metadataKey] = newValue | ||
} | ||
} | ||
|
||
private func prettify(_ metadata: Logger.Metadata) -> String? { | ||
if metadata.isEmpty { | ||
return nil | ||
} | ||
return metadata.map { | ||
"\($0)=\($1)" | ||
}.joined(separator: " ") | ||
} | ||
} | ||
|
||
extension OSLogType { | ||
static func from(loggerLevel: Logger.Level) -> Self { | ||
switch loggerLevel { | ||
case .trace: | ||
/// `OSLog` doesn't have `trace`, so use `debug` | ||
return .debug | ||
case .debug: | ||
return .debug | ||
case .info: | ||
return .info | ||
case .notice: | ||
// https://developer.apple.com/documentation/os/logging/generating_log_messages_from_your_code | ||
// According to the documentation, `default` is `notice`. | ||
return .default | ||
case .warning: | ||
/// `OSLog` doesn't have `warning`, so use `info` | ||
return .info | ||
case .error: | ||
return .error | ||
case .critical: | ||
return .fault | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
Tests/SwiftRestRequestsTests/AbstractRestApiCallerTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// This File belongs to SwiftRestEssentials | ||
// Copyright © 2024 Thomas Kausch. | ||
// All Rights Reserved. | ||
|
||
import XCTest | ||
@testable import SwiftRestRequests | ||
|
||
|
||
import Logging | ||
|
||
|
||
class AbstractRestApiCallerTests: XCTestCase { | ||
|
||
// When available we prefere to use the OSLog | ||
static var onceExecution: () = { | ||
|
||
#if os(Linux) | ||
// Configure `swift-log`default logger | ||
#else | ||
/// Configure `swift-log` logging system to use OSLog backend | ||
LoggingSystem.bootstrap(OSLogHandler.init) | ||
#endif | ||
|
||
Logger.securityLogger.logLevel = .trace | ||
Logger.interceptorLogger.logLevel = .trace | ||
Logger.apiCallerLogger.logLevel = .trace | ||
|
||
}() | ||
|
||
|
||
override func setUp() { | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
let _ = AbstractRestApiCallerTests.onceExecution | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.