I am using CocoaLumberjack for a Swift project. I would like to implement custom log levels/flags, as I would like to use 6 rather than the default 5, and would prefer different names.
The documentation for doing this is not helpful. It is only a solution for Objective-C.
The fact that DDLogFlag is defined as NS_OPTIONS means I actually could simply ignore the pre-defined values here, create my own constants, and just write some wrapping code to convert from one to the other.
However, DDLogLevel is defined as NS_ENUM, which means Swift won't be very happy with me trying to instantiate something to say 0b111111, which isn't an existing value in the enum. If it were an NS_OPTIONS, like DDLogFlag, I could just ignore the pre-existing definitions from the library and use whatever valid UInt values I wanted to.
As far as I can tell, I just have to write some Objective-C code to define my own replacements for DDLogLevel, DDLogFlag, and write a custom function to pass this in to and access these properties on DDLogMessage. But this feels bad.
How can I use my own custom logging levels in Swift with CocoaLumberjack?
This is indeed only possible in Objective-C right now - and there also only for the
#defineLog Macros. Even then I could imagine that the "modern" ObjC compiler will warn about the types that are passed toDDLogMessage.The docs are indeed a bit outdated here and stem from a time where Objective-C was closer to C that it is to Swift nowadays... :-)
Nevertheless, in the end
DDLogLevelandDDLogFlagare both stored asNSUInteger. Which means it can theoretically take anyNSUIntegervalue (akaUIntin Swift).To define your own levels, you would simply create an
enum MyLogLevel: UInt { /*...*/ }and then write your own logging functions. Those functions can actually forward to the existing functions:The
unsafeBitCasthere works, because in the end it's just anUIntand_DDLogMessagedoes not switch over the level, but instead does a bit mask check against theflag.Disclaimer: I'm a CocoaLumberjack maintainer myself.
We don't recommend using a custom log level in Swift. There's not much benefit from it and logging frameworks like swift-log also use predefined log levels.
However, I personally could also imagine declaring
DDLogLevelwithNS_OPTIONSinstead ofNS_ENUM. The OSLog Swift overlay also uses an extensible OSLogType. If this is something you'd like to see, please open a PR so we can discuss it with the team. We need to be a bit careful with API compatibility, but like I said it's totally doable.On a side-note: May I ask what you need custom levels for?