A custom logger implementation and TaskLocal helper for Swift-Log.
This package provides two and a half things (and a small bonus):
- TaskLocal support for
Loggerstruct - A custom (and a pretty one) log handler implementation
- Also it does
import Loggingso you don't have to (.mp4), just importLGNLogand it will work
As per this package Logger now has @TaskLocal var current property which does precisely what you think it does:
your app can call Logger.current in every place you can imagine to get a current logger, and you shouldn't bother
creating new temporary loggers here and there. By default it's just a simple logger with label default.
Sure enough, you can bind it to your configured logger for some async Task just like that:
var logger = Logger(label: "custom_label")
logger[metadataKey: "requestID"] = "\(UUID())"
logger[metadataKey: "FileLine"] = "main.swift:322"
Logger.$current.withValue(logger) {
Logger.current.info("hello")
}And there you have it.
Of course, default formatting isn't very pretty:
2021-10-23T17:51:14+0300 info custom_label : FileLine=main.swift:322 requestID=00000000-1637-0034-1711-000000000000 Hello
so this package comes with a prettier formatting. You can enable it by calling:
LoggingSystem.bootstrap(LGNLogger.init)Et voilĂ :
[2021-10-24 13:01:57 +0000 @ main.swift:322] [custom_label] [info] [00000000-1637-0034-1711-000000000000]: Hello (metadata: {"FileLine":"main.swift:322"})
Additionally, it has a few config vars:
LGNLogger.logLevel = .traceLGNLogger.hideTimezone = trueLGNLogger.hideLabel = trueLGNLogger.requestIDKey = "customRequestIDKey"