-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
performance: convert codables to directory under a separate thread (#77)
- Loading branch information
Showing
4 changed files
with
78 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Foundation | ||
|
||
extension Logger { | ||
|
||
func debug<T: Codable>(_ message: LogMessage, _ object: T) { | ||
Task { [weak self] in | ||
let logDetails: LogDetails? = await self?.toLogDetails(object) | ||
self?.log(level: .debug, message: message, details: logDetails) | ||
} | ||
} | ||
|
||
func info<T: Codable>(_ message: LogMessage, _ object: T) { | ||
Task { [weak self] in | ||
let logDetails: LogDetails? = await self?.toLogDetails(object) | ||
self?.log(level: .info, message: message, details: logDetails) | ||
} | ||
} | ||
|
||
func warn<T: Codable>(_ message: LogMessage, _ object: T) { | ||
Task { [weak self] in | ||
let logDetails: LogDetails? = await self?.toLogDetails(object) | ||
self?.log(level: .warn, message: message, details: logDetails) | ||
} | ||
} | ||
|
||
func error<T: Codable>(_ message: LogMessage, _ object: T) { | ||
Task { [weak self] in | ||
let logDetails: LogDetails? = await self?.toLogDetails(object) | ||
self?.log(level: .error, message: message, details: logDetails) | ||
} | ||
} | ||
} | ||
|
||
extension Logger { | ||
|
||
fileprivate func toLogDetails<T: Codable>(_ object: T) async -> LogDetails? { | ||
guard let data = try? JSONEncoder().encode(object) else { | ||
return nil | ||
} | ||
|
||
guard | ||
let dictionary = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) | ||
else { | ||
return nil | ||
} | ||
|
||
return dictionary as? LogDetails | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters