Skip to content

Commit

Permalink
Add preliminary app logging
Browse files Browse the repository at this point in the history
  • Loading branch information
backspace committed Jul 24, 2024
1 parent 73dddbc commit f67ed88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
33 changes: 26 additions & 7 deletions waydowntown/waydowntown_app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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());
}

Expand Down Expand Up @@ -37,6 +47,20 @@ class _MyHomePageState extends State<MyHomePage> {
@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,
Expand Down Expand Up @@ -98,7 +122,7 @@ class _RequestGameRouteState extends State<RequestGameRoute> {
throw Exception('Failed to load game');
}
} catch (error) {
print('Error fetching game from $endpoint: $error');
logger.severe('Error fetching game from $endpoint: $error');
}
}

Expand All @@ -125,7 +149,7 @@ class _RequestGameRouteState extends State<RequestGameRoute> {
game = Game.fromJson(response.data);
});
} catch (error) {
print('Error submitting answer: $error');
logger.severe('Error submitting answer: $error');
}
}

Expand Down Expand Up @@ -163,7 +187,6 @@ class _RequestGameRouteState extends State<RequestGameRoute> {
ElevatedButton(
onPressed: () async {
await submitAnswer(answer);
print("game over? ${game!.isOver}");
},
child: const Text('Submit'),
)
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions waydowntown/waydowntown_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions waydowntown/waydowntown_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f67ed88

Please sign in to comment.