-
Notifications
You must be signed in to change notification settings - Fork 32
Closed
Description
Hey @sushichop 👋🏻
First of all, thank you for working on Puppy! Puppy is my go-to Swift logging lib, I really appreciate the amount of work you are investing in it.
Now to the issue. There is a section in the README, which shows how to use LogFormattable
. In this example, the date value is returned by the dateFormatter
function:
struct LogFormatter: LogFormattable {
func formatMessage(_ level: LogLevel, message: String, tag: String, function: String,
file: String, line: UInt, swiftLogInfo: [String : String],
label: String, date: Date, threadID: UInt64) -> String {
let date = dateFormatter(date)
let fileName = fileName(file)
let moduleName = moduleName(file)
return "\(date) \(threadID) [\(level.emoji) \(level)] \(swiftLogInfo) \(moduleName)/\(fileName)#L.\(line) \(function) \(message)".colorize(level.color)
}
}
dateFormatter
looks like this:
@Sendable
public func dateFormatter(_ date: Date, locale: String = "en_US_POSIX", dateFormat: String = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ", timeZone: String = "") -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: locale)
dateFormatter.dateFormat = dateFormat
dateFormatter.timeZone = TimeZone(identifier: timeZone)
return dateFormatter.string(from: date)
}
You see, calling the DateFormatter()
constructor is a really expensive operation, and in this case, it is being called on each log event. Performance can be greatly improved by pre-initialising the DateFormatter()
object, and injecting it into the formatMessage
function.
There are multiple ways to address this, would you like me to create a PR?
sushichop
Metadata
Metadata
Assignees
Labels
No labels