Reputation: 731
I saw the below instructions in the README file of XCGLogger github page.
"Another common usage pattern is to have multiple loggers, perhaps one for UI issues, one for networking, and another for data issues.
Each log destination can have its own log level. As a convenience, you can set the log level on the log object itself and it will pass that level to each destination. Then set the destinations that need to be different."
I think that's very useful and meaningful to use XCGLogger. Could any expert show an demo about how to add multiple destinations with different purpose. Or I need to use multiple log objects?
Upvotes: 2
Views: 322
Reputation: 13323
Yes, you would use different log objects in that case.
Based on the example of advanced usage in the readme, you could do something like this:
// Create a logger for UI related events
let logUI = XCGLogger(identifier: "uiLogger", includeDefaultDestinations: false)
// Create a destination for the system console log (via NSLog)
let systemLogDestination = XCGNSLogDestination(owner: logUI, identifier: "uiLogger.systemLogDestination")
// Optionally set some configuration options
systemLogDestination.outputLogLevel = .Debug
systemLogDestination.showLogIdentifier = false
systemLogDestination.showFunctionName = true
systemLogDestination.showThreadName = true
systemLogDestination.showLogLevel = true
systemLogDestination.showFileName = true
systemLogDestination.showLineNumber = true
systemLogDestination.showDate = true
// Add the destination to the logger
logUI.addLogDestination(systemLogDestination)
// Create a logger for DB related events
let logDB = XCGLogger(identifier: "dbLogger", includeDefaultDestinations: false)
// Create a file log destination
let fileLogDestination = XCGFileLogDestination(owner: logDB, writeToFile: "/path/to/file", identifier: "advancedLogger.fileLogDestination")
// Optionally set some configuration options
fileLogDestination.outputLogLevel = .Verbose
fileLogDestination.showLogIdentifier = false
fileLogDestination.showFunctionName = true
fileLogDestination.showThreadName = true
fileLogDestination.showLogLevel = true
fileLogDestination.showFileName = true
fileLogDestination.showLineNumber = true
fileLogDestination.showDate = true
// Add the destination to the logger
logDB.addLogDestination(fileLogDestination)
// Add basic app info, version info etc, to the start of the logs
logUI.logAppDetails()
logDB.logAppDetails()
// Add database version to DB log
logDB.info("DB Schema Version 1.0")
This creates two log objects, one for UI events with a Debug level, one for DB events with a Verbose level.
Upvotes: 4