coenttb/swift-logging-extras
A Swift package for integrating swift-logging with swift-dependencies.
Overview
This package extends Apple's swift-log with integration for Point-Free's Dependencies library, providing dependency injection support for Logger instances. It includes automatic test logger configuration and enhanced logging methods with metadata support.
Features
- Dependencies integration for Logger instances
- Automatic test logger configuration with process name
- Enhanced logging method with file and line metadata
- TestDependencyKey conformance for Logger
- Type-safe dependency access via @Dependency property wrapper
Installation
Swift Package Manager
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/coenttb/swift-logging-extras", from: "0.0.1")
]Then add LoggingExtras to your target dependencies:
targets: [
.target(
name: "YourTarget",
dependencies: [
.product(name: "LoggingExtras", package: "swift-logging-extras")
]
)
]Quick Start
import LoggingExtras
import Dependencies
struct MyFeature {
@Dependency(\.logger) var logger
func doSomething() {
logger.info("Starting operation")
// ... your code ...
logger.debug("Operation completed")
}
}Usage
Basic Usage with Dependencies
Access the logger through the Dependencies system:
import LoggingExtras
import Dependencies
struct MyService {
@Dependency(\.logger) var logger
func performTask() {
logger.info("Task started")
logger.debug("Processing...")
logger.notice("Task completed")
}
}Test Usage
In tests, the logger is automatically configured with the process name:
import LoggingExtras
import DependenciesTestSupport
import Testing
@Test
func testLogging() async throws {
try await withDependencies { _ in
// Logger is automatically set to test value
} operation: {
@Dependency(\.logger) var logger
logger.info("Test message") // Will include process name
}
}Custom Logger Configuration
Override the logger dependency for specific features:
import LoggingExtras
import Dependencies
import DependenciesTestSupport
@Test
func testWithCustomLogger() async throws {
try await withDependencies {
$0.logger = Logger(label: "com.example.myapp.feature")
} operation: {
@Dependency(\.logger) var logger
logger.info("Using custom logger")
}
}Enhanced Logging with Metadata
Use the enhanced logging method that automatically adds file and line metadata:
import LoggingExtras
import Dependencies
import Logging
struct MyFeature {
@Dependency(\.logger) var logger
func processUser(id: String) {
// Automatically includes file and line information in metadata
logger.log(
.info,
"Processing user",
metadata: ["userId": "\(id)"]
)
}
}API Reference
DependencyValues Extension
extension DependencyValues {
public var logger: Logger { get set }
}Access the logger through the Dependencies system.
Logger Extension
extension Logger: TestDependencyKey {
public static let testValue = Logger(label: ProcessInfo.processInfo.processName)
}Provides automatic test logger configuration.
Enhanced Logging Method
extension Logger {
public func log(
_ level: Logger.Level,
_ message: @autoclosure () -> Logger.Message,
metadata: Logger.Metadata? = nil,
file: String = #file,
function: String = #function,
line: UInt = #line
)
}Enhanced logging method that merges provided metadata with file and line information.
Requirements
- Swift 6.0+
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
License
This package is released under the Apache 2.0 license. See LICENSE for details.
Contributing
Contributions are welcome. Please open an issue or pull request on GitHub.
Package Metadata
Repository: coenttb/swift-logging-extras
Homepage: https://coenttb.com
Stars: 1
Forks: 0
Open issues: 2
Default branch: main
Primary language: swift
License: Apache-2.0
Topics: dependencies, logging, observability, swift, utilities
README: README.md