Ios CocoaLumberjack的全球日志级别

Ios CocoaLumberjack的全球日志级别,ios,objective-c,logging,cocoalumberjack,Ios,Objective C,Logging,Cocoalumberjack,我在一个iPhone项目中使用它来记录一些信息 我遵循了这一点,一切正常,但有一件事让我感到困扰:似乎没有一种优雅的方式来定义整个应用程序的日志级别。要使其工作,我需要在每个源文件中定义一个常量,如下所示: static const int ddLogLevel = LOG_LEVEL_VERBOSE; 那么,有没有办法为应用程序定义全局日志级别 我在这个主题上找到了,但我仍然需要在每个文件中添加一个导入…你可以在你的*.pch文件中使用一个\include语句,这样它就会自动包含在你的所有项

我在一个iPhone项目中使用它来记录一些信息

我遵循了这一点,一切正常,但有一件事让我感到困扰:似乎没有一种优雅的方式来定义整个应用程序的日志级别。要使其工作,我需要在每个源文件中定义一个常量,如下所示:

static const int ddLogLevel = LOG_LEVEL_VERBOSE;
那么,有没有办法为应用程序定义全局日志级别


我在这个主题上找到了,但我仍然需要在每个文件中添加一个导入…

你可以在你的*.pch文件中使用一个
\include
语句,这样它就会自动包含在你的所有项目文件中。

我没有找到比我在问题中提到的更好的方法

常数h

常数m

记录器配置

导入您的类


CocoaLumberjack附带了一个示例应用程序,该应用程序演示了如何设置全局日志级别,您可以在此处找到该级别

您可以在*.pch文件中使用它,根据当前的构建配置自动获取不同的全局日志级别。[对于xcode 4+]

#ifdef DEBUG
  static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
  static const int ddLogLevel = LOG_LEVEL_WARN;
#endif
或者,如果每个记录器需要不同的日志级别,可以使用DDLog+addLogger:withLogLevel:方法轻松实现

[DDLog addLogger:[DDASLLogger sharedInstance] withLogLevel:LOG_LEVEL_INFO];
[DDLog addLogger:[DDTTYLogger sharedInstance] withLogLevel:LOG_LEVEL_DEBUG];

在您提到的每个源文件中定义日志级别有一个好处。您可以仅对当前正在处理的部分使用详细日志记录级别。对于rest部分,您可以使用其他级别,如info、warn、error。

以便动态注入日志级别(例如,从配置文件):

1) 使用以下代码创建名为DDLogLevel的新类:

#import "DDLogLevel.h"
#import "DDLog.h"

@implementation DDLogLevel

static int _ddLogLevel = LOG_LEVEL_VERBOSE;

+ (int)ddLogLevel
{
    return _ddLogLevel;
}

+ (void)ddSetLogLevel:(int)logLevel
{
    _ddLogLevel = logLevel;
}

@end
2) 在DDLogLevel.h中,找到包含以下语句的行:

#ifndef LOG_LEVEL_DEF
    #define LOG_LEVEL_DEF ddLogLevel
#endif
并将其替换为:

#ifndef LOG_LEVEL_DEF
    #define LOG_LEVEL_DEF [DDLogLevel ddLogLevel]
#endif

3) 最后,从您的初始化过程(可能从appDelegate)调用具有所需级别的ddSetLogLevel。

正如FreeAsInBeer所回答的,您可以在.pch文件中定义此常量。您可以在.pch文件中这样做

// include Lumberjack header file 
#import <Lumberjack/Lumberjack.h>

// define ddLogLevel constant
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
请不要再有前缀标题了。 您不需要现在已弃用的
.pch
文件,只需在需要的地方包含一个头文件即可

Logger.h-CocoaLumberjack 1.9.x CocoaLumberjack 2.x的更改
#导入
int ddLogLevel=DDLogLevelVerbose;
如果语法在2.0未通过测试版时发生更改,请进行评论或编辑

AppDelegate中的示例用法
#导入“AppDelegate.h”
#导入“Logger.h”
#进口
#进口
#进口
@接口AppDelegate()
@属性(强,非原子)DDFileLogger*fileLogger;
@结束
@实现AppDelegate
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
[DDLog addLogger:[DDASLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
DDFileLogger*fileLogger=[[DDFileLogger alloc]init];
fileLogger.rollingFrequency=60*60*24;//24小时滚动
fileLogger.logFileManager.maximumNumberOfLogFiles=7;
[DDLog addLogger:fileLogger];
self.fileLogger=文件记录器;
DDLogDebug(@“%s”,函数);
返回YES;
}
-(无效)应用程序将重新签名:(UIApplication*)应用程序
{
DDLogDebug(@“%s”,函数);
}
-(无效)应用程序标识符背景:(UIApplication*)应用程序
{
DDLogDebug(@“%s”,函数);
}
-(无效)应用程序将进入前台:(UIApplication*)应用程序
{
DDLogDebug(@“%s”,函数);
}
-(无效)应用IDBECOMEACTIVE:(UIApplication*)应用
{
DDLogDebug(@“%s”,函数);
}
-(无效)申请将终止:(UIApplication*)申请
{
DDLogDebug(@“%s”,函数);
}

我这样做的灵感来自于答案。。但是,这就是我的不同做法,这样我既可以拥有全局级别的日志级别,也可以覆盖每个文件中的全局日志级别(如果我选择):

  • 我没有调用文件
    Constants.h
    ,而是调用了
    GlobalDebugLevel.h
    。这是因为在该文件中包含任何其他全局常量都没有意义,除非您总是使用全局调试级别,并且不使用特定于文件的日志级别
  • 在我想要拥有自己日志级别的文件中。。我只需注释掉`#import“globallowlevel.h”,然后包括如下内容:
static const int ddLogLevel=LOG\u LEVEL\u VERBOSE

大家都很高兴:)


p、 这是一个免费的解决方案。。最初我尝试过,但编译器会抱怨说,每当我想在文件级别重写它时,
ddLogLevel
已经定义了

我的解决方案包括简单的头文件
DSLogging.h
(及其对应文件),它导入
cocoalamberjack.h
,并定义方便的宏来设置使用cocoalamberjack日志宏的文件。以下是您应该如何使用它:

  • 导入
    DSLogging.h
    标题(两种方式):
    • 在使用CocoaLumberjack的每个文件中导入它
    • 将其导入
      .pch
      文件一次走这条路之前。
  • 使用
    DSLogLevelSetup…
    宏设置文件的日志级别。注意:使用日志记录的每个源文件中都应该有宏
  • 有关更多详细信息,请参阅内部文档

    DSLogging.h
    标题: 为什么我认为这是一个好方法:
  • 这比Cocoa伐木工人强一点

    • 全局级别(可以是可变的)
    • 允许您按本地级别“覆盖”全局级别(可以是可变的)
  • 它不削减CocoaLumberjack的功能

    • 使用变量设置级别,因此它可以与CocoaLumberjack的高级功能一起使用
  • 我是CocoaLumberjack的新手,我对自己的方法过于乐观,wo
    #import "DDLogLevel.h"
    #import "DDLog.h"
    
    @implementation DDLogLevel
    
    static int _ddLogLevel = LOG_LEVEL_VERBOSE;
    
    + (int)ddLogLevel
    {
        return _ddLogLevel;
    }
    
    + (void)ddSetLogLevel:(int)logLevel
    {
        _ddLogLevel = logLevel;
    }
    
    @end
    
    #ifndef LOG_LEVEL_DEF
        #define LOG_LEVEL_DEF ddLogLevel
    #endif
    
    #ifndef LOG_LEVEL_DEF
        #define LOG_LEVEL_DEF [DDLogLevel ddLogLevel]
    #endif
    
    // include Lumberjack header file 
    #import <Lumberjack/Lumberjack.h>
    
    // define ddLogLevel constant
    static const int ddLogLevel = LOG_LEVEL_VERBOSE;
    
    // include Lumberjack header file
    #import "Lumberjack.h" 
    
    #undef ZEKit_LOG_LEVEL
    #if defined (DEBUG) && (DEBUG == 1)
    #define ZEKit_LOG_LEVEL LOG_LEVEL_VERBOSE
    #else
    #define ZEKit_LOG_LEVEL LOG_LEVEL_WARN
    #endif
    
    static const int ddLogLevel = ZEKit_LOG_LEVEL;
    
    // ... Other custom settings
    
    #ifndef Project_Logger_h
    #define Project_Logger_h
    
    #if defined(__OBJC__)
    
    #import <CocoaLumberjack/DDLog.h>
    extern int ddLogLevel;
    
    #endif
    
    #endif
    
    #import "Logger.h"
    
    int ddLogLevel = LOG_LEVEL_VERBOSE;
    
    #import <CocoaLumberjack/CocoaLumberjack.h>
    
    int ddLogLevel = DDLogLevelVerbose;
    
    #import "AppDelegate.h"
    
    #import "Logger.h"
    
    #import <CocoaLumberjack/DDFileLogger.h>
    #import <CocoaLumberjack/DDASLLogger.h>
    #import <CocoaLumberjack/DDTTYLogger.h>
    
    
    
    @interface AppDelegate ()
    @property (strong, nonatomic) DDFileLogger *fileLogger;
    @end
    
    
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [DDLog addLogger:[DDASLLogger sharedInstance]];
        [DDLog addLogger:[DDTTYLogger sharedInstance]];
    
        DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
        fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
        fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
    
        [DDLog addLogger:fileLogger];
        self.fileLogger = fileLogger;
    
        DDLogDebug(@"%s", __PRETTY_FUNCTION__);
    
        return YES;
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application
    {
        DDLogDebug(@"%s", __PRETTY_FUNCTION__);
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        DDLogDebug(@"%s", __PRETTY_FUNCTION__);
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        DDLogDebug(@"%s", __PRETTY_FUNCTION__);
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        DDLogDebug(@"%s", __PRETTY_FUNCTION__);
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application
    {
        DDLogDebug(@"%s", __PRETTY_FUNCTION__);
    }
    
    //
    //  Created by DanSkeel on 23.04.15.
    
    #import "CocoaLumberjack.h"
    
    #define DS_LogScopeGlobal extern
    #define DS_LogScopeLocal static
    #define DS_LogMutableYes
    #define DS_LogMutableNo const
    
    #define DS_LogValueGlobal ;
    #define DS_LogValueLocal(lvl) = lvl
    
    #define DS_Setup_Log(scope, mutablility, value) scope mutablility DDLogLevel ddLogLevel value
    
    /** To setup loggin enviroment for particular file use one of these macros
     *
     *  @note Use CocoaLumberjack as usual (https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/Documentation/GettingStarted.md):
     *
     *  1. just import DSLoggin.h in source file instead of CocoaLumberjack.h
     *
     *  2. Use one of these macros to setup loggin enviroment for the file.
     *  Note: there should one of these macros in EACH file that uses CocoaLumberjack macroses.
     *  @example To enable logging for file with globally defined level you can make convinient snippet:
     *  @code
     *  #import "DSLogging.h"
     *  DSLogLevelSetupGlobal
     *  @endcode
     * 
     *  Use @b SetupGlobal to setup files that will use global level from @p DSLogging.m file
     *
     *  Use @b SetupMutableGlobal to be able to change global level at runtime (assign new level to ddLogLevel variable)
     *
     *  Use @b Setup(DDLogLevel) to set local log level
     *
     *  Use @b SetupMutable(DDLogLevel) to be able to modify local level at runtime ((assign new level to ddLogLevel variable))
     *
     *  This approach preserves a lot of CocoaLumberjack advantages. See SO https://stackoverflow.com/a/29837945/991816
     *
     *  @remarks details: these macros just help you define/reference ddLogLevel value. So if you
     *  see warning about <i> undeclared identifier </i> it should remind you to use one of these macros in this file.
     */
    extern char optionClickMeToSeePrettyDoc;
    #define DSLogLevelSetupMutableGlobal DS_Setup_Log(DS_LogScopeGlobal, DS_LogMutableYes, DS_LogValueGlobal)
    #define DSLogLevelSetupGlobal        DS_Setup_Log(DS_LogScopeGlobal, DS_LogMutableNo,  DS_LogValueGlobal)
    #define DSLogLevelSetupMutable(lvl)  DS_Setup_Log(DS_LogScopeLocal,  DS_LogMutableYes, DS_LogValueLocal(lvl))
    #define DSLogLevelSetup(lvl)         DS_Setup_Log(DS_LogScopeLocal,  DS_LogMutableNo,  DS_LogValueLocal(lvl))
    
    //
    //  Created by DanSkeel on 23.04.15.
    
    #import "DSLogging.h"
    
    DDLogLevel ddLogLevel = DDLogLevelVerbose;
    
    #ifdef DEBUG
      [DDLog addLogger:[DDTTYLogger sharedInstance] withLevel:DDLogLevelDebug];
    #else
      [DDLog addLogger:[DDTTYLogger sharedInstance] withLevel:DDLogLevelError];
    #endif
    
    #import <Foundation/Foundation.h>
    
    @interface GFDPerson : NSObject{
    @protected
        NSArray         *pLogLevelNames;
        NSArray         *pLogLevelKeys;
        NSDictionary    *pLogLevels;
    }
    
    -(void)logPerson;
    -(void)setLogLevel:(NSUInteger)logLevel;
    
    @end
    
    #import "GFDPerson.h"
    #import "DSLogging.h"
    
    DSLogLevelSetupMutable(DDLogLevelWarning);
    
    @implementation GFDPerson
    
    -(id)init{
        if (self = [super init]) {
            pLogLevelNames = [[NSArray alloc] initWithObjects:
                              @"no logging",
                              @"only errors",
                              @"errors and warnings",
                              @"errors, warnings and infos",
                              @"verbose",
                              nil];
    
            pLogLevelKeys = [[NSArray alloc] initWithObjects:
                             [[NSNumber numberWithInteger:DDLogLevelOff]stringValue],
                             [[NSNumber     numberWithInteger:DDLogLevelError]stringValue],
                             [[NSNumber     numberWithInteger:DDLogLevelWarning]stringValue],
                             [[NSNumber     numberWithInteger:DDLogLevelInfo]stringValue],
                             [[NSNumber     numberWithInteger:DDLogLevelVerbose]stringValue],
                             nil];
    
            pLogLevels = [[NSDictionary alloc]initWithObjects:pLogLevelNames
                                                      forKeys:pLogLevelKeys];
    
            return self;
        }
        return nil;
    }
    
    -(void)setLogLevel:(NSUInteger)logLevel{
        ddLogLevel = logLevel;
    }
    
    -(void)logPerson{
    
        NSLog(@"Person is logging with Loglevel: %@",[pLogLevels valueForKey:    [[NSNumber numberWithInteger:ddLogLevel]stringValue]]);
        DDLogVerbose(@"Person log verbose");
        DDLogInfo(@"Person log info");
        DDLogWarn(@"Person log warning");
        DDLogError(@"Person log error");
        DDLogDebug(@"Person log debug");
    }
    
    @end
    
    #import <Foundation/Foundation.h>
    #import "DSLogging.h"
    #import "GFDPerson.h"
    
    DSLogLevelSetupMutable(DDLogLevelError);
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // insert code here...
            [DDLog addLogger:[DDASLLogger sharedInstance]];
            [DDLog addLogger:[DDTTYLogger sharedInstance]];
    
            ddLogLevel = DDLogLevelWarning;
            NSLog(@"Warning:");
            DDLogWarn(@"WARNING LOG!");
            DDLogError(@"ERROR LOG!");
            DDLogVerbose(@"VERBOSE LOG!");
    
            ddLogLevel = DDLogLevelError;
            NSLog(@"Error:");
            DDLogWarn(@"WARNING LOG!");
            DDLogError(@"ERROR LOG!");
            DDLogVerbose(@"VERBOSE LOG!");
    
            ddLogLevel = DDLogLevelOff;
            NSLog(@"Off:");
            DDLogWarn(@"WARNING LOG!");
            DDLogError(@"ERROR LOG!");
            DDLogVerbose(@"VERBOSE LOG!");
    
            ddLogLevel = DDLogLevelVerbose;
            NSLog(@"Verbose:");
            DDLogWarn(@"WARNING LOG!");
            DDLogError(@"ERROR LOG!");
            DDLogVerbose(@"VERBOSE LOG!");
    
            ddLogLevel = DDLogLevelOff;
            GFDPerson *personA = [[GFDPerson alloc] init];
            [personA logPerson];
    
            [personA setLogLevel:DDLogLevelVerbose];
            [personA logPerson];
    
            [personA setLogLevel:DDLogLevelError];
            [personA logPerson];
    
        }
        return 0;
    }
    
    Warning:
    WARNING LOG!
    ERROR LOG!
    Error:
    ERROR LOG!
    Off:
    Verbose:
    WARNING LOG!
    ERROR LOG!
    VERBOSE LOG!
    Person is logging with Loglevel: errors and warnings
    Person log warning
    Person log error
    Person is logging with Loglevel: verbose
    Person log verbose
    Person log info
    Person log warning
    Person log error
    Person log debug
    Person is logging with Loglevel: only errors
    Person log error
    
    dynamicLogLevel = .verbose