A library for logging in macOS platform.
Specify dependency in Package.swift
by adding this:
.package(url: "https://github.com/kelvinjjwong/LoggerFactory.git", .upToNextMajor(from: "1.1.3"))
Then run swift build
to download and integrate the package.
Use CocoaPods to install LoggerFactory
by adding it to Podfile
:
pod 'LoggerFactory', '~> 1.1.3'
Then run pod install
to download and integrate the package.
Such as in AppDelegate.swift
import LoggerFactory
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
LoggerFactory.append(logWriter: ConsoleLogger())
LoggerFactory.append(logWriter: FileLogger())
}
Or:
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
LoggerFactory.append(logWriter: ConsoleLogger())
LoggerFactory.append(logWriter: FileLogger(pathOfFolder: "/path/to/folder/of/log/files"))
}
let logger = LoggerFactory.get(category: "DB")
Or:
let logger = LoggerFactory.get(category: "DB", subCategory: "PostgresDB")
You can specify logging levels including [.info, .warning, .error, .debug, .trace, .performance, .todo].
By default, [.info, .warning, .error, .todo] will be printed. You can use includeTypes
and excludeTypes
to add or remove some of them.
let logger = LoggerFactory.get(category: "DB", subCategory: "PostgresDB", includeTypes: [.performance, .debug, .trace])
Or:
let logger = LoggerFactory.get(category: "DB", subCategory: "PostgresDB", excludeTypes: [.todo, .warning])
By default, it logs at info
level when you don't specify.
self.logger.log("Some info message.")
self.logger.log(.error, "Error at somewhere.")
or you can specify the error in a do...catch...
do {
...
}catch{
self.logger.log(.error, "Error at somewhere.", error)
}
or you can calculate how much time elapsed by using timecost
at performance
level:
let startTime = Date()
// do something
self.logger.timecost("did something", fromDate: startTime)
Writing log to file: /Users/kelvinwong/Library/Containers/nonamecat.PostgresModelApp/Data/Documents/log/2023-07-20_0647.log
📗 2023-07-19T22:47:37Z [DB][PostgresDB] connecting: [email protected]:5432/ModelTest
📗 2023-07-19T22:47:37Z [ViewController] [record]: 1 Optional("Tom") Optional(20)
📗 2023-07-19T22:47:37Z [ViewController] [record]: 2 Optional("Daisy") Optional(17)
📗 2023-07-19T22:47:37Z [ViewController] [record]: 3 Optional("DrWHO") nil
📗 2023-07-19T22:47:37Z [ViewController] [record]: 4 Optional("Jack") Optional(38)
📗 2023-07-19T22:47:37Z [ViewController] [record]: 5 nil Optional(41)
📗 2023-07-19T22:47:37Z [ViewController] [record]: 6 Optional("Helen") Optional(14)
📕 2023-07-19T22:52:12Z [DB][PostgresDB] Error: connection lost.
🕘 2023-07-19T22:54:23Z [DB][PostgresDB] did something - time cost: 123 seconds.