simpler dynamic log levels

144 views
Skip to first unread message

jthor

unread,
Feb 2, 2012, 1:45:39 PM2/2/12
to CocoaLumberjack
Hi Robbie,

Thanks so much for the work you have put into LumberJack. We are
starting a new project and wanted a good, stable logging library to
use from the ground up and LumberJack seem to fit the bill. I was
reading in your wiki about how to implement dynamic logging levels and
was wondering if there might be a way to do dynamic log levels without
having to add the 'ddLogLevel' and 'ddSetLogLevel' methods to each and
every class that we would like to be able to dynamically change
logging levels on.

I have implemented a slight change in the code and it seems to be
working for me but I would like to get your opinion about our
solution. Here is the approach. We have created a new class that
contains a few methods very similar to what you already provided. It
looks something like this:

@interface XLogConfig : NSObject
+ (void)prepareLogging;
+ (void)setDefaultLogLevel: (int)level;
+ (void)setLogLevel: (int)level forClass:(Class) aClass;
+ (void)setLogLevel: (int)level forClassName:(NSString *) aClassName;
+ (int)logLevelForFileName: (const char *) aFileName;
@end

In the 'setLogLevel: forClass:' we set the log level in a
NSMutableDictionary object. We then use this same NSMutableDictionary
object in the 'logLevelForFileName' method to return the log level
that was set.

In the next step we had to replace your macros with a call to the
above XLogConfig class. So that we replaced the original macro:
#define DDLogError(frmt, ...) LOG_OBJC_MAYBE(LOG_ASYNC_ERROR,
ddLogLevel, LOG_FLAG_ERROR, 0, frmt, ##__VA_ARGS__)

with one that looks like this:

#define DDLogError(frmt, ...) LOG_OBJC_MAYBE(LOG_ASYNC_ERROR,
[XLogConfig logLevelForFileName:__FILE__], LOG_FLAG_ERROR, 0, frmt,
##__VA_ARGS__)

Once we made the above change we can now dynamically change the
logging level on any class/filename pair with a call like the
following:
[XLogConfig setLogLevel:LOG_LEVEL_ERROR
forClassName:@"AppMenuViewController"];

Also we can change the global logging level with a call like this
which will set the default level for any file/class that has not been
customized:
[XLogConfig setDefaultLogLevel:LOG_LEVEL_VERBOSE];

And we can do this without any changes to our class definitions. Can
you forsee any problems with this approach? Is it something that you
would consider integrating into a future release of LumberJack?

Thanks again for this great library. We look forward to getting a lot
of us out of it.

John Thorhauer

Robbie Hanson

unread,
Apr 18, 2012, 8:38:20 PM4/18/12
to cocoalu...@googlegroups.com
Hi John,

This message slipped by me and got lost in my inbox. Sorry about that.

One thing that strikes me about your approach is that NSMutableDictionary isn't thread-safe. So one would need to add some type of locking mechanism within the logLevelForFileName code. This means you're changing the code of every log message from this:

- ivar fetch (is this even considered an operation?)

to this:

- objective-c method invocation
- lock
- dictionary lookup
- unlock

(or a dispatch_sync).

Furthermore, the above has to happen every time. Both for log statements that are above or below logging level threshold.
This will significantly slow down the speed of logging.

-Robbie Hanson

> --
> You received this message because you are subscribed to the Google Groups "CocoaLumberjack" group.
> To post to this group, send email to cocoalu...@googlegroups.com.
> To unsubscribe from this group, send email to cocoalumberja...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cocoalumberjack?hl=en.
>

Reply all
Reply to author
Forward
0 new messages