Global log level for CocoaLumberjack

后端 未结 12 922
[愿得一人]
[愿得一人] 2020-12-23 20:19

I\'m using CocoaLumberjack in an iPhone project, to log some information.

I\'ve followed the Getting started guide, and everything works fine, but there is one thing

相关标签:
12条回答
  • 2020-12-23 20:59

    You could use an #include statement in your *.pch file so that it's automatically included in all your project's files.

    0 讨论(0)
  • 2020-12-23 20:59

    You can use this in your *.pch file to automatically get different global log levels depending upon your current build configuration.[for xcode 4+]

    #ifdef DEBUG
      static const int ddLogLevel = LOG_LEVEL_VERBOSE;
    #else
      static const int ddLogLevel = LOG_LEVEL_WARN;
    #endif
    

    or If you need a different log level for every logger, you can easily achieve this using the DDLog +addLogger:withLogLevel: method.

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

    Defining a log level in every source file you mentioned has a benefit. You can use verbose logging level just for the part that you're currently working on. For rest part, you can use other level like info, warn, error.

    0 讨论(0)
  • 2020-12-23 21:01

    In order to dynamically inject log level (for example, from configuration file):

    1) Create a new class named DDLogLevel with the following code:

    #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) In DDLogLevel.h, find the row that contains the following statement:

    #ifndef LOG_LEVEL_DEF
        #define LOG_LEVEL_DEF ddLogLevel
    #endif
    

    And replace it with:

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

    3) Finally, call from your Initialization process (perhaps from appDelegate) to ddSetLogLevel with the desired level.

    0 讨论(0)
  • 2020-12-23 21:04

    The way I did it was inspired by this answer.. however This is how I did it differently so that I can have both a global level log level and be able to override the global log level within each file if I so chose:

    • Instead of calling the file Constants.h I called it GlobalDebugLevel.h. This is because it doesn't make sense to include any other global constants in this file, unless you really will always use the global debug level and have no use for file specific log levels.
    • In the files that I want to have it's own log level.. I simply comment out the `#import "GlobalLogLevel.h" and then include something like this:

    static const int ddLogLevel = LOG_LEVEL_VERBOSE;

    and everybody is happy :)

    p.s. this is a .pch free solution.. inititally I tried that but then the compiler would complain that ddLogLevel is already defined whenever I wanted to override it at a file level

    0 讨论(0)
  • 2020-12-23 21:09

    No more Prefix Headers, please.

    You do not need the now deprecated .pch file, simply include a header file where needed.

    Logger.h - CocoaLumberjack 1.9.x

    #ifndef Project_Logger_h
    #define Project_Logger_h
    
    #if defined(__OBJC__)
    
    #import <CocoaLumberjack/DDLog.h>
    extern int ddLogLevel;
    
    #endif
    
    #endif
    

    Logger.m

    #import "Logger.h"
    
    int ddLogLevel = LOG_LEVEL_VERBOSE;
    

    Changes for CocoaLumberjack 2.x

    #import <CocoaLumberjack/CocoaLumberjack.h>
    
    int ddLogLevel = DDLogLevelVerbose;
    

    If the syntax changes when 2.0 is out of beta please comment or edit.

    Example usage in AppDelegate

    #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__);
    }
    
    0 讨论(0)
  • 2020-12-23 21:09

    Here is a dynamic logging example, which uses DanSkeels DSLogging code from below:

    GFDPerson.h

    #import <Foundation/Foundation.h>
    
    @interface GFDPerson : NSObject{
    @protected
        NSArray         *pLogLevelNames;
        NSArray         *pLogLevelKeys;
        NSDictionary    *pLogLevels;
    }
    
    -(void)logPerson;
    -(void)setLogLevel:(NSUInteger)logLevel;
    
    @end
    

    GFDPerson.m

    #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
    

    main.m

    #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;
    }
    

    output of this code:

    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
    

    Please comment, If I misunderstood or misused something...

    0 讨论(0)
提交回复
热议问题