From 6f50e899e82e2bccfe01fb05def4be08202a14f1 Mon Sep 17 00:00:00 2001 From: Kislay Kumar Date: Sat, 6 Apr 2024 11:56:17 +0530 Subject: [PATCH 1/2] Updated: colored stack trace on log --- .../_local/console_service.dart | 76 ++++++++++--------- .../logging_library/logging_library.dart | 2 +- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/lib/vaahextendflutter/services/logging_library/_local/console_service.dart b/lib/vaahextendflutter/services/logging_library/_local/console_service.dart index 748dd83..5bd685a 100644 --- a/lib/vaahextendflutter/services/logging_library/_local/console_service.dart +++ b/lib/vaahextendflutter/services/logging_library/_local/console_service.dart @@ -6,17 +6,20 @@ import 'package:flutter/material.dart'; import '../models/log.dart'; class Console { - static void _printChunks(Colorize text) { + static void _printLog(String text, [Set? logStyle]) { final RegExp pattern = RegExp('.{1,800}'); // 800 is the size of each chunk - pattern.allMatches(text.toString()).forEach( - (RegExpMatch match) => debugPrint( - match.group(0), - ), + pattern.allMatches(text).forEach((RegExpMatch match) { + if (logStyle == null || logStyle.isEmpty) { + return debugPrint( + match.group(0), ); - } + } - static void _printLog(Colorize text) { - _printChunks(text); + Colorize chunk = Colorize(match.group(0).toString()).apply(logStyle.first); + return debugPrint( + '$chunk', + ); + }); } static String _parseData(Object? data) { @@ -30,61 +33,60 @@ class Console { } static void log(String text, [Object? data]) { - Colorize txt = Colorize(text); - _printLog(txt); + _printLog(text); if (data != null) { - Colorize dataColor = Colorize(_parseData(data)); - dataColor.white(); - _printLog(dataColor); + _printLog(_parseData(data), { + Styles.WHITE, + }); } } static void info(String text, [Object? data]) { - Colorize txt = Colorize(text); - txt.blue(); - _printLog(txt); + _printLog(text, { + Styles.BLUE, + }); if (data != null) { - Colorize dataColor = Colorize(_parseData(data)); - dataColor.blue(); - _printLog(dataColor); + _printLog(_parseData(data), { + Styles.BLUE, + }); } } static void success(String text, [Object? data]) { - Colorize txt = Colorize(text); - txt.green(); - _printLog(txt); + _printLog(text, { + Styles.GREEN, + }); if (data != null) { - Colorize dataColor = Colorize(_parseData(data)); - dataColor.green(); - _printLog(dataColor); + _printLog(_parseData(data), { + Styles.GREEN, + }); } } static void warning(String text, [Object? data]) { - Colorize txt = Colorize(text); - txt.yellow(); - _printLog(txt); + _printLog(text, { + Styles.YELLOW, + }); if (data != null) { - Colorize dataColor = Colorize(_parseData(data)); - dataColor.yellow(); - _printLog(dataColor); + _printLog(_parseData(data), { + Styles.YELLOW, + }); } } static void danger(String text, [Object? data]) { - Colorize txt = Colorize(text); - txt.red(); - _printLog(txt); + _printLog(text, { + Styles.RED, + }); if (data != null) { - Colorize dataColor = Colorize(_parseData(data)); - dataColor.red(); - _printLog(dataColor); + _printLog(_parseData(data), { + Styles.RED, + }); } } diff --git a/lib/vaahextendflutter/services/logging_library/logging_library.dart b/lib/vaahextendflutter/services/logging_library/logging_library.dart index 3a254cc..da20ad1 100644 --- a/lib/vaahextendflutter/services/logging_library/logging_library.dart +++ b/lib/vaahextendflutter/services/logging_library/logging_library.dart @@ -77,7 +77,7 @@ class Log { bool disableCloudLogging = false, }) { if (_config.enableLocalLogs && !disableLocalLogging) { - Console.danger('$throwable\n$hint', data); + Console.danger('$throwable\n$hint\n$stackTrace', data); } if (_config.enableCloudLogs && !disableCloudLogging) { final hintWithData = { From 1c1f964f36ef0751f8984495139dab16330c8715 Mon Sep 17 00:00:00 2001 From: Chintan Prajapati Date: Mon, 27 May 2024 11:28:07 +0530 Subject: [PATCH 2/2] fixed: error, stacktrace, data, hint, & message --- lib/vaahextendflutter/env/env.dart | 6 +- lib/vaahextendflutter/services/api.dart | 13 +- .../services/dynamic_links.dart | 21 ++-- .../_cloud/firebase_logging_service.dart | 11 +- .../_cloud/sentry_logging_service.dart | 20 ++- .../_local/console_service.dart | 114 +++++++++++------- .../logging_library/logging_library.dart | 61 +++++----- .../internal/services/pusher.dart | 8 +- .../notification/push/services/local.dart | 9 +- .../pages/ui/components/inputs/complex.dart | 12 +- 10 files changed, 170 insertions(+), 105 deletions(-) diff --git a/lib/vaahextendflutter/env/env.dart b/lib/vaahextendflutter/env/env.dart index 82f0a54..1123053 100644 --- a/lib/vaahextendflutter/env/env.dart +++ b/lib/vaahextendflutter/env/env.dart @@ -32,7 +32,11 @@ class EnvController extends GetxController { throw Exception('Environment configuration not found for key: $envPath'); } } catch (error, stackTrace) { - Log.exception(error, stackTrace: stackTrace); + Log.exception( + "Error occured while initializing EnvController", + throwable: error, + stackTrace: stackTrace, + ); exit(0); } } diff --git a/lib/vaahextendflutter/services/api.dart b/lib/vaahextendflutter/services/api.dart index c816ddf..af9c921 100644 --- a/lib/vaahextendflutter/services/api.dart +++ b/lib/vaahextendflutter/services/api.dart @@ -348,7 +348,11 @@ abstract class Api { bool showAlert, String alertType, ) async { - Log.exception(error, stackTrace: error.stackTrace); + Log.exception( + "Handle Timeout Error", + throwable: error, + stackTrace: error.stackTrace, + ); if (showAlert) { if (alertType == 'dialog') { if (Alerts.showErrorDialog != null) { @@ -416,7 +420,12 @@ abstract class Api { if (error.response?.data != null) { try { - Log.exception(catchErr, data: error.response, stackTrace: stackTrace); + Log.exception( + "Handle Response Error", + throwable: catchErr, + hint: error.response, + stackTrace: stackTrace, + ); final Map response = error.response?.data as Map; if (response['errors'] != null) { diff --git a/lib/vaahextendflutter/services/dynamic_links.dart b/lib/vaahextendflutter/services/dynamic_links.dart index c5b4a69..2b6639f 100644 --- a/lib/vaahextendflutter/services/dynamic_links.dart +++ b/lib/vaahextendflutter/services/dynamic_links.dart @@ -46,7 +46,7 @@ abstract class DynamicLinks { shortLinkType: ShortDynamicLinkType.unguessable, ); } catch (error, stackTrace) { - Log.exception(error, stackTrace: stackTrace, hint: "Error creating dynamic link!"); + Log.exception("Error creating dynamic link!", throwable: error, stackTrace: stackTrace); return null; } } @@ -61,10 +61,13 @@ abstract class DynamicLinks { decoded: "${linkData.link.host}${linkData.link.path}?payload=$payload", ), ); - Log.success({ - "encoded": linkData.link.toString(), - "decoded": "${linkData.link.host}${linkData.link.path}?payload=$payload", - }); + Log.success( + "Handle Deeplink", + data: { + "encoded": linkData.link.toString(), + "decoded": "${linkData.link.host}${linkData.link.path}?payload=$payload", + }, + ); if (payload != null && payload['path'] != null) { Get.to( payload['path'], @@ -76,9 +79,9 @@ abstract class DynamicLinks { } } catch (error, stackTrace) { Log.exception( - error, + "Error handling dynamic link! ${linkData.asMap()}", + throwable: error, stackTrace: stackTrace, - hint: "Error handling dynamic link! ${linkData.asMap()}", ); } } @@ -88,9 +91,9 @@ abstract class DynamicLinks { return jsonDecode(link.queryParameters['payload'].toString()); } catch (error, stackTrace) { Log.exception( - error, + "Error decoding payload! $link", + throwable: error, stackTrace: stackTrace, - hint: "Error decoding payload! $link", ); return null; } diff --git a/lib/vaahextendflutter/services/logging_library/_cloud/firebase_logging_service.dart b/lib/vaahextendflutter/services/logging_library/_cloud/firebase_logging_service.dart index aaabaf2..d83206d 100644 --- a/lib/vaahextendflutter/services/logging_library/_cloud/firebase_logging_service.dart +++ b/lib/vaahextendflutter/services/logging_library/_cloud/firebase_logging_service.dart @@ -2,16 +2,17 @@ import '../models/log.dart'; import 'logging_service.dart'; abstract class FirebaseLoggingService implements LoggingService { - static logEvent({ - required String message, - EventType? type, + static logEvent( + String message, { Object? data, + EventType? type, }) => throw UnimplementedError(); static logException( - dynamic throwable, { - dynamic stackTrace, + String message, { + Object? throwable, + StackTrace? stackTrace, dynamic hint, }) => throw UnimplementedError(); diff --git a/lib/vaahextendflutter/services/logging_library/_cloud/sentry_logging_service.dart b/lib/vaahextendflutter/services/logging_library/_cloud/sentry_logging_service.dart index 653b86a..bdb4c2c 100644 --- a/lib/vaahextendflutter/services/logging_library/_cloud/sentry_logging_service.dart +++ b/lib/vaahextendflutter/services/logging_library/_cloud/sentry_logging_service.dart @@ -4,10 +4,10 @@ import '../models/log.dart'; import 'logging_service.dart'; abstract class SentryLoggingService implements LoggingService { - static logEvent({ - required String message, - SentryLevel? level, + static logEvent( + String message, { Object? data, + SentryLevel? level, }) { final SentryEvent event = SentryEvent(message: SentryMessage(message), level: level); Sentry.captureEvent( @@ -17,11 +17,19 @@ abstract class SentryLoggingService implements LoggingService { } static logException( - dynamic throwable, { - dynamic stackTrace, + String message, { + Object? throwable, + StackTrace? stackTrace, dynamic hint, }) { - Sentry.captureException(throwable, stackTrace: stackTrace, hint: hint); + Sentry.captureException( + throwable, + stackTrace: stackTrace, + hint: Hint.withMap({ + "message": message, + "hint": hint, + }), + ); } static logTransaction({ diff --git a/lib/vaahextendflutter/services/logging_library/_local/console_service.dart b/lib/vaahextendflutter/services/logging_library/_local/console_service.dart index 5bd685a..ef1b9aa 100644 --- a/lib/vaahextendflutter/services/logging_library/_local/console_service.dart +++ b/lib/vaahextendflutter/services/logging_library/_local/console_service.dart @@ -10,15 +10,10 @@ class Console { final RegExp pattern = RegExp('.{1,800}'); // 800 is the size of each chunk pattern.allMatches(text).forEach((RegExpMatch match) { if (logStyle == null || logStyle.isEmpty) { - return debugPrint( - match.group(0), - ); + return debugPrint(match.group(0)); } - Colorize chunk = Colorize(match.group(0).toString()).apply(logStyle.first); - return debugPrint( - '$chunk', - ); + return debugPrint('$chunk'); }); } @@ -32,61 +27,98 @@ class Console { } } - static void log(String text, [Object? data]) { - _printLog(text); + static void log( + String message, { + Object? data, + }) { + _printLog(message); if (data != null) { - _printLog(_parseData(data), { - Styles.WHITE, - }); + _printLog(_parseData(data)); } } - static void info(String text, [Object? data]) { - _printLog(text, { - Styles.BLUE, - }); + static void info( + String message, { + Object? data, + }) { + _printLog( + message, + {Styles.BLUE}, + ); if (data != null) { - _printLog(_parseData(data), { - Styles.BLUE, - }); + _printLog( + _parseData(data), + {Styles.BLUE}, + ); } } - static void success(String text, [Object? data]) { - _printLog(text, { - Styles.GREEN, - }); + static void success( + String message, { + Object? data, + }) { + _printLog( + message, + {Styles.GREEN}, + ); if (data != null) { - _printLog(_parseData(data), { - Styles.GREEN, - }); + _printLog( + _parseData(data), + {Styles.GREEN}, + ); } } - static void warning(String text, [Object? data]) { - _printLog(text, { - Styles.YELLOW, - }); + static void warning( + String message, { + Object? data, + }) { + _printLog( + message, + {Styles.YELLOW}, + ); if (data != null) { - _printLog(_parseData(data), { - Styles.YELLOW, - }); + _printLog( + _parseData(data), + {Styles.YELLOW}, + ); } } - static void danger(String text, [Object? data]) { - _printLog(text, { - Styles.RED, - }); + static void danger( + String message, { + Object? throwable, + StackTrace? stackTrace, + dynamic hint, + }) { + _printLog( + message, + {Styles.RED}, + ); + + if (throwable != null) { + _printLog( + _parseData(throwable), + {Styles.RED}, + ); + } - if (data != null) { - _printLog(_parseData(data), { - Styles.RED, - }); + if (stackTrace != null) { + _printLog( + stackTrace.toString(), + {Styles.RED}, + ); + } + + if (hint != null) { + _printLog( + _parseData(hint), + {Styles.RED}, + ); } } diff --git a/lib/vaahextendflutter/services/logging_library/logging_library.dart b/lib/vaahextendflutter/services/logging_library/logging_library.dart index da20ad1..91a7343 100644 --- a/lib/vaahextendflutter/services/logging_library/logging_library.dart +++ b/lib/vaahextendflutter/services/logging_library/logging_library.dart @@ -13,91 +13,94 @@ class Log { ]; static void log( - dynamic text, { + String message, { Object? data, bool disableLocalLogging = false, bool disableCloudLogging = false, }) { if (_config.enableLocalLogs && !disableLocalLogging) { - Console.log(text.toString(), data); + Console.log(message, data: data); } if (_config.enableCloudLogs && !disableCloudLogging) { - _logEvent(text.toString(), data: data, type: EventType.log); + _logEvent(message, data: data, type: EventType.log); } } static void info( - dynamic text, { + String message, { Object? data, bool disableLocalLogging = false, bool disableCloudLogging = false, }) { if (_config.enableLocalLogs && !disableLocalLogging) { - Console.info(text.toString(), data); + Console.info(message, data: data); } if (_config.enableCloudLogs && !disableCloudLogging) { - _logEvent(text.toString(), data: data, type: EventType.info); + _logEvent(message, data: data, type: EventType.info); } } static void success( - dynamic text, { + String message, { Object? data, bool disableLocalLogging = false, bool disableCloudLogging = false, }) { if (_config.enableLocalLogs && !disableLocalLogging) { - Console.success(text.toString(), data); + Console.success(message, data: data); } if (_config.enableCloudLogs && !disableCloudLogging) { - _logEvent(text.toString(), data: data, type: EventType.success); + _logEvent(message, data: data, type: EventType.success); } } static void warning( - dynamic text, { + String message, { Object? data, bool disableLocalLogging = false, bool disableCloudLogging = false, }) { if (_config.enableLocalLogs && !disableLocalLogging) { - Console.warning(text.toString(), data); + Console.warning(message, data: data); } if (_config.enableCloudLogs && !disableCloudLogging) { - _logEvent(text.toString(), data: data, type: EventType.warning); + _logEvent(message, data: data, type: EventType.warning); } } static void exception( - dynamic throwable, { - Object? data, - dynamic stackTrace, + String message, { + Object? throwable, + StackTrace? stackTrace, dynamic hint, bool disableLocalLogging = false, bool disableCloudLogging = false, }) { if (_config.enableLocalLogs && !disableLocalLogging) { - Console.danger('$throwable\n$hint\n$stackTrace', data); + Console.danger( + message, + throwable: throwable, + stackTrace: stackTrace, + hint: hint, + ); } if (_config.enableCloudLogs && !disableCloudLogging) { - final hintWithData = { - 'hint': hint, - 'data': data, - }; for (final service in _services) { switch (service) { case SentryLoggingService: SentryLoggingService.logException( - throwable, + message, + throwable: throwable, stackTrace: stackTrace, - hint: hintWithData, + hint: hint, ); return; case FirebaseLoggingService: FirebaseLoggingService.logException( - throwable, + message, + throwable: throwable, stackTrace: stackTrace, - hint: hintWithData, + hint: hint, ); return; default: @@ -139,7 +142,7 @@ class Log { } static void _logEvent( - String text, { + String message, { Object? data, EventType? type, }) { @@ -147,16 +150,16 @@ class Log { switch (service) { case SentryLoggingService: SentryLoggingService.logEvent( - message: text, - level: type?.toSentryLevel, + message, data: data, + level: type?.toSentryLevel, ); return; case FirebaseLoggingService: FirebaseLoggingService.logEvent( - message: text, - type: type, + message, data: data, + type: type, ); return; default: diff --git a/lib/vaahextendflutter/services/notification/internal/services/pusher.dart b/lib/vaahextendflutter/services/notification/internal/services/pusher.dart index b5a4618..ac95838 100644 --- a/lib/vaahextendflutter/services/notification/internal/services/pusher.dart +++ b/lib/vaahextendflutter/services/notification/internal/services/pusher.dart @@ -47,8 +47,8 @@ class InternalNotificationsWithPusher implements InternalNotificationsService { await _pusher.init( apiKey: environmentConfig.pusherConfig!.apiKey, cluster: environmentConfig.pusherConfig!.cluster, - onError: (message, code, error) => Log.exception(error, data: message), - onSubscriptionError: (message, error) => Log.exception(error, data: message), + onError: (message, code, error) => Log.exception(message, throwable: error), + onSubscriptionError: (message, error) => Log.exception(message, throwable: error), ); await _pusher.connect(); await _pusher.subscribe( @@ -92,9 +92,9 @@ class InternalNotificationsWithPusher implements InternalNotificationsService { _pendingNotificationsCountStreamController.add(count); } catch (error, stackTrace) { Log.exception( - error, + "Error parsing pusher internal notification", + throwable: error, stackTrace: stackTrace, - hint: "Error parsing pusher internal notification", ); } } diff --git a/lib/vaahextendflutter/services/notification/push/services/local.dart b/lib/vaahextendflutter/services/notification/push/services/local.dart index 1366477..1c31a49 100644 --- a/lib/vaahextendflutter/services/notification/push/services/local.dart +++ b/lib/vaahextendflutter/services/notification/push/services/local.dart @@ -78,7 +78,12 @@ abstract class LocalNotifications { // static Future _handleSubscriptionStateChanges() async {} static void _handleNotification({required String? payload, String? actionId}) { - Log.info(payload); - Log.info(actionId); + Log.info( + "LocalNotification Payload", + data: { + "payload": payload, + "actionId": actionId, + }, + ); } } diff --git a/lib/views/pages/ui/components/inputs/complex.dart b/lib/views/pages/ui/components/inputs/complex.dart index 227f7f4..c967680 100644 --- a/lib/views/pages/ui/components/inputs/complex.dart +++ b/lib/views/pages/ui/components/inputs/complex.dart @@ -117,7 +117,7 @@ class InputSliderPreview extends StatelessWidget { Text('basic slider', style: normal), InputSlider( initialValue: 0.8, - onChanged: (value) => Log.info(value, disableCloudLogging: true), + onChanged: (value) => Log.info("Slider Value: $value", disableCloudLogging: true), ), Text('with input slider', style: normal), InputSlider( @@ -126,7 +126,7 @@ class InputSliderPreview extends StatelessWidget { initialValue: 50, step: 2, forceInputBox: true, - onChanged: (value) => Log.info(value, disableCloudLogging: true), + onChanged: (value) => Log.info("Slider Value: $value", disableCloudLogging: true), ), Text('step', style: normal), InputSlider( @@ -134,7 +134,7 @@ class InputSliderPreview extends StatelessWidget { min: 0, max: 100, step: 20, - onChanged: (value) => Log.info(value, disableCloudLogging: true), + onChanged: (value) => Log.info("Slider Value: $value", disableCloudLogging: true), ), Text('decimal step', style: normal), InputSlider( @@ -142,14 +142,14 @@ class InputSliderPreview extends StatelessWidget { min: 0, max: 10, step: 0.5, - onChanged: (value) => Log.info(value, disableCloudLogging: true), + onChanged: (value) => Log.info("Slider Value: $value", disableCloudLogging: true), ), Text('vertical slider', style: normal), Padding( padding: verticalPadding24, child: InputSlider( initialValue: 0, - onChanged: (value) => Log.info(value, disableCloudLogging: true), + onChanged: (value) => Log.info("Slider Value: $value", disableCloudLogging: true), forceVertical: true, ), ), @@ -159,7 +159,7 @@ class InputSliderPreview extends StatelessWidget { max: 10, initialValues: const RangeValues(2, 6), step: 0.1, - onChanged: (value) => Log.info(value, disableCloudLogging: true), + onChanged: (value) => Log.info("Slider Value: $value", disableCloudLogging: true), precision: 1, ), ],