Many iOS developers have found the Cocoa Lumberjack Logging framework to fill a need that simple NSLog
statements don't. It's reminiscent of Log4J in the Java world.
In any event, I have written my own custom formatter for Lumberjack, but what I don't see is any documentation on how to flush log statements immediately.
For example, if I'm walking through the debugger and I hit an NSLog()
statement, it flushes the log statement to the console immediately. That's the behavior I'd like to get from a DDLogVerbose()
call in Lumberjack.
Right now, I go back and change these statements to NSLog()
statements if I want them to spit out immediately as I'm debugging a segment of code. With Lumberjack being so robust, I got to think there's a way to configure it to flush with no delay.
Anyone know how to make it so?
I found the answer in the DDLog.h
file. Lumberjack has the concept of asynchronous and synchronous logging. At initial read, it didn't hit me as to what this was for.
Basically, if you want the log statement to be output in sequence, one needs to make it synchronized (though, as Mike mentioned, this will slow down performance). So, this should only be done in a debugging situation. Ideally, I'll put another header together and/or some other pre-processor macro to ensure I don't leave the switch flipped on as synchronous.
Here's what you do:
DDLog.h
#define LOG_ASYNC_ENABLED YES
. You can change this to NO
in one spot for synchronous logging across the board, or you can change individual levels, in the lines that follow.Note that the header discourages changing the DDLog.h file itself. So, following the instructions on the Lumberjack wiki page link, they explain how to use a different header file to articulate these override customizations.
Using that, here's what I've successfully written and tested, as "MyAppLumberjack.h" header file that I import in my app's precompiled header:
#import "DDLog.h"
#import "DDASLLogger.h"
#import "DDTTYLogger.h"
// ========================= Overrides ========================================
// --> per https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomLogLevels
// ----------------------------------------------------------------------------
// Are we in an optimized (i.e. Release) build?
#ifdef __OPTIMIZE__
// YES: Nothing to do from the default. (You could simplify this by using #ifndef above instead)
#else
// NO: We're in a Debug build. As such, let's configure logging to flush right away.
// Undefine the asynchronous defaults:
#undef LOG_ASYNC_VERBOSE
#undef LOG_ASYNC_INFO
#undef LOG_ASYNC_WARN
// Define the logs levels to be synchronous:
#define LOG_ASYNC_VERBOSE (NO && LOG_ASYNC_ENABLED) // Debug logging will be synchronous
#define LOG_ASYNC_INFO (NO && LOG_ASYNC_ENABLED) // Info logging will be synchronous
#define LOG_ASYNC_WARN (NO && LOG_ASYNC_ENABLED) // Warn logging will be synchronous
#endif