https://github.com/rightpoint/rplogging
Log level settings for iOS, macOS, tvOS, & watchOS
https://github.com/rightpoint/rplogging
Last synced: about 1 year ago
JSON representation
Log level settings for iOS, macOS, tvOS, & watchOS
- Host: GitHub
- URL: https://github.com/rightpoint/rplogging
- Owner: Rightpoint
- License: mit
- Created: 2022-08-15T23:35:48.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-26T21:20:42.000Z (over 2 years ago)
- Last Synced: 2025-02-16T00:48:06.789Z (over 1 year ago)
- Language: Swift
- Size: 24.4 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Logging
Set a logging level to dictate priority of events logged
### Quick Start
By default, nothing will be logged, so you want to set a logging level during app set-up, before any loggable events. Different logging levels are often set for different build schemes (debug scheme may be `.verbose` while release might be `.warn`).
```swift
Log.logLevel = .warn
```
Use the level functions when logging to indicate what type of event has occurred
```swift
Log.verbose("Lower priority events will not be logged")
Log.error("Errors are higher priority than .warn, so will be logged")
```
Log levels (from highest to lowest priority):
- verbose
- debug
- info
- warn
- error
- off
A Trace is similar to a Log, but can be used to measure timing. Traces will also be logged as os_signposts which are viewable in Xcode Instruments.
```swift
Trace.begin("App Startup")
...
Trace.end("App Startup")
Trace.event("App moved to background")
```
## Advanced functionality
One or more LogHandlers may be added at both the instance level, or global level. A `LogHandler` may be used for example to send the log messages to the console, or to an analytics service. There is a `OSLogHandler` defined globally by default, which will send log output to the console or oslog if running iOS 14 or greater.
```swift
public protocol LogHandler {
func handle(message: Log.Message)
}
// add a handler that will receive messages from ALL log instances.
Log.addGlobalHandler(MyLogHandler())
```
Log messages may be categorized by defining separate instances of the Log class.
```swift
final class Loggers {
static let userInterface = Log("UI", logLevel: .verbose)
static let app = Log("App")
static let network = Log("Network", logLevel: .error)
}
Loggers.userInterface.info("Button Pressed")
Loggers.network.error("Token Expired")
Loggers.app.warn("Out of Memory")
```
You may also include custom handlers per Log instance. Messages will still be reported to the global handler as well.
```swift
Loggers.network.addHandler(MyLogHandler())
```
Log level can also be represented by emoji instead of strings.
```swift
Log.useEmoji = true
```
Emoji key:
- .verbose = 📖
- .debug = 🐝
- .info = ✏️
- .warn = ⚠️
- .error = ⁉️