LGN-Log/README.md

2.2 KiB

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 import LGNLog 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[metadataKey: "FileLine"] = "main.swift:322"
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-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:

Sets a log level globally for all loggers initiated with this backend (.info by default):

LGNLogger.logLevel = .trace

Hides timezone from log message (saves a few bytes) (false by default)

LGNLogger.hideTimezone = true

Hides label from log message (false by default)

LGNLogger.hideLabel = true

Fetches request ID from Metadata and puts in preamble (requestID by default)

LGNLogger.requestIDKey = "customRequestIDKey"