如何使用多个目的地?

时间:2016-05-01 15:14:57

标签: xcglogger

我在XCGLogger github页面的README文件中看到了以下说明。

“另一种常见的使用模式是拥有多个记录器,可能一个用于UI问题,一个用于网络,另一个用于数据问题。

每个日志目标都可以拥有自己的日志级别。为方便起见,您可以在日志对象本身上设置日志级别,并将该级别传递到每个目标。然后设置需要不同的目的地。“

我认为使用XCGLogger非常有用且有意义。任何专家都可以展示如何添加具有不同目的的多个目的地的演示。或者我需要使用多个日志对象?

1 个答案:

答案 0 :(得分:4)

是的,在这种情况下你会使用不同的日志对象。

根据自述文件中的高级用法示例,您可以执行以下操作:

// 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")

这将创建两个日志对象,一个用于具有调试级别的UI事件,一个用于具有详细级别的DB事件。