How can I construct custom loggers?

I'm looking to construct a custom logger API. For example:

log.verbose(Date(), "Begun process")

(That's contrived example, the point being some object and a message. I will then be manipulating both.)

Within my Log object I then want to transform the date value and the message into a final log message. "Nanoseconds: 9790324:: Begun process". If I was working with strings, it would be a pretty simple issue. But I'm not. It looks like Logger, which actually does the logging takes in a OSLogMessage which I cannot instantiate and cannot pass by value or reference. Even if I had just:

func verbose(_ date: Date, _ message: OSLogInterpolation) {
  log(date: Date, level: .info, message: message)
}

func info(_ date: Date, _ message: OSLogInterpolation) {
  log(date: Date, level: .default, message: message)
}

private func log(date: Date, level: OSLogType, message: OSLogInterpolation) {
 // Neither of these work
  log.log(level: level, message)
  log.log(level: level, OSLogMessage(interpolation: message))
}

Ideally what I'd like is:

private func log(date: Date, level: OSLogType, message: OSLogInterpolation) {
  let interpolation = "Nanoseconds: \(date.getNanoseconds):: " + message
  log.log(level: level, OSLogMessage(interpolation: interpolation))
}

With the idea being that OSLogMessage is still respecting any privacy notices on the interpolation.

I’m confused by your goals here. The system log already includes high-resolution timestamps in all log entries. Why do you want to add an additional one?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The Logger type is seriously intertwined with the compiler and, as such, is not suitable for this purpose.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How can I construct custom loggers?
 
 
Q