SquidLogger is a multicast logger written in Swift that send to multiple logs.
NetworkLogger.debug("hello world.")
PaymentLogger.info("hello world.")
Output
π debug [Network] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - hello world.
π‘ info [Payment] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - hello world.
My project contains multiple logs. (e.g.: Crashlytics logging, Xcode console, our log server)
I don't want to write the same code. Like the next.
let text = "Fail to fetching tha User data."
Crashlytics.log(text)
print(text)
OurLogServer.error(text)
I want to control logging per modules. Like the next.
- log to Networking is warning or more.
- log to Payment is debug or more.
Therefore I needed this logger.
Initialize and default logger. Log stream is only default console log stream if SquidLoggerManager.configure() arguments empty when initialize.
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Initialize code
SquidLoggerManager.configure()
// Each logs with log level
IkaLogger.debug("hello world.")
IkaLogger.info("hello world.")
IkaLogger.warn("hello world.")
IkaLogger.error("hello world.")
IkaLogger.fatal("hello world.")
return true
}
}
Output
π info [Default] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - hello world.
π‘ info [Default] 2018-03-24 03:38:02.494 AppDelegate.swift(19) application(_:didFinishLaunchingWithOptions:) - hello world.
β οΈ warn [Default] 2018-03-24 03:38:02.496 AppDelegate.swift(20) application(_:didFinishLaunchingWithOptions:) - hello world.
π« error [Default] 2018-03-24 03:38:02.496 AppDelegate.swift(21) application(_:didFinishLaunchingWithOptions:) - hello world.
π fatal [Default] 2018-03-24 03:38:02.496 AppDelegate.swift(22) application(_:didFinishLaunchingWithOptions:) - hello world.
Attention This is sample so I don't recommend you. Because this library is a multicast logger and not a logger. So confirm below.
SquidLoggerManager.defaultLogLevel
will apply to all log streams.
SquidLoggerManager.configureLogLevel(to: .info)
SquidLoggerManager.defaultLogLevel = .warn
At first, implement a log stream.
struct ConsoleLogger: SquidLogStreaming {
public func print<T>(_ object: T) {
Swift.print(object)
}
}
Next, add stream to SquidLoggerManager.
SquidLoggerManager.configure(streams: [ConsoleLogger()])
Attention Will remove default log stream from managed log streams if added any new log stream.
A log stream(ConsoleLogger
) has already been implemented.
SquidLoggerManager.configureLogLevel(to: .error, at: ConsoleLogger.self)
You just implement below if you want to use for networking area.
struct NetworkLogger: SquidLogger {
static var category: String = "Network"
static var logLevel: SquidLogLevel?
}
You will just call a NetworkLogger.
NetworkLogger.info("User data fetch completed")
Attention SquidLogger isn't thread safe. Because this library primary purpose is multicast. logging is not important purpose.
struct NetworkLogger: SquidLogger {
static var category: String = "Network"
static var logLevel: SquidLogLevel? = .debug
}
NetworkLogger.logLevel = .error
At first, Implement symboler.
struct MyLogLevelSymbol: SquidLogLevelSymbol {
func toSymbol(from level: SquidLogLevel) -> String {
switch level {
case .info: return "I"
case .debug: return "D"
case .warn: return "W"
case .error: return "E"
case .fatal: return "F"
case .none: return ""
}
}
}
configure your symboler instance.
SquidLoggerManager.logLevelSymboler = MyLogLevelSymbol()
struct MyTextFormatter: SquidLogTextFormatter {
func format<T>(_ params: SquidLogParameter<T>) -> String {
return "\(params.object)"
}
}
SquidLoggerManager.textFormatter = MyTextFormatter()
implement SquidLogStreaming.makeLogText
method.
struct ConsoleLogger: SquidLogStreaming {
func makeLogText<T>(_ params: SquidLogParameter<T>) -> String? {
return "\(params.level):\(params.object)"
}
}
Implement SquidLogStreaming.filterLogText
method.
struct ConsoleLogger: SquidLogStreaming {
func filterLogText(_ text: String) -> String {
guard let regex = try? NSRegularExpression(pattern: "hello", options: []) else { return text }
let range = NSRange(location: 0, length: text.count)
return regex.stringByReplacingMatches(in: text, options: [], range: range, withTemplate: "<FILTERED>")
}
}
IkaLogger.warn("hello world.")
π warn [Default] 2018-03-24 03:38:02.494 AppDelegate.swift(18) application(_:didFinishLaunchingWithOptions:) - <FILTERED> world.
Final Level | SquidLogManager. defaultLogLevel |
SquidLogger. logLevel |
SquidLogStreaming. logLevel |
---|---|---|---|
debug | debug | nil | nil |
info | debug | info | nil |
warn | debug | nil | warn |
warn | debug | info | warn |
error | error | nil | nil |
info | error | info | nil |
warn | error | nil | warn |
warn | error | info | warn |
- Swift 3.2 or later
- iOS 9 or later
- Xcode 9.2 or later
- Don't forget primary purpose.
- Lightweight