Skip to content

Commit

Permalink
feat: add logging to supabase_flutter
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Oct 3, 2024
1 parent 8d5ce7e commit 7e67afc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/gotrue/lib/src/gotrue_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ class GoTrueClient {
});
}
final state = AuthState(event, session, fromBroadcast: !broadcast);
_log.fine('Notifying subscribers: $state');
_log.fine('onAuthStateChange: $state');
_onAuthStateChangeController.add(state);
_onAuthStateChangeControllerSync.add(state);
}
Expand Down
31 changes: 20 additions & 11 deletions packages/supabase_flutter/lib/src/supabase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import 'dart:async';
import 'package:async/async.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:logging/logging.dart';
import 'package:supabase/supabase.dart';
import 'package:supabase_flutter/src/constants.dart';
import 'package:supabase_flutter/src/flutter_go_true_client_options.dart';
import 'package:supabase_flutter/src/local_storage.dart';
import 'package:supabase_flutter/src/supabase_auth.dart';

import 'version.dart';

final _log = Logger('supabase.supabase_flutter');

/// Supabase instance.
///
/// It must be initialized before used, otherwise an error is thrown.
Expand Down Expand Up @@ -82,6 +87,19 @@ class Supabase {
!_instance._initialized,
'This instance is already initialized',
);
_instance._debugEnable = debug ?? kDebugMode;

if (_instance._debugEnable) {
_log.onRecord.listen((record) {
if (record.level >= Level.INFO) {
debugPrint(
'${record.loggerName}: ${record.level.name}: ${record.message} ${record.error ?? ""}');
}
});
}

_log.config("Initialize Supabase v$version");

if (authOptions.pkceAsyncStorage == null) {
authOptions = authOptions.copyWith(
pkceAsyncStorage: SharedPreferencesGotrueAsyncStorage(),
Expand All @@ -106,8 +124,6 @@ class Supabase {
storageOptions: storageOptions,
accessToken: accessToken,
);
_instance._debugEnable = debug ?? kDebugMode;
_instance.log('***** Supabase init completed *****');

_instance._supabaseAuth = SupabaseAuth();
await _instance._supabaseAuth.initialize(options: authOptions);
Expand All @@ -119,6 +135,8 @@ class Supabase {
_instance._supabaseAuth.recoverSession(),
);

_log.info('***** Supabase init completed *****');

return _instance;
}

Expand Down Expand Up @@ -175,13 +193,4 @@ class Supabase {
);
_initialized = true;
}

void log(String msg, [StackTrace? stackTrace]) {
if (_debugEnable) {
debugPrint(msg);
if (stackTrace != null) {
debugPrintStack(stackTrace: stackTrace);
}
}
}
}
40 changes: 18 additions & 22 deletions packages/supabase_flutter/lib/src/supabase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:async/async.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:logging/logging.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:url_launcher/url_launcher.dart';

Expand All @@ -34,6 +35,8 @@ class SupabaseAuth with WidgetsBindingObserver {

final _appLinks = AppLinks();

final _log = Logger('supabase.supabase_flutter');

/// - Obtains session from local storage and sets it as the current session
/// - Starts a deep link observer
/// - Emits an initial session if there were no session stored in local storage
Expand All @@ -48,9 +51,7 @@ class SupabaseAuth with WidgetsBindingObserver {
(data) {
_onAuthStateChange(data.event, data.session);
},
onError: (error, stackTrace) {
Supabase.instance.log(error.toString(), stackTrace);
},
onError: (error, stackTrace) {},
);

await _localStorage.initialize();
Expand All @@ -65,7 +66,8 @@ class SupabaseAuth with WidgetsBindingObserver {
.setInitialSession(persistedSession);
shouldEmitInitialSession = false;
} catch (error, stackTrace) {
Supabase.instance.log(error.toString(), stackTrace);
_log.warning(
'Error while setting initial session', error, stackTrace);
}
}
}
Expand Down Expand Up @@ -96,9 +98,9 @@ class SupabaseAuth with WidgetsBindingObserver {
}
}
} on AuthException catch (error, stackTrace) {
Supabase.instance.log(error.message, stackTrace);
_log.warning(error.message, error, stackTrace);
} catch (error, stackTrace) {
Supabase.instance.log(error.toString(), stackTrace);
_log.warning("Error while recovering session", error, stackTrace);
}
}

Expand Down Expand Up @@ -186,9 +188,7 @@ class SupabaseAuth with WidgetsBindingObserver {
}

void _onAuthStateChange(AuthChangeEvent event, Session? session) {
Supabase.instance.log('**** onAuthStateChange: $event');
if (session != null) {
Supabase.instance.log(jsonEncode(session.toJson()));
_localStorage.persistSession(jsonEncode(session.toJson()));
} else if (event == AuthChangeEvent.signedOut) {
_localStorage.removePersistedSession();
Expand All @@ -206,7 +206,7 @@ class SupabaseAuth with WidgetsBindingObserver {

/// Enable deep link observer to handle deep links
Future<void> _startDeeplinkObserver() async {
Supabase.instance.log('***** SupabaseDeepLinkingMixin startAuthObserver');
_log.fine('Starting deeplink observer');
_handleIncomingLinks();
await _handleInitialUri();
}
Expand All @@ -216,7 +216,7 @@ class SupabaseAuth with WidgetsBindingObserver {
/// Automatically called on dispose().
void _stopDeeplinkObserver() {
if (_deeplinkSubscription != null) {
Supabase.instance.log('***** SupabaseDeepLinkingMixin stopAuthObserver');
_log.fine('Stopping deeplink observer');
_deeplinkSubscription?.cancel();
}
}
Expand All @@ -234,7 +234,7 @@ class SupabaseAuth with WidgetsBindingObserver {
}
},
onError: (Object err, StackTrace stackTrace) {
_onErrorReceivingDeeplink(err.toString(), stackTrace);
_onErrorReceivingDeeplink(err, stackTrace);
},
);
}
Expand Down Expand Up @@ -273,39 +273,35 @@ class SupabaseAuth with WidgetsBindingObserver {
await _handleDeeplink(uri);
}
} on PlatformException catch (err, stackTrace) {
_onErrorReceivingDeeplink(err.message ?? err.toString(), stackTrace);
_onErrorReceivingDeeplink(err.message ?? err, stackTrace);
// Platform messages may fail but we ignore the exception
} on FormatException catch (err, stackTrace) {
_onErrorReceivingDeeplink(err.message, stackTrace);
} catch (err, stackTrace) {
_onErrorReceivingDeeplink(err.toString(), stackTrace);
_onErrorReceivingDeeplink(err, stackTrace);
}
}

/// Callback when deeplink receiving succeeds
Future<void> _handleDeeplink(Uri uri) async {
if (!_isAuthCallbackDeeplink(uri)) return;

Supabase.instance.log('***** SupabaseAuthState handleDeeplink $uri');

// notify auth deeplink received
Supabase.instance.log('onReceivedAuthDeeplink uri: $uri');
_log.fine('handle deeplink uri: $uri');
_log.info('handle deeplink uri');

try {
await Supabase.instance.client.auth.getSessionFromUrl(uri);
} on AuthException catch (error, stackTrace) {
// ignore: invalid_use_of_internal_member
Supabase.instance.client.auth.notifyException(error, stackTrace);
Supabase.instance.log(error.toString(), stackTrace);
} catch (error, stackTrace) {
Supabase.instance.log(error.toString(), stackTrace);
_log.warning('Error while getSessionFromUrl', error, stackTrace);
}
}

/// Callback when deeplink receiving throw error
void _onErrorReceivingDeeplink(String message, StackTrace stackTrace) {
Supabase.instance
.log('onErrorReceivingDeepLink message: $message', stackTrace);
void _onErrorReceivingDeeplink(Object error, StackTrace stackTrace) {
_log.warning('Error while receiving deeplink', error, stackTrace);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/supabase_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies:
url_launcher: ^6.1.2
path_provider: ^2.0.0
shared_preferences: ^2.0.0
logging: ^1.2.0

dev_dependencies:
dart_jsonwebtoken: ^2.4.1
Expand Down

0 comments on commit 7e67afc

Please sign in to comment.