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...
There's an example app included with CocoaLumberjack that shows how to set a global log level that you can find here https://github.com/robbiehanson/CocoaLumberjack/tree/master/Xcode/GlobalLogLevel
In order to dynamically inject log level (for example, from configuration file):
1) Create a new class named DDLogLevel with the following code:
2) In DDLogLevel.h, find the row that contains the following statement:
And replace it with:
3) Finally, call from your Initialization process (perhaps from appDelegate) to ddSetLogLevel with the desired level.
Share my configuration for CocoaLumberjack 2.0.0 with
global log level
and optionallocal log level
with preserved DynamicLogLevels feature.My solution includes simple header file
DSLogging.h
(and it's counterpart) that importCocoaLumberjack.h
and define convenience macros for setting up the files that use CocoaLumberjack log macros. Here is how you should use it:DSLogging.h
header (two ways):.pch
file once. Consider this before going this way.DSLogLevelSetup...
macros to set log level for file. Note: there should be macros in EACH source file that uses logging.See documentation inside for more details. Download gist.
DSLogging.h
header:DSLogging.m
source:Why I think it's a good approach:
It's a little better than just CocoaLumberjack
It doesn't cut CocoaLumberjack functions
I'm new to CocoaLumberjack and I can be too optimistic about my approach, would be glad to hear your critics if I lie at some point.
There is a much easier way to solve this, you can set the log level at the Logger instantiation:
So there is no need for extra imports or .pch-file.
As answered by FreeAsInBeer, you can define this constant in .pch file. You may do like this in .pch file.
Im my implement, I create a new header file(e.g. mylog.h) for custom Lumberjack settings. in this way, I use
#import
statement in my .pch file for including mylog.h. This custom header file may like this.You could use an
#include
statement in your *.pch file so that it's automatically included in all your project's files.