以前XCGLogger对我来说运行得很好,但我决定尝试一些更高级的功能。现在,我在Xcode的控制台视图中的日志输出中填充了:
XCGLogger writing log to: <my logfile name>
它会出现在每条记录的消息之前。你知道为什么吗?
我的XCGLogger设置:
var log : XCGLogger {
let log = XCGLogger(identifier: "advancedLogger", includeDefaultDestinations: false)
let fileDestination = FileDestination(writeToFile: Constants.file.debugging, identifier: "advancedLogger.fileDestination")
fileDestination.showLogIdentifier = false
fileDestination.showFunctionName = true
fileDestination.showThreadName = false
fileDestination.showLevel = false
fileDestination.showFileName = true
fileDestination.showLineNumber = true
fileDestination.showDate = true
#if DEBUG
fileDestination.outputLevel = .verbose
#else
fileDestination.outputLevel = .debug
// don't log on main thread in production
fileDestination.logQueue = XCGLogger.logQueue
#endif
// Add the destination to the logger
log.add(destination: fileDestination)
return log
}发布于 2016-12-16 06:03:11
修复了!我会告诉你我做错了什么。有关更正后的代码块,请参阅底部。
首先,在设置log变量时,我在声明中省略了=符号:
let log : XCGLogger {
并且忽略了将()添加到块的末尾。
然后,我收到来自编译器的错误消息,指出:
'let' declarations cannot be computed properties
我想也许在Swift 3.0中有一些我没有意识到的变化,所以我把它从let改成了var,然后继续,意思是稍后再来讨论这个问题。当然,我忘了。
然后我经历了上面解释的问题,在这里发帖后,再次浏览了所有内容,并意识到每次都会出现该消息的唯一方法是每次我登录时都会以某种方式初始化它。哦!现在,我记起了有关“计算的属性”的警告,并最终意识到每次访问log变量时,我的代码都在运行所有的日志初始化。
一旦我返回并将=和()添加到log的声明中,并将其切换回let,一切都按预期工作:
let log : XCGLogger = {
let log = XCGLogger(identifier: "advancedLogger", includeDefaultDestinations: false)
let fileDestination = FileDestination(writeToFile: Constants.file.debugging, identifier: "advancedLogger.fileDestination")
fileDestination.showLogIdentifier = false
fileDestination.showFunctionName = true
fileDestination.showThreadName = false
fileDestination.showLevel = false
fileDestination.showFileName = true
fileDestination.showLineNumber = true
fileDestination.showDate = true
#if DEBUG
fileDestination.outputLevel = .verbose
#else
fileDestination.outputLevel = .debug
// don't log on main thread in production
fileDestination.logQueue = XCGLogger.logQueue
#endif
// Add the destination to the logger
log.add(destination: fileDestination)
return log
}()https://stackoverflow.com/questions/41173826
复制相似问题