LogKit is a Swift logging framework that provides a flexible, protocol-based approach to logging with support for multiple logging services, custom log entries, and structured metadata.
- 🔄 Asynchronous logging with Swift concurrency support
- 🎯 Protocol-based design for extensibility
- 🏷 Support for structured logging with metadata, tags, and labels
- 🔌 Multiple logging service support through a unified interface
- 🛡 Type-safe logging actions
- 💪 Written in Swift with full Sendable compliance for thread safety
[Add installation instructions based on your package manager]
The central coordinator that manages multiple logging services:
let logService = LogService()
await logService.register(myCustomLogger)The main protocol for implementing logging services:
public protocol LogKitServiceable: Sendable {
associatedtype Action: LogKitAction
associatedtype Entry: LogKitEntry
var id: LogKitIdentifier { get }
var handler: LogHandler { get }
var labels: [String: String] { get set}
var metadata: Logger.Metadata { get set }
func log(_ action: Action, entry: Entry) async throws
func log(_ entry: some DataProtocol, as _: Entry.Type) async throws
}Protocol for defining log entries with structured data:
public protocol LogKitEntry: Codable, Sendable {
var tags: [String]? { get set }
var timestamp: Date? { get set }
var message: String? { get set }
var source: String? { get set }
var file: String? { get set }
var function: String? { get set }
var line: UInt? { get set }
func log() async throws
}LogKit provides a comprehensive error handling system through LogKitError:
generic: General purpose errorsinvalidData: Data formatting or parsing errorsinvalidEntry: Log entry validation errorsinvalidService: Service configuration errorsmissingService: Service availability errors
// Define a custom log entry
struct MyLogEntry: LogKitEntry {
var tags: [String]?
var timestamp: Date?
var message: String?
var source: String?
var file: String?
var function: String?
var line: UInt?
func log() async throws {
// Implement logging logic
}
}
// Define a custom logging action
struct MyLogAction: LogKitAction {
let base: String
var description: String { base }
}
// Create and register a logging service
class MyLogService: LogKitServiceable {
let id: LogKitIdentifier = "my-logger"
let handler: LogHandler
func log(_ action: MyLogAction, entry: MyLogEntry) async throws {
// Implement logging logic
}
}
// Use the logging service
let service = LogService()
let logger = MyLogService()
try await service.register(logger)- Structured Logging: Use tags, labels, and metadata to organize your logs
- Error Handling: Always handle logging errors appropriately
- Async/Await: Take advantage of Swift's concurrency system for non-blocking logging
- Type Safety: Use custom
LogKitActiontypes to ensure type-safe logging operations
[Add contribution guidelines]
[Add license information]