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
As answered by FreeAsInBeer, you can define this constant in .pch file. You may do like this in .pch file.
// include Lumberjack header file
#import <Lumberjack/Lumberjack.h>
// define ddLogLevel constant
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
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.
// include Lumberjack header file
#import "Lumberjack.h"
#undef ZEKit_LOG_LEVEL
#if defined (DEBUG) && (DEBUG == 1)
#define ZEKit_LOG_LEVEL LOG_LEVEL_VERBOSE
#else
#define ZEKit_LOG_LEVEL LOG_LEVEL_WARN
#endif
static const int ddLogLevel = ZEKit_LOG_LEVEL;
// ... Other custom settings
I didn't find a better way to do it than the one explained in the article I mentioned in the question.
Constant.h
extern int const ddLogLevel;
Constant.m
#import "Constants.h"
#import "DDLog.h"
int const ddLogLevel = LOG_LEVEL_VERBOSE;
Logger configuration
#import "DDLog.h"
#import "DDASLLogger.h"
#import "DDTTYLogger.h"
#import "DDFileLogger.h"
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[DDLog addLogger:[DDASLLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
[DDLog addLogger:fileLogger];
[fileLogger release];
...
Import your class
#import "DDLog.h"
#import "Constants.h"
...
- (void)someMethod {
DDLogVerbose(@"Log this message");
}
Share my configuration for CocoaLumberjack 2.0.0 with global log level
and optional local log level
with preserved DynamicLogLevels feature.
My solution includes simple header file DSLogging.h
(and it's counterpart) that import CocoaLumberjack.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://
// Created by DanSkeel on 23.04.15.
#import "CocoaLumberjack.h"
#define DS_LogScopeGlobal extern
#define DS_LogScopeLocal static
#define DS_LogMutableYes
#define DS_LogMutableNo const
#define DS_LogValueGlobal ;
#define DS_LogValueLocal(lvl) = lvl
#define DS_Setup_Log(scope, mutablility, value) scope mutablility DDLogLevel ddLogLevel value
/** To setup loggin enviroment for particular file use one of these macros
*
* @note Use CocoaLumberjack as usual (https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/Documentation/GettingStarted.md):
*
* 1. just import DSLoggin.h in source file instead of CocoaLumberjack.h
*
* 2. Use one of these macros to setup loggin enviroment for the file.
* Note: there should one of these macros in EACH file that uses CocoaLumberjack macroses.
* @example To enable logging for file with globally defined level you can make convinient snippet:
* @code
* #import "DSLogging.h"
* DSLogLevelSetupGlobal
* @endcode
*
* Use @b SetupGlobal to setup files that will use global level from @p DSLogging.m file
*
* Use @b SetupMutableGlobal to be able to change global level at runtime (assign new level to ddLogLevel variable)
*
* Use @b Setup(DDLogLevel) to set local log level
*
* Use @b SetupMutable(DDLogLevel) to be able to modify local level at runtime ((assign new level to ddLogLevel variable))
*
* This approach preserves a lot of CocoaLumberjack advantages. See SO https://stackoverflow.com/a/29837945/991816
*
* @remarks details: these macros just help you define/reference ddLogLevel value. So if you
* see warning about <i> undeclared identifier </i> it should remind you to use one of these macros in this file.
*/
extern char optionClickMeToSeePrettyDoc;
#define DSLogLevelSetupMutableGlobal DS_Setup_Log(DS_LogScopeGlobal, DS_LogMutableYes, DS_LogValueGlobal)
#define DSLogLevelSetupGlobal DS_Setup_Log(DS_LogScopeGlobal, DS_LogMutableNo, DS_LogValueGlobal)
#define DSLogLevelSetupMutable(lvl) DS_Setup_Log(DS_LogScopeLocal, DS_LogMutableYes, DS_LogValueLocal(lvl))
#define DSLogLevelSetup(lvl) DS_Setup_Log(DS_LogScopeLocal, DS_LogMutableNo, DS_LogValueLocal(lvl))
DSLogging.m
source://
// Created by DanSkeel on 23.04.15.
#import "DSLogging.h"
DDLogLevel ddLogLevel = DDLogLevelVerbose;
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'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
There is a much easier way to solve this, you can set the log level at the Logger instantiation:
#ifdef DEBUG
[DDLog addLogger:[DDTTYLogger sharedInstance] withLevel:DDLogLevelDebug];
#else
[DDLog addLogger:[DDTTYLogger sharedInstance] withLevel:DDLogLevelError];
#endif
So there is no need for extra imports or .pch-file.
For those using CocoaLumberjackSwift you can simply set the following global variable anywhere in your code:
dynamicLogLevel = .verbose
Discussion here