LGNLog
A custom logger implementation and TaskLocal helper for Swift-Log.
Why and how
This package provides two and a half things (and a small bonus):
- TaskLocal support for
Logger
struct - A custom (and a pretty one) log handler implementation
- Also it does
import Logging
so you don’t have to (.mp4), just importLGNLog
and it will work
TaskLocal
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.$current.withValue(logger) {
Logger.current.info("hello")
}
And there you have it.
Custom implementation
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-23 14:56:55 +0000 @ main.swift:322] [custom_label] [info]: Hello (metadata: {"FileLine":"main.swift:322","RequestID":"00000000-1637-0034-1711-000000000000"})
As a bonus, you can always change logging level:
LGNLogger.logLevel = .trace
and it will affect logging level of all loggers created after bootstrapping LGNLogger.