Skip to content

Commit

Permalink
feat: disable logs if not debug
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-endymion committed Jan 9, 2025
1 parent 213993b commit 0588352
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 45 deletions.
12 changes: 4 additions & 8 deletions lib/app/features/core/providers/dio_provider.c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

import 'package:dio/dio.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:ion/app/features/core/model/feature_flags.dart';
import 'package:ion/app/features/core/providers/feature_flags_provider.c.dart';
import 'package:ion/app/services/logger/logger.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'dio_provider.c.g.dart';

@riverpod
Dio dio(Ref ref) {
final logApp = ref.read(featureFlagsProvider.notifier).get(LoggerFeatureFlag.logApp);

final dio = Dio();

if (logApp) {
dio.interceptors.add(
Logger.talkerDioLogger,
);
final logger = Logger.talkerDioLogger;

if (logger != null) {
dio.interceptors.add(logger);
}

return dio;
Expand Down
11 changes: 7 additions & 4 deletions lib/app/features/core/providers/feature_flags_provider.c.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: ice License 1.0

import 'package:ion/app/features/core/model/feature_flags.dart';
import 'package:ion/app/features/core/providers/env_provider.c.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'feature_flags_provider.c.g.dart';
Expand All @@ -17,10 +18,12 @@ class FeatureFlags extends _$FeatureFlags {
HideCreatorsWithoutPicture.hideCreatorsWithoutPicture: true,

/// Log flags
LoggerFeatureFlag.logApp: true,
LoggerFeatureFlag.logRouters: false,
LoggerFeatureFlag.logNostrDart: true,
LoggerFeatureFlag.logIonIdentityClient: true,
if (ref.read(envProvider.notifier).get(EnvVariable.SHOW_DEBUG_INFO)) ...{
LoggerFeatureFlag.logApp: true,
LoggerFeatureFlag.logRouters: false,
LoggerFeatureFlag.logNostrDart: true,
LoggerFeatureFlag.logIonIdentityClient: true,
},
};
}

Expand Down
11 changes: 7 additions & 4 deletions lib/app/features/core/providers/init_provider.c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:ion/app/features/core/providers/feature_flags_provider.c.dart';
import 'package:ion/app/features/core/providers/template_provider.c.dart';
import 'package:ion/app/features/core/providers/window_manager_provider.c.dart';
import 'package:ion/app/features/wallet/data/coins/domain/coin_initializer.c.dart';
import 'package:ion/app/services/logger/logger.dart';
import 'package:ion/app/services/nostr/nostr.dart';
import 'package:ion/app/services/nostr/nostr_logger.dart';
import 'package:ion/app/services/storage/local_storage.c.dart';
Expand All @@ -18,11 +19,13 @@ part 'init_provider.c.g.dart';

@Riverpod(keepAlive: true)
Future<void> initApp(Ref ref) async {
final logNostrDart = ref.read(featureFlagsProvider.notifier).get(LoggerFeatureFlag.logNostrDart);
final featureFlagsNotifier = ref.read(featureFlagsProvider.notifier);
final logApp = featureFlagsNotifier.get(LoggerFeatureFlag.logApp);
final logNostrDart = featureFlagsNotifier.get(LoggerFeatureFlag.logNostrDart);

Nostr.initialize(
logNostrDart ? NostrLogger() : null,
);
if (logApp) Logger.init();

Nostr.initialize(logNostrDart ? NostrLogger() : null);

await Future.wait([
ref.read(windowManagerProvider.notifier).show(),
Expand Down
5 changes: 4 additions & 1 deletion lib/app/features/core/views/pages/error_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import 'package:ion/app/components/screen_offset/screen_side_offset.dart';
import 'package:ion/app/exceptions/exceptions.dart';
import 'package:ion/app/extensions/extensions.dart';
import 'package:ion/app/features/core/providers/env_provider.c.dart';
import 'package:ion/app/services/logger/logger.dart';
import 'package:ion/generated/assets.gen.dart';

class ErrorModal extends ConsumerWidget {
const ErrorModal({required this.error, super.key});
ErrorModal({required this.error, super.key}) {
Logger.error(error);
}

final Object error;

Expand Down
24 changes: 13 additions & 11 deletions lib/app/features/debug/views/debug_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DebugPage extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final featureFlags = ref.watch(featureFlagsProvider);
final talker = Logger.talker;

return Column(
mainAxisSize: MainAxisSize.min,
Expand All @@ -30,21 +31,22 @@ class DebugPage extends ConsumerWidget {
child: ListView(
shrinkWrap: true,
children: [
Card(
child: ListTile(
leading: const Icon(Icons.bug_report),
title: const Text('View Debug Logs'),
subtitle: const Text('Check application logs and diagnostics'),
trailing: const Icon(Icons.chevron_right),
onTap: () => Navigator.of(context).push(
MaterialPageRoute<TalkerScreen>(
builder: (context) => TalkerScreen(
talker: Logger.talker,
if (talker != null)
Card(
child: ListTile(
leading: const Icon(Icons.bug_report),
title: const Text('View Debug Logs'),
subtitle: const Text('Check application logs and diagnostics'),
trailing: const Icon(Icons.chevron_right),
onTap: () => Navigator.of(context).push(
MaterialPageRoute<TalkerScreen>(
builder: (context) => TalkerScreen(
talker: talker,
),
),
),
),
),
),
SizedBox(height: 16.0.s),
ExpansionTile(
title: const Text('Feature Flags'),
Expand Down
44 changes: 27 additions & 17 deletions lib/app/services/logger/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,49 @@ import 'package:talker_flutter/talker_flutter.dart';
class Logger {
Logger._();

static final Talker talker = TalkerFlutter.init();

static final talkerDioLogger = TalkerDioLogger(
talker: talker,
settings: TalkerDioLoggerSettings(
printRequestHeaders: true,
printResponseHeaders: true,
requestPen: AnsiPen()..cyan(),
responsePen: AnsiPen()..green(),
errorPen: AnsiPen()..red(),
),
);
static Talker? _talker;

static void init() {
_talker = TalkerFlutter.init();
}

static Talker? get talker => _talker;

static TalkerDioLogger? get talkerDioLogger => TalkerDioLogger(
talker: talker,
settings: TalkerDioLoggerSettings(
printRequestHeaders: true,
printResponseHeaders: true,
requestPen: AnsiPen()..cyan(),
responsePen: AnsiPen()..green(),
errorPen: AnsiPen()..red(),
),
);

static void log(
String message, {
Object? error,
StackTrace? stackTrace,
}) {
talker.log(message);
_talker?.log(message);

if (error != null) {
talker.error(error, stackTrace);
_talker?.error(error, stackTrace);
}
}

static void info(String message) => talker.info(message);
static void info(String message) {
_talker?.info(message);
}

static void warning(String message) => talker.warning(message);
static void warning(String message) {
_talker?.warning(message);
}

static void error(
Object error, {
StackTrace? stackTrace,
}) {
talker.error(error, stackTrace);
_talker?.error(error, stackTrace);
}
}

0 comments on commit 0588352

Please sign in to comment.