feat(logging): add logging to auth feature

This commit is contained in:
qzl
2026-04-01 14:28:30 +08:00
parent 2b12d1ef79
commit 63d225c567
5 changed files with 138 additions and 15 deletions
+2 -2
View File
@@ -96,7 +96,7 @@ class LogService {
void info({
required String message,
required String module,
required Map<String, dynamic> extra,
Map<String, dynamic>? extra,
StackTrace? stackTrace,
}) {
final trace = stackTrace ?? StackTrace.current;
@@ -118,7 +118,7 @@ class LogService {
void warning({
required String message,
required String module,
required Map<String, dynamic> extra,
Map<String, dynamic>? extra,
StackTrace? stackTrace,
}) {
final trace = stackTrace ?? StackTrace.current;
+4 -4
View File
@@ -33,23 +33,23 @@ class Logger {
void info({
required String message,
required Map<String, dynamic> extra,
Map<String, dynamic>? extra,
StackTrace? stackTrace,
}) => _service.info(
message: message,
module: module,
extra: extra,
extra: extra ?? {},
stackTrace: stackTrace,
);
void warning({
required String message,
required Map<String, dynamic> extra,
Map<String, dynamic>? extra,
StackTrace? stackTrace,
}) => _service.warning(
message: message,
module: module,
extra: extra,
extra: extra ?? {},
stackTrace: stackTrace,
);
@@ -1,10 +1,12 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../core/logging/logger.dart';
import '../../data/repositories/auth_repository.dart';
import 'auth_event.dart';
import 'auth_state.dart';
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository _repository;
final Logger _logger = getLogger('features.auth.bloc');
AuthBloc(this._repository) : super(AuthInitial()) {
on<AuthStarted>(_onStarted);
@@ -19,6 +21,10 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
final refreshToken = await _repository.getRefreshToken();
if (refreshToken != null) {
final response = await _repository.refreshSession(refreshToken);
_logger.info(
message: 'Session refreshed successfully',
extra: {'user_id': response.user.id},
);
emit(
AuthAuthenticated(
user: AuthUser(id: response.user.id, phone: response.user.phone),
@@ -29,11 +35,20 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
emit(
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
);
} catch (_) {
} catch (e, stackTrace) {
_logger.error(
message: 'Session refresh failed',
error: e,
stackTrace: stackTrace,
);
try {
await _repository.clearSessionLocalOnly();
} catch (_) {
// Keep state convergence even when storage cleanup fails.
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to clear local session',
error: e,
stackTrace: stackTrace,
);
} finally {
emit(
const AuthUnauthenticated(
@@ -45,6 +60,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
}
void _onLoggedIn(AuthLoggedIn event, Emitter<AuthState> emit) {
_logger.info(message: 'User logged in', extra: {'user_id': event.user.id});
emit(AuthAuthenticated(user: event.user));
}
@@ -54,8 +70,13 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
) async {
try {
await _repository.deleteSession();
} catch (_) {
// Keep state convergence even when logout cleanup fails.
_logger.info(message: 'User logged out');
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to delete session on logout',
error: e,
stackTrace: stackTrace,
);
} finally {
emit(
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.signedOut),
@@ -67,10 +88,15 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
AuthSessionInvalidated event,
Emitter<AuthState> emit,
) async {
_logger.warning(message: 'Session invalidated by server');
try {
await _repository.clearSessionLocalOnly();
} catch (_) {
// Keep state convergence even when local cleanup fails.
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to clear local session',
error: e,
stackTrace: stackTrace,
);
} finally {
emit(
const AuthUnauthenticated(reason: AuthUnauthenticatedReason.expired),
@@ -5,6 +5,7 @@ import 'package:formz/formz.dart';
import 'package:equatable/equatable.dart';
import '../../../../data/network/api_exception.dart';
import '../../../../core/l10n/l10n.dart';
import '../../../../core/logging/logger.dart';
import '../../data/repositories/auth_repository.dart';
import '../../data/models/auth_response.dart';
import '../../../../shared/forms/inputs.dart';
@@ -78,6 +79,7 @@ class LoginState extends Equatable {
class LoginCubit extends Cubit<LoginState> {
final AuthRepository _repository;
final Logger _logger = getLogger('features.auth.login');
Timer? _resendTimer;
LoginCubit(this._repository) : super(const LoginState());
@@ -149,10 +151,16 @@ class LoginCubit extends Cubit<LoginState> {
),
);
return true;
} catch (e) {
} catch (e, stackTrace) {
if (isClosed) {
return false;
}
_logger.error(
message: 'Failed to send OTP',
error: e,
stackTrace: stackTrace,
extra: {'phone': requestPhone},
);
final message = e is ApiException
? e.message
: L10n.current.authSendCodeFailed;
@@ -176,10 +184,11 @@ class LoginCubit extends Cubit<LoginState> {
}
emit(state.copyWith(status: FormzSubmissionStatus.success));
return response;
} catch (e) {
} catch (e, stackTrace) {
if (isClosed) {
return null;
}
_logger.error(message: 'Login failed', error: e, stackTrace: stackTrace);
final message = e is ApiException ? e.message : e.toString();
emit(
state.copyWith(