我试图在Swift中使用CocoaLumberjack。使用pod 'CocoaLumberjack/Swift‘
在目标C中,我做了以下工作
int ddLogLevel = DDLogLevelOff;
@implementation CLDDLoglevel
+ (int)ddLogLevel
{
return ddLogLevel;
}
+ (void)setLogLevel:(int)logLevel
{
ddLogLevel = logLevel;
}
在斯威夫特,我不知道怎么做
我创建了一个实现DDRegisteredDynamicLogging的类
这给了我两种方法
static func ddLogLevel() -> DDLogLevel {
}
static func ddSetLogLevel(level: DDLogLevel) {
}
但是,我仍然不清楚如何在哪里以及如何声明DDLogLevel来设置和获取
等价于int ddLogLevel = DDLogLevelOff;
我试过了
static var ddLogLevel: DDLogLevel = defaultDebugLevel
发布于 2015-12-09 20:39:30
这并不理想,但我已经将其用于CocoaLumberjack 2.2.0,如下所示:
level
参数至关重要。没有它,消息将始终被记录下来。我希望CocoaLumberjack的Swift支持能够成熟并消除这些绊脚石。在那之前,祝你伐木愉快!
发布于 2020-07-18 23:39:10
对于那些仍在寻找在Swift中动态更改日志记录级别的简单方法的人,只需在任何时候使用dynamicLogLevel
即可。例如:
dynamicLogLevel = .info
发布于 2016-03-23 15:38:38
在这个讨论中还有另一个解决方案。它对我来说是暂时的,我正在试验它。
您必须通过定义UnitDDLoggable
来采用
var logPrefix = ""
var logLevel = DDLogLevel.Debug
它允许一个人简单地写:
DDLogWarn("Danger, Will Robinson"
“迅捷2.3”的代码:
import CocoaLumberjackSwift
/// Base protocol for unit specific logging. Generally you won't implement this protocol directly, you will
/// implement one of the protocols that inherit from it
protocol UnitLoggable {
/// Prefix to append to each log line, should include a trailing space to separate it from the log message
var logPrefix:String { get }
}
/// Implment this protocol to use CocoaLumberjack logging with the level controlable at the file level
protocol UnitDDLoggable : UnitLoggable {
/// Lumberjack log level to use for this code unit, Lumberjack log calls in this unit will use this level
/// not the default log level, to use the shared lumberjack level this property should return defaultDebugLevel
var logLevel:DDLogLevel { get }
}
extension UnitDDLoggable {
final func DDLogDebug(@autoclosure logText: () -> String, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true) {
SwiftLogMacro(async, level: logLevel, flag: .Debug, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
final func DDLogInfo(@autoclosure logText: () -> String, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true) {
SwiftLogMacro(async, level: logLevel, flag: .Info, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
final func DDLogWarn(@autoclosure logText: () -> String, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true) {
SwiftLogMacro(async, level: logLevel, flag: .Warning, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
final func DDLogVerbose(@autoclosure logText: () -> String, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true) {
SwiftLogMacro(async, level: logLevel, flag: .Verbose, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
final func DDLogError(@autoclosure logText: () -> String,context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = false) {
SwiftLogMacro(async, level: logLevel, flag: .Error, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
}
https://stackoverflow.com/questions/34048338
复制相似问题