OneSignal-iOS-SDK icon indicating copy to clipboard operation
OneSignal-iOS-SDK copied to clipboard

jwt(feat): add public log listener methods

Open nan-li opened this issue 5 months ago • 0 comments

Description

One Line Summary

Adds the following new public methods to allow an app developer to capture logs as strings at runtime.

Details

Adds the following new public methods:

  • OneSignal.Debug.addLogListener
  • OneSignal.Debug.removeLogListener

These new methods provide a way to capture all SDK log entires at runtime, allowing the app developer to store these and/or send them to their server.

The log listener is independent of logLevel, all message are always sent to listeners.

Sample Usage

// AppDelegate.h
// Add OSLogListener after UIApplicationDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate, OSLogListener>
@end

// AppDelegate.m
@implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Add your AppDelegate as an observer
  [OneSignal.Debug addLogListener:self];
}

// Add this new method
- (void)onLogEvent:(OneSignalLogEvent * _Nonnull)event {
  NSLog(@"onLogEvent: %lu - %@", (unsigned long)event.level, event.entry);
}
@end

// Remove the observer
[OneSignal.Debug removeLogListener:self];
// AppDelegate.swift
// Add OSLogListener after UIApplicationDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, OSLogListener {
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Add your AppDelegate as an observer
    OneSignal.Debug.addLogListener(self)
  }

  // Add this new method
  func onLogEvent(_ event: OneSignalLogEvent) {
    print("onLogEvent: \(event.level) - \(event.entry)")
  }
}
     
// Remove the observer
OneSignal.Debug.removeLogListener(self)

Motivation

App developers want a way to capture all SDK logs and send them to their server to debug issues in production.

Scope

Only add new methods to OneSignal.Debug to allow of capturing logs at runtime.

Testing

Unit testing

None

Manual testing

Tested on an iPhone 13, tested concurrent modification of log listeners array as well

Affected code checklist

  • [ ] Notifications
    • [ ] Display
    • [ ] Open
    • [ ] Push Processing
    • [ ] Confirm Deliveries
  • [ ] Outcomes
  • [ ] Sessions
  • [ ] In-App Messaging
  • [ ] REST API requests
  • [X] Public API changes

Checklist

Overview

  • [x] I have filled out all REQUIRED sections above
  • [X] PR does one thing
  • [X] Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • [ ] I have included test coverage for these changes, or explained why they are not needed
  • [ ] All automated tests pass, or I explained why that is not possible
  • [X] I have personally tested this on my device, or explained why that is not possible

Final pass

  • [X] Code is as readable as possible.
  • [X] I have reviewed this PR myself, ensuring it meets each checklist item

This change is Reviewable

nan-li avatar Jun 19 '25 21:06 nan-li