Log
is a lightweight logging framework written in Swift.
First, you need to create an instance of IPrinter
that prints messages to a specific output, such as XCode's console or the Console
app.
The log
package provides predefined printers for printing messages in the XCode console (ConsolePrinter
) and the system console (OSPrinter
). You can also create your own printer. To do this, your object must conform to IPrinterStrategy
and implement the necessary methods.
import Log
let osPrinter = OSPrinter()
let consolePrinter = ConsolePrinter()
Second, create a Logger
instance and pass these printers as initialization parameters while defining a log level. The log level determines the level of log messages to print. If the log level is set to a specific level, all messages with different log levels will be ignored. To print all messages, use .all
.
let log = Logger(
printers: [osPrinter, consolePrinter],
logLevel: .all
)
log.error(message: "test message")
Each instance of IPrinter
has an array of formatters that are responsible for formatting input messages. The log
package provides predefined prefix and timestamp formatters. To use these, you need to pass them to an initializer of a printer.
let osPrinter = OSPrinter(formatters: [PrefixFormatter(name: "your prefix here")])
...
log.fault(message: "message") // "🚨🚨🚨 [your prefix here] => message"
Here is a list of predefined formatters:
Formatters | Description |
---|---|
PrefixLogFormatter | Add a specified prefix to a printed message |
TimestampLogFormatter | Add a timestamp before a printed message based on a date format |
If you want to create a custom message formatter, your object must conform to the ILogFormatter
protocol and implement the necessary methods.
struct MyCustomMessageFormatter: ILogFormatter {
func format(message: String, with logLevel: LogLevel) -> String {
// your implementation here
}
}
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 7.0+ / visionOS 1.0+
- Xcode 14.0
- Swift 5.7
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler. It is in early development, but log
does support its use on supported platforms.
Once you have your Swift package set up, adding log
as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/space-code/log.git", .upToNextMajor(from: "1.0.0"))
]
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
Bootstrapping development environment
make bootstrap
Please feel free to help out with this project! If you see something that could be made better or want a new feature, open up an issue or send a Pull Request!
Nikita Vasilev, nv3212@gmail.com
log is available under the MIT license. See the LICENSE file for more info.