mfb-technologies-inc/swiftybeaver
**Colorful**, flexible, **lightweight** logging for Swift 5 & **Swift 6**.
Installation
- For Swift 4 & 5 install the latest SwiftyBeaver version
- For Swift 3 install SwiftyBeaver 1.8.4
- For Swift 2 install SwiftyBeaver 0.7.0
Swift Package Manager
For Swift Package Manager add the following package to your Package.swift file. Just Swift 4 & 5 are supported:
.package(url: "https://github.com/SwiftyBeaver/SwiftyBeaver.git", .upToNextMajor(from: "2.0.0")),Disable swift-log integration to avoid adding it as a dependency.
Set the SWIFT_LOG_INTEGRATION environment variable to NO or FALSE for any commands that resolve package dependencies.
SWIFT_LOG_INTEGRATION=FALSE swift build
SWIFT_LOG_INTEGRATION=FALSE swift testUsage
SwiftyBeaver
Add that near the top of your AppDelegate.swift to be able to use SwiftyBeaver in your whole project.
import SwiftyBeaver
let log = SwiftyBeaver.self
At the beginning of your AppDelegate:didFinishLaunchingWithOptions() add the SwiftyBeaver log destinations (console, file, etc.), optionally adjust the log format and then you can already do the following log level calls globally:
// add log destinations. at least one is needed!
let console = ConsoleDestination() // log to Xcode Console
let file = FileDestination() // log to default swiftybeaver.log file
// use custom format and set console output to short time, log level & message
console.format = "$DHH:mm:ss$d $L $M"
// or use this for JSON output: console.format = "$J"
// In Xcode 15, specifying the logging method as .logger to display color, subsystem, and category information in the console.(Relies on the OSLog API)
console.logPrintWay = .logger(subsystem: "Main", category: "UI")
// If you prefer not to use the OSLog API, you can use print instead.
// console.logPrintWay = .print
// add the destinations to SwiftyBeaver
log.addDestination(console)
log.addDestination(file)
// Now let’s log!
log.verbose("not so important") // prio 1, VERBOSE in silver
log.debug("something to debug") // prio 2, DEBUG in green
log.info("a nice information") // prio 3, INFO in blue
log.warning("oh no, that won’t be good") // prio 4, WARNING in yellow
log.error("ouch, an error did occur!") // prio 5, ERROR in red
// log anything!
log.verbose(123)
log.info(-123.45678)
log.warning(Date())
log.error(["I", "like", "logs!"])
log.error(["name": "Mr Beaver", "address": "7 Beaver Lodge"])
// optionally add context to a log message
console.format = "$L: $M $X"
log.debug("age", context: 123) // "DEBUG: age 123"
log.info("my data", context: [1, "a", 2]) // "INFO: my data [1, \"a\", 2]"
Alternatively, if you are using SwiftUI, consider using the following setup:
import SwiftyBeaver
let logger = SwiftyBeaver.self
@main
struct yourApp: App {
init() {
let console = ConsoleDestination()
logger.addDestination(console)
// etc...
}
var body: some Scene {
WindowGroup {
}
}
}swift-log Integration with SwiftyBeaverSwiftLog
SwiftyBeaver can be used through swift-log with the SwiftyBeaverSwiftLog target.
In the same place you add your destinations to SwiftyBeaver you can boostratp LoggingSystem as shown:
import Logging
import SwiftyBeaver
import SwiftyBeaverSwiftLog
LoggingSystem.bootstrap { label in
SwiftyBeaverLogHandler(metadata: ["label": .string(label)])
}Server-side Swift
We ❤️ server-side Swift and SwiftyBeaver support it out-of-the-box! Try for yourself and run SwiftyBeaver inside a Ubuntu Docker container. Just install Docker and then go to your project folder on macOS or Ubuntu and type:
docker run --rm -it -v $PWD:/app swiftybeaver /bin/bash -c "cd /app ; swift build ; swift test"
# optionally log into container to run Swift CLI and do more stuff
docker run --rm -it --privileged=true -v $PWD:/app swiftybeaverBest: for the popular server-side Swift web framework Vapor you can use our Vapor logging provider which makes server logging awesome again 🙌
Documentation
Getting Started:
Logging Destinations:
Advanced Topics:
License
SwiftyBeaver Framework is released under the MIT License.
Package Metadata
Repository: mfb-technologies-inc/swiftybeaver
Default branch: main
README: README.md