Skip to content

Commit bc95a9e

Browse files
committed
refactor(token_blacklist_service): replace print statements with logging
- Add logging dependency and integrate Logger into InMemoryTokenBlacklistService - Replace all print statements with appropriate log levels - Update constructor to require Logger instance - Adjust log messages to follow consistent formatting
1 parent f524e4f commit bc95a9e

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

lib/src/services/token_blacklist_service.dart

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22

33
import 'package:ht_shared/ht_shared.dart';
44
import 'package:meta/meta.dart';
5+
import 'package:logging/logging.dart';
56

67
/// {@template token_blacklist_service}
78
/// Defines the interface for a service that manages a blacklist of
@@ -51,19 +52,20 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
5152
/// expired token IDs. Defaults to 1 hour.
5253
InMemoryTokenBlacklistService({
5354
Duration cleanupInterval = const Duration(hours: 1),
54-
}) {
55+
required Logger log,
56+
}) : _log = log {
5557
_cleanupTimer = Timer.periodic(cleanupInterval, (_) async {
5658
try {
5759
await cleanupExpired();
5860
} catch (e) {
5961
// Log error during cleanup, but don't let it crash the timer
60-
print(
61-
'[InMemoryTokenBlacklistService] Error during scheduled cleanup: $e',
62+
_log.severe(
63+
'Error during scheduled cleanup: $e',
6264
);
6365
}
6466
});
65-
print(
66-
'[InMemoryTokenBlacklistService] Initialized with cleanup interval: '
67+
_log.info(
68+
'Initialized with cleanup interval: '
6769
'$cleanupInterval',
6870
);
6971
}
@@ -73,26 +75,27 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
7375
final Map<String, DateTime> blacklistStore = {};
7476
Timer? _cleanupTimer;
7577
bool _isDisposed = false;
78+
final Logger _log;
7679

7780
@override
7881
Future<void> blacklist(String jti, DateTime expiry) async {
7982
if (_isDisposed) {
80-
print(
81-
'[InMemoryTokenBlacklistService] Attempted to blacklist on disposed service.',
83+
_log.warning(
84+
'Attempted to blacklist on disposed service.',
8285
);
8386
return;
8487
}
8588
// Simulate async operation
8689
await Future<void>.delayed(Duration.zero);
8790
try {
8891
blacklistStore[jti] = expiry;
89-
print(
90-
'[InMemoryTokenBlacklistService] Blacklisted jti: $jti '
92+
_log.info(
93+
'Blacklisted jti: $jti '
9194
'(expires: $expiry)',
9295
);
9396
} catch (e) {
94-
print(
95-
'[InMemoryTokenBlacklistService] Error adding jti $jti to store: $e',
97+
_log.severe(
98+
'Error adding jti $jti to store: $e',
9699
);
97100
throw OperationFailedException('Failed to add token to blacklist: $e');
98101
}
@@ -101,8 +104,8 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
101104
@override
102105
Future<bool> isBlacklisted(String jti) async {
103106
if (_isDisposed) {
104-
print(
105-
'[InMemoryTokenBlacklistService] Attempted to check blacklist on disposed service.',
107+
_log.warning(
108+
'Attempted to check blacklist on disposed service.',
106109
);
107110
return false;
108111
}
@@ -122,8 +125,8 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
122125
}
123126
return true; // It's in the blacklist and not expired
124127
} catch (e) {
125-
print(
126-
'[InMemoryTokenBlacklistService] Error checking blacklist for jti $jti: $e',
128+
_log.severe(
129+
'Error checking blacklist for jti $jti: $e',
127130
);
128131
throw OperationFailedException('Failed to check token blacklist: $e');
129132
}
@@ -132,8 +135,8 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
132135
@override
133136
Future<void> cleanupExpired() async {
134137
if (_isDisposed) {
135-
print(
136-
'[InMemoryTokenBlacklistService] Attempted cleanup on disposed service.',
138+
_log.warning(
139+
'Attempted cleanup on disposed service.',
137140
);
138141
return;
139142
}
@@ -150,17 +153,17 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
150153

151154
if (expiredKeys.isNotEmpty) {
152155
expiredKeys.forEach(blacklistStore.remove);
153-
print(
154-
'[InMemoryTokenBlacklistService] Cleaned up ${expiredKeys.length} '
156+
_log.info(
157+
'Cleaned up ${expiredKeys.length} '
155158
'expired jti entries.',
156159
);
157160
} else {
158-
print(
159-
'[InMemoryTokenBlacklistService] Cleanup ran, no expired entries found.',
161+
_log.finer(
162+
'Cleanup ran, no expired entries found.',
160163
);
161164
}
162165
} catch (e) {
163-
print('[InMemoryTokenBlacklistService] Error during cleanup process: $e');
166+
_log.severe('Error during cleanup process: $e');
164167
// Optionally rethrow or handle as an internal error
165168
// For now, just log it to prevent crashing the cleanup timer.
166169
}
@@ -172,7 +175,7 @@ class InMemoryTokenBlacklistService implements TokenBlacklistService {
172175
_isDisposed = true;
173176
_cleanupTimer?.cancel();
174177
blacklistStore.clear();
175-
print('[InMemoryTokenBlacklistService] Disposed.');
178+
_log.info('Disposed.');
176179
}
177180
}
178181
}

0 commit comments

Comments
 (0)