Cocoalumberjack: Swift not able get log level working for debug and release mode

Created on 18 Sep 2019  路  2Comments  路  Source: CocoaLumberjack/CocoaLumberjack

Hello,
I have integrated framework in a macOS Swift project and it's working as expected but I recently realized that log level in Debug and Release mode is all same. In Objective-C I used to define ddLogLevel with Debug macro and it works all good but and for the Swift, I have defined same in Bridging header file. See below -

@import CocoaLumberjack;

#ifdef DEBUG
static  const int ddLogLevel = DDLogLevelInfo;
#else
static const int ddLogLevel = DDLogLevelError;
#endif

Logger is initialized in AppDelegate.swift, so even if I change to Error/Info for debug mode it is logging all statements, Please advise if I am missing something here.

Update: Even I remove ddLogLevel app does compile and works without it so now not sure where to define the log level?

Thanks.

Most helpful comment

@mpogra The ddLogLevel you are setting is not used at all in Swift and is just for the Objective-C version of CocoaLumberjack. In Swift, there are two log levels you can define. However, only one of them is defined in code, the other is defined at build time.

One level is defined in your GCC_PREPROCESSOR_DEFINITIONS build setting. There you can add a DD_LOG_LEVEL define with whatever log level you want to log at the minimum (e.g. DDLogLevelInfo). Note, however, that this is the absolute minimum. There is no way to dynamically decrease the log level below this define. This is necessary to make sure, Swift does strip the strings which are not logged out of the binary.

The other, more dynamic level is defined using the global Swift variable dynamicLogLevel. With this, you can further control, what messages are logged and this one is also changeable at runtime. Strings that are below that level are not stripped from the binary, however.

Both levels have a default of DDLogLevelAll.

Hope I could help. I'm closing this since it isn't really an "issue" with CocoaLumberjack, but feel free to comment again if you have further questions.

All 2 comments

@mpogra The ddLogLevel you are setting is not used at all in Swift and is just for the Objective-C version of CocoaLumberjack. In Swift, there are two log levels you can define. However, only one of them is defined in code, the other is defined at build time.

One level is defined in your GCC_PREPROCESSOR_DEFINITIONS build setting. There you can add a DD_LOG_LEVEL define with whatever log level you want to log at the minimum (e.g. DDLogLevelInfo). Note, however, that this is the absolute minimum. There is no way to dynamically decrease the log level below this define. This is necessary to make sure, Swift does strip the strings which are not logged out of the binary.

The other, more dynamic level is defined using the global Swift variable dynamicLogLevel. With this, you can further control, what messages are logged and this one is also changeable at runtime. Strings that are below that level are not stripped from the binary, however.

Both levels have a default of DDLogLevelAll.

Hope I could help. I'm closing this since it isn't really an "issue" with CocoaLumberjack, but feel free to comment again if you have further questions.

Thanks so much for the details information and it is now clear to me. Surprisingly I wasn't able to find it anywhere, maybe it can be added to Swift integration docs.

Was this page helpful?
0 / 5 - 0 ratings