-
Notifications
You must be signed in to change notification settings - Fork 316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add hook to customize the way logs are emitted #481
Conversation
Hi!! 👋 Thanks so much for opening! The one gotcha here is that making the code from So in order for this to work, we'd to make it available through |
I'll take a look at the header issue.
If an adopter is specifying a custom log handler, they don't need (nor want) the Purchases-provided log format. They know by virtue of setting the log handler that the messages are coming from the Purchases framework, and getting the |
@syzygydevelopment that's a good point, I agree. |
I've been looking at this and am struggling to see how exposing the level would be possible, due to the structure of the package. Do you have any suggestions on how it could be exposed? |
sorry @syzygydevelopment I should have been more clear about how to do it. And thinking about it a bit more it's unfortunately not trivial, with the biggest problem being that The way I'd do it is:
+ (void)setLogHandler:(void (^)(NSInteger, NSString *))logHandler;
+ (void)setLogHandler:(void (^)(NSInteger, NSString *))logHandler {
[RCLogger setLogHandler:logHandler];
} Then update @objc public static var logHandler: (Int, String) -> Void = { levelAsInt, message in
guard let level = LogLevel(rawValue: levelAsInt) else { return }
NSLog("[\(frameworkDescription)] - \(level.description()): \(message)")
}
...
@objc public static func log(level: LogLevel, message: String) {
guard level != .debug || shouldShowDebugLogs else { return }
logHandler(level.rawValue, message)
} I can't think of a better way around the enum problem, that doesn't involve having This is probably the best approach, although it's quite a bit more work. This is way more complicated than it should be, but having a mixed language framework that works in cocoapods, spm, carthage, etc has been challenging, to say the least. I'm hoping to do some bigger refactors soon that will this kind of thing a lot easier. I totally understand if you don't want to go ahead and do the full implementation yourself, since it's a bit convoluted. If that's the case I'm happy to merge this and use it as a base to build the rest with. Let me know what you think! |
@aboedo I've found a good compromise:
I verified that:
Since this involved creating a new |
@syzygydevelopment sounds great! And yeah, deprecating the old Thanks again! |
@syzygydevelopment sorry for the embarasingly long wait on this. It's been a very packed couple of months. I'm merging this now and fixing the tests in a separate PR. Thanks again for doing this and for your patience! |
This changes adds the ability to set a new handler on the
RCLogger
type. By default, the handler is the current implementation ofNSLog
that emits the framework description.The log handler provides the parameters of the log level and the message, and then anyone providing a custom handler can interpret those parameters however they wish and/or redirect them to their own logging system.