hasan
hasan

Reputation: 24205

CocoaLumberjack log level

I don't really understand what log level means.

In Lumbejack the following log levels defined:

#define LOG_LEVEL_OFF     DDLogLevelOff
#define LOG_LEVEL_ERROR   DDLogLevelError
#define LOG_LEVEL_WARN    DDLogLevelWarning
#define LOG_LEVEL_INFO    DDLogLevelInfo
#define LOG_LEVEL_DEBUG   DDLogLevelDebug
#define LOG_LEVEL_VERBOSE DDLogLevelVerbose
#define LOG_LEVEL_ALL     DDLogLevelAll

What some of those mean? and how they are used? Is those related to CocoaLumberjack all for iOS?

Also, I use the following code in my pch file:

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

What those mean? I searched the project for the ddLogLevel var and I didnt find it used anywhere. also, not in lumberjack pod.

Upvotes: 1

Views: 1506

Answers (2)

keithbhunter
keithbhunter

Reputation: 12324

Those are varying degrees of logging granularity. LOG_LEVEL_ALL means any and every log will be written to the console and file that lumberjack uses. LOG_LEVEL_OFF is the other end of the extreme where no logging happens. You can use these to determine what kind of logs to show for which build. Here are some examples of use cases.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // this log isn't particularly useful for a production build, 
    // but can be helpful when debugging. So we use verbose.
    DDLogVerbose(@"This view controller loaded");
}

- (void)didRecieveMemoryWarning
{
    // This is a bad situation, but it isn't an error really.
    // Just a signal that we need to free up memory. So we use warning.
    DDLogWarning(@"We are using too much memory.");
}

- (void)doSomething
{
    NSError *error;
    [self doSomethingElse:&error];
    if (error) {
        // This is definitely an error we need to handle. If you have to
        // look through crash logs on a production build, you definitely
        // want this log to show up there.
        DDLogError(@"Encountered an error: %@", error);
    }
}

In these examples, all of the logs will show up when you run your app from Xcode, but only the error log will show up in a production crash log. The ddLogLevel constant is how you determine what level of logging you want.

Upvotes: 2

rmaddy
rmaddy

Reputation: 318804

Setting the ddLogLevel filters what messages appear from the various DDLogXXX methods.

If you set ddLogLevel to LOG_LEVEL_ALL then all DDLogXXX methods will be logged. If you set ddLogLevel to LOG_LEVEL_INFO then only Info, Warning, and Error will be logged.

Just look at the list of #define lines you show. Selecting a given value results in only messages from that level and those higher up in the list.

If you set ddLogLevel to LOG_LEVEL_INFO and you have the following two lines:

DDLogInfo("some info message");
DDLogDebug("some debug message");

Then only the first message will be logged because Debug is lower than Info.

The actual meaning of each level is somewhat subjective. Just use them consistently in your app. The most important or critical messages should have the highest levels and the least important should have the lower level.

I use DDLogError when my app encounters unexpected values or when a method that provides an NSError parameter fails. I log a relevant message and include the NSError value.

I use DDLogInfo for "I'm here" type messages.

I use DDLogDebug to log variable values.

I don't use DDLogWarn too often but you could use it for unexpected issues where there isn't an actual error but something important to note.

Upvotes: 2

Related Questions