From f67ed88037f49d4836603bf9a41b10cea3bb7374 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 23 Jul 2024 21:29:02 -0500 Subject: [PATCH] Add preliminary app logging --- waydowntown/waydowntown_app/lib/main.dart | 33 ++++++++++++++++++----- waydowntown/waydowntown_app/pubspec.lock | 8 ++++++ waydowntown/waydowntown_app/pubspec.yaml | 1 + 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/waydowntown/waydowntown_app/lib/main.dart b/waydowntown/waydowntown_app/lib/main.dart index cddb73ea..a41765d2 100644 --- a/waydowntown/waydowntown_app/lib/main.dart +++ b/waydowntown/waydowntown_app/lib/main.dart @@ -1,10 +1,20 @@ import 'package:dio/dio.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:pretty_dio_logger/pretty_dio_logger.dart'; +import 'package:logging/logging.dart'; + +final logger = Logger('main'); Future main() async { await dotenv.load(fileName: '.env'); + Logger.root.level = kDebugMode ? Level.ALL : Level.INFO; + Logger.root.onRecord.listen((record) { + // ignore: avoid_print + print( + '${record.time} [${record.loggerName}] ${record.level.name}: ${record.message}'); + }); runApp(const Waydowntown()); } @@ -37,6 +47,20 @@ class _MyHomePageState extends State { @override Widget build(BuildContext context) { final dio = Dio(BaseOptions()); + final logger = Logger('dio'); + + dio.interceptors.add(PrettyDioLogger( + requestHeader: true, + requestBody: true, + responseBody: true, + responseHeader: false, + error: true, + compact: true, + maxWidth: 90, + logPrint: (message) { + logger.fine(message); + })); + return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, @@ -98,7 +122,7 @@ class _RequestGameRouteState extends State { throw Exception('Failed to load game'); } } catch (error) { - print('Error fetching game from $endpoint: $error'); + logger.severe('Error fetching game from $endpoint: $error'); } } @@ -125,7 +149,7 @@ class _RequestGameRouteState extends State { game = Game.fromJson(response.data); }); } catch (error) { - print('Error submitting answer: $error'); + logger.severe('Error submitting answer: $error'); } } @@ -163,7 +187,6 @@ class _RequestGameRouteState extends State { ElevatedButton( onPressed: () async { await submitAnswer(answer); - print("game over? ${game!.isOver}"); }, child: const Text('Submit'), ) @@ -215,10 +238,6 @@ class Game { ? _findIncluded(json['included'], 'games') : json['data']; - print("gameJson: $gameJson"); - print( - "isover ${gameJson['relationships']?['winner_answer']?['links']?['related'] != null ? true : false}"); - return Game( id: gameJson['id'], incarnation: diff --git a/waydowntown/waydowntown_app/pubspec.lock b/waydowntown/waydowntown_app/pubspec.lock index 78376d51..d95dd29b 100644 --- a/waydowntown/waydowntown_app/pubspec.lock +++ b/waydowntown/waydowntown_app/pubspec.lock @@ -178,6 +178,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.0" + logging: + dependency: "direct main" + description: + name: logging + sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340" + url: "https://pub.dev" + source: hosted + version: "1.2.0" matcher: dependency: transitive description: diff --git a/waydowntown/waydowntown_app/pubspec.yaml b/waydowntown/waydowntown_app/pubspec.yaml index 663e1141..7bad76bc 100644 --- a/waydowntown/waydowntown_app/pubspec.yaml +++ b/waydowntown/waydowntown_app/pubspec.yaml @@ -39,6 +39,7 @@ dependencies: cupertino_icons: ^1.0.6 http: ^1.2.1 pretty_dio_logger: ^1.3.1 + logging: ^1.2.0 dev_dependencies: flutter_test: