mirror of
https://github.com/apple/swift-log
synced 2026-11-23 03:44:22 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c6fb14227 | ||
|
|
53d00853e6 | ||
|
|
c5ece24786 | ||
|
|
172948a4b5 | ||
|
|
47a917767f | ||
|
|
f054da4c19 |
No files matched your search
@@ -56,10 +56,10 @@ jobs:
|
||||
swift_6_2_enabled: true
|
||||
swift_6_3_enabled: true
|
||||
macos_xcode_test_enabled: true
|
||||
ios_xcode_test_enabled: true
|
||||
watchos_xcode_test_enabled: true
|
||||
tvos_xcode_test_enabled: true
|
||||
visionos_xcode_test_enabled: true
|
||||
ios_xcode_test_enabled: false
|
||||
watchos_xcode_test_enabled: false
|
||||
tvos_xcode_test_enabled: false
|
||||
visionos_xcode_test_enabled: false
|
||||
|
||||
wasm:
|
||||
name: Wasm Swift SDK
|
||||
|
||||
@@ -62,10 +62,10 @@ jobs:
|
||||
swift_6_2_enabled: true
|
||||
swift_6_3_enabled: true
|
||||
macos_xcode_test_enabled: true
|
||||
ios_xcode_test_enabled: true
|
||||
watchos_xcode_test_enabled: true
|
||||
tvos_xcode_test_enabled: true
|
||||
visionos_xcode_test_enabled: true
|
||||
ios_xcode_test_enabled: false
|
||||
watchos_xcode_test_enabled: false
|
||||
tvos_xcode_test_enabled: false
|
||||
visionos_xcode_test_enabled: false
|
||||
|
||||
android:
|
||||
name: Android Swift SDK
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 734,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 734,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 734,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 759,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 734,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 734,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 734,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 1993,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 1968,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 1050,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 1050,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 1046,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 5935,
|
||||
"instructions" : 6200,
|
||||
"objectAllocCount" : 2
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 7151,
|
||||
"instructions" : 7600,
|
||||
"objectAllocCount" : 3
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 1005,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 1017,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 1966,
|
||||
"objectAllocCount" : 0
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 4551,
|
||||
"objectAllocCount" : 1
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"instructions" : 9623,
|
||||
"objectAllocCount" : 4
|
||||
}
|
||||
}
|
||||
@@ -67,3 +67,8 @@ The community has built numerous specialized logging backends.
|
||||
A great way to discover available log backend implementations is searching the
|
||||
[Swift Package Index](https://swiftpackageindex.com/search?query=swift-log)
|
||||
for the `swift-log` keyword.
|
||||
|
||||
## Building with CMake
|
||||
|
||||
While Swift Package Manager (SPM) is the primary build system for development and testing, CMake build support is also available for integrated or mixed build environments if needed. Note that tests are not included in the CMake build, they are managed through Swift Package Manager with `swift test`.
|
||||
|
||||
@@ -345,8 +345,13 @@ internal struct StdioOutputStream: TextOutputStream, @unchecked Sendable {
|
||||
)
|
||||
#elseif canImport(WASILibc)
|
||||
// no file locking on WASI
|
||||
#if compiler(>=6.5)
|
||||
let file = WASILibc.stderr
|
||||
#else
|
||||
let file = WASILibc.stderr!
|
||||
#endif
|
||||
return StdioOutputStream(
|
||||
file: WASILibc.stderr!,
|
||||
file: file,
|
||||
flushMode: .always,
|
||||
lock: nil,
|
||||
unlock: nil,
|
||||
@@ -417,8 +422,13 @@ internal struct StdioOutputStream: TextOutputStream, @unchecked Sendable {
|
||||
)
|
||||
#elseif canImport(WASILibc)
|
||||
// no file locking on WASI
|
||||
#if compiler(>=6.5)
|
||||
let file = WASILibc.stdout
|
||||
#else
|
||||
let file = WASILibc.stdout!
|
||||
#endif
|
||||
return StdioOutputStream(
|
||||
file: WASILibc.stdout!,
|
||||
file: file,
|
||||
flushMode: .always,
|
||||
lock: nil,
|
||||
unlock: nil,
|
||||
|
||||
@@ -280,6 +280,10 @@ extension LogHandler {
|
||||
}
|
||||
|
||||
/// A default implementation for the SwiftLog 1.0 log message handler.
|
||||
///
|
||||
/// Forwards to ``log(event:)`` directly. Forwarding to the source-carrying deprecated
|
||||
/// method closed a cycle with its own default, so a handler implementing only
|
||||
/// ``log(event:)`` overflowed the stack when called through this entry point.
|
||||
@available(*, deprecated, renamed: "log(event:)")
|
||||
public func log(
|
||||
level: Logging.Logger.Level,
|
||||
@@ -290,13 +294,15 @@ extension LogHandler {
|
||||
line: UInt
|
||||
) {
|
||||
self.log(
|
||||
level: level,
|
||||
message: message,
|
||||
metadata: metadata,
|
||||
source: Logger.currentModule(filePath: file),
|
||||
file: file,
|
||||
function: function,
|
||||
line: line
|
||||
event: LogEvent(
|
||||
level: level,
|
||||
message: message,
|
||||
metadata: metadata,
|
||||
source: Logger.currentModule(filePath: file),
|
||||
file: file,
|
||||
function: function,
|
||||
line: line
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,7 +262,9 @@ extension Logger.MetadataValue {
|
||||
///
|
||||
/// **Setting:** Replaces the value with `.stringConvertible(AttributedStringCarrier(...))`
|
||||
/// carrying the given attributes. For `.string` and `.stringConvertible` cases the string
|
||||
/// representation is preserved. For `.dictionary` and `.array` cases the setter is a no-op.
|
||||
/// representation is preserved. Setting attributes on a `.dictionary` or `.array` value is a
|
||||
/// programmer error: it triggers an assertion in debug builds and leaves the value unchanged in
|
||||
/// release builds. Set attributes on the leaf values instead.
|
||||
///
|
||||
/// Only handlers that inspect attributes pay the cost of the getter check.
|
||||
@inlinable
|
||||
|
||||
@@ -66,6 +66,79 @@ struct CompatibilityTest {
|
||||
testLogging.history.assertExist(level: .error, message: "yes: error", source: "yes: error source")
|
||||
testLogging.history.assertExist(level: .critical, message: "yes: critical", source: "yes: critical source")
|
||||
}
|
||||
|
||||
/// A handler implementing only `log(event:)` must survive a caller on the SwiftLog 1.0
|
||||
/// entry point. Both deprecated defaults used to forward to each other.
|
||||
@available(*, deprecated, message: "Testing deprecated functionality")
|
||||
@Test func swiftLog1CompatibilityMethodReachesAnEventOnlyHandler() {
|
||||
let handler = EventOnlyLogHandler()
|
||||
|
||||
handler.log(
|
||||
level: .warning,
|
||||
message: "compat",
|
||||
metadata: ["k": "v"],
|
||||
file: "/Sources/CompatModule/Caller.swift",
|
||||
function: #function,
|
||||
line: 42
|
||||
)
|
||||
|
||||
let events = handler.recorded
|
||||
#expect(events.count == 1)
|
||||
#expect(events.first?.level == .warning)
|
||||
#expect(events.first?.message == "compat")
|
||||
#expect(events.first?.metadata == ["k": "v"])
|
||||
#expect(events.first?.source == "CompatModule")
|
||||
#expect(events.first?.line == 42)
|
||||
}
|
||||
|
||||
/// The same entry point must still reach a handler implementing only the
|
||||
/// source-carrying deprecated method.
|
||||
@available(*, deprecated, message: "Testing deprecated functionality")
|
||||
@Test func swiftLog1CompatibilityMethodReachesASourceOnlyHandler() {
|
||||
let testLogging = NewerButStillOldTestLogging()
|
||||
let handler = testLogging.make(label: "compat")
|
||||
|
||||
handler.log(
|
||||
level: .error,
|
||||
message: "compat",
|
||||
metadata: nil,
|
||||
file: "/Sources/CompatModule/Caller.swift",
|
||||
function: #function,
|
||||
line: 42
|
||||
)
|
||||
|
||||
testLogging.history.assertExist(level: .error, message: "compat", source: "CompatModule")
|
||||
}
|
||||
}
|
||||
|
||||
/// Implements `log(event:)` and nothing else.
|
||||
private struct EventOnlyLogHandler: LogHandler {
|
||||
private final class Store: @unchecked Sendable {
|
||||
private let lock = Lock()
|
||||
private var events: [LogEvent] = []
|
||||
func append(_ event: LogEvent) {
|
||||
self.lock.withLock { self.events.append(event) }
|
||||
}
|
||||
var all: [LogEvent] {
|
||||
self.lock.withLock { self.events }
|
||||
}
|
||||
}
|
||||
|
||||
private let store = Store()
|
||||
|
||||
var metadata: Logger.Metadata = [:]
|
||||
var logLevel: Logger.Level = .trace
|
||||
|
||||
subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
|
||||
get { self.metadata[metadataKey] }
|
||||
set { self.metadata[metadataKey] = newValue }
|
||||
}
|
||||
|
||||
func log(event: LogEvent) {
|
||||
self.store.append(event)
|
||||
}
|
||||
|
||||
var recorded: [LogEvent] { self.store.all }
|
||||
}
|
||||
|
||||
private struct OldSchoolTestLogging {
|
||||
|
||||
Reference in new issue
Block a user