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 that bugs me: there doesn't seem to be an elegant way to define a log level for the whole app. To make it work I need to define a constant in every source file, like this:
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
So, is there a way to define a global log level for the application?
I found this article on the subject, but I still need to add an #import in every file...
Here is a dynamic logging example, which uses DanSkeels DSLogging code from below:
GFDPerson.h
GFDPerson.m
main.m
output of this code:
Please comment, If I misunderstood or misused something...
I didn't find a better way to do it than the one explained in the article I mentioned in the question.
Constant.h
Constant.m
Logger configuration
Import your class
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
Logger.m
Changes for CocoaLumberjack 2.x
If the syntax changes when 2.0 is out of beta please comment or edit.
Example usage in AppDelegate
You can use this in your *.pch file to automatically get different global log levels depending upon your current build configuration.[for xcode 4+]
or If you need a different log level for every logger, you can easily achieve this using the DDLog +addLogger:withLogLevel: method.
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.
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:
Constants.h
I called itGlobalDebugLevel.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.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 thatddLogLevel
is already defined whenever I wanted to override it at a file level