Dependency free Logger API for Swift.
Find a file
2019-11-09 12:40:15 +01:00
.swiftpm/xcode/xcshareddata/xcschemes Turn "gather code coverage" option on 2019-11-05 19:26:54 +01:00
Sources/SwiftLogger Convert Xcode project to Swift package 2019-11-05 18:37:11 +01:00
Tests Convert Xcode project to Swift package 2019-11-05 18:37:11 +01:00
.gitignore Update gitignore to take into account macOS files and directories 2019-11-05 18:22:27 +01:00
.travis.yml Enable code coverage when testing swift package 2019-11-07 21:58:15 +01:00
LICENSE Initial commit 2018-08-13 23:11:58 +02:00
Package.swift Convert Xcode project to Swift package 2019-11-05 18:37:11 +01:00
README.md Added build status and code coverage badges 2018-09-19 20:50:11 +02:00

SwiftLogger

Build Status codecov

Dependency free Logger API for Swift.

Motivation

Beside obvious benefit for unit testing (see NullLogger), having loose coupling to a logging framework makes it easy to log messages into multiple logging systems at once (see AgregateLogger).

Usage

Logger protocol provides simple interface utilizing log levels defined by The Syslog Protocol - RFC5424.

logger.log("message", level: .emergency)
logger.log("message", level: .alert)
logger.log("message", level: .critical)
logger.log("message", level: .error)
logger.log("message", level: .warning)
logger.log("message", level: .notice)
logger.log("message", level: .informational)
logger.log("message", level: .debug)

Log calls capture time, file name, method name and line number automatically. This way it is possible to compose log messages similar to the one presented below.

2018-08-31 18:29:34.748 <crit> SwiftFile:75 method() message

Integration with logger frameworks

You need to provide implementation for one method.

final class CustomLogger: Logger {
    
    public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) {
        /// compose message and forward it to a logging framework
    }
}

Provided loggers

ConsoleLogger writes messages into the standard output.

NullLogger ignores all messages with the intention to minimize observer effect. Useful for unit testing.

DiskLogger writes messages into the file at specified URL with log rotation support.

AgregateLogger forwards messages to all the loggers it is initialized with.