Skip to content

Commit

Permalink
feat: add logging to supabase package
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Sep 26, 2024
1 parent 3575ac3 commit 5e6e0c9
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/gotrue/lib/src/gotrue_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:logging/logging.dart';

import 'broadcast_stub.dart' if (dart.library.html) './broadcast_web.dart'
as web;
import 'version.dart';

part 'gotrue_mfa_api.dart';

Expand Down Expand Up @@ -111,7 +112,7 @@ class GoTrueClient {
_autoRefreshToken = autoRefreshToken ?? true;

_log.config(
'GoTrueClient initialized with url: $_url, autoRefreshToken: $_autoRefreshToken, flowType: $_flowType, tickDuration: ${Constants.autoRefreshTickDuration}, tickThreshold: ${Constants.autoRefreshTickThreshold}');
'Initialize GoTrueClient v$version with url: $_url, autoRefreshToken: $_autoRefreshToken, flowType: $_flowType, tickDuration: ${Constants.autoRefreshTickDuration}, tickThreshold: ${Constants.autoRefreshTickThreshold}');

final gotrueUrl = url ?? Constants.defaultGotrueUrl;
final gotrueHeader = {
Expand Down Expand Up @@ -626,6 +627,7 @@ class GoTrueClient {
_log.warning("Can't refresh session, no current session found.");
throw AuthSessionMissingException();
}
_log.info('Refresh session');

final currentSessionRefreshToken =
refreshToken ?? _currentSession?.refreshToken;
Expand Down
1 change: 1 addition & 0 deletions packages/supabase/lib/src/auth_user.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:gotrue/gotrue.dart' show User;

@Deprecated('No longer used. May be removed in the future.')
class AuthUser extends User {
AuthUser({
required super.id,
Expand Down
1 change: 1 addition & 0 deletions packages/supabase/lib/src/remove_subscription_result.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:supabase/src/supabase_realtime_error.dart';

@Deprecated("No longer used. May be removed in the future.")
class RemoveSubscriptionResult {
const RemoveSubscriptionResult({required this.openSubscriptions, this.error});
final int openSubscriptions;
Expand Down
19 changes: 18 additions & 1 deletion packages/supabase/lib/src/supabase_client.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:async';

import 'package:http/http.dart';
import 'package:logging/logging.dart';
import 'package:supabase/src/constants.dart';
import 'package:supabase/src/version.dart';
import 'package:supabase/supabase.dart';
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';

Expand Down Expand Up @@ -66,6 +68,8 @@ class SupabaseClient {
/// Increment ID of the stream to create different realtime topic for each stream
final _incrementId = Counter();

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

/// Getter for the HTTP headers
Map<String, String> get headers {
return _headers;
Expand Down Expand Up @@ -145,7 +149,12 @@ class SupabaseClient {
storage = _initStorageClient(storageOptions.retryAttempts);
realtime = _initRealtimeClient(options: realtimeClientOptions);
if (accessToken == null) {
_log.config(
'Initialize SupabaseClient v$version with no custom access token');
_listenForAuthEvents();
} else {
_log.config(
'Initialize SupabaseClient v$version with custom access token');
}
}

Expand Down Expand Up @@ -223,6 +232,8 @@ class SupabaseClient {
return realtime.removeAllChannels();
}

/// Get either the custom access token from [accessToken] or the supabase one
/// from [_authInstance]
Future<String?> _getAccessToken() async {
if (accessToken != null) {
return await accessToken!();
Expand All @@ -231,14 +242,19 @@ class SupabaseClient {
if (_authInstance.currentSession?.isExpired ?? false) {
try {
await _authInstance.refreshSession();
} catch (error) {
} catch (error, stackTrace) {
final expiresAt = _authInstance.currentSession?.expiresAt;
if (expiresAt != null) {
// Failed to refresh the token.
final isExpiredWithoutMargin = DateTime.now()
.isAfter(DateTime.fromMillisecondsSinceEpoch(expiresAt * 1000));
if (isExpiredWithoutMargin) {
// Throw the error instead of making an API request with an expired token.
_log.warning(
'Access token is expired and refreshing failed, aborting api request',
error,
stackTrace,
);
rethrow;
}
}
Expand All @@ -248,6 +264,7 @@ class SupabaseClient {
}

Future<void> dispose() async {
_log.fine('Dispose SupabaseClient');
await _authStateSubscription?.cancel();
await _isolate.dispose();
auth.dispose();
Expand Down
2 changes: 2 additions & 0 deletions packages/supabase/lib/src/supabase_event_types.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@Deprecated('No longer used. May be removed in the future.')
enum SupabaseEventTypes { insert, update, delete, all, broadcast, presence }

// ignore: deprecated_member_use_from_same_package
extension SupabaseEventTypesName on SupabaseEventTypes {
String name() {
final name = toString().split('.').last;
Expand Down
1 change: 1 addition & 0 deletions packages/supabase/lib/src/supabase_realtime_error.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@Deprecated('No longer used. May be removed in the future.')
class SupabaseRealtimeError extends Error {
/// Creates an Unsubscribe error with the provided [message].
SupabaseRealtimeError([this.message]);
Expand Down
4 changes: 4 additions & 0 deletions packages/supabase/lib/src/supabase_stream_builder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart';
import 'package:supabase/supabase.dart';

Expand Down Expand Up @@ -49,6 +50,8 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
/// Used to identify which row has changed
final List<String> _uniqueColumns;

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

/// StreamController for `stream()` method.
BehaviorSubject<SupabaseStreamEvent>? _streamController;

Expand Down Expand Up @@ -129,6 +132,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
_getStreamData();
},
onCancel: () {
_log.fine('stream controller for table: $_table got closed');
_channel?.unsubscribe();
_streamController?.close();
_streamController = null;
Expand Down
1 change: 1 addition & 0 deletions packages/supabase/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
storage_client: 2.0.3
rxdart: '>=0.27.5 <0.29.0'
yet_another_json_isolate: 2.0.2
logging: ^1.2.0

dev_dependencies:
lints: ^3.0.0
Expand Down

0 comments on commit 5e6e0c9

Please sign in to comment.