A simple wrapper of os.Logger
and OSLogStore
to log and export entries.
- Define your log types by conforming to
LogEvent
. It's preferable to group them into anenum
:
enum LogType: LogEventType {
case app
case api
var category: String {
switch self {
case .app:
return "App"
case .api:
return "API"
}
}
}
- Create a logger for your app:
let log = JourneyLog<LogType>(identifier: "identifier")
The provided "identifier" will identify your log as a subsystem in Apple's unified logging system. Typically, you use the same value as your app’s bundle ID, for example, com.yourcompany.yourapp
.
- Start logging:
log.info(.app, "App launches")
log.warning(.app, "Feature is not implemented")
let url = URL(string: "www.example.com")!
log.debug(.api, "Request: \(url)")
let error = APIError.dataNotFound
log.error(.api, "Request failed with error: \(error)")
It will show to Xcode Console:
- Export the log entries:
do {
let entries = try await log.exportEntries() // -> [LogEntry]
let logText = entries
.map {
"\(dateFormatter.string(from: $0.date)) \($0.process) [\($0.category)] \($0.composedMessage)"
}
} catch {
// Handle error
}
JourneyLog requires iOS from 15.0
and macOS from 12.0
.
You can add JourneyLog to an Xcode project as a package dependency.
If you want to use JourneyLog in a SwiftPM project, it's as simple as adding it to a dependencies clause in your Package.swift:
dependencies: [
.package(url: "https://github.com/Thieurom/JourneyLog", from: "0.1.0")
]