From f56337e393118f4374551a4f2a2a524a1dd19085 Mon Sep 17 00:00:00 2001 From: kyle Date: Thu, 12 Oct 2023 08:13:20 -0700 Subject: [PATCH 1/4] fix(logging): example app init logic --- .../lib/amplifyconfiguration_logging.dart | 8 +- .../example/lib/main.dart | 78 ++++++++++++------- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/amplifyconfiguration_logging.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/amplifyconfiguration_logging.dart index 24eb1c1719..9efb303398 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/amplifyconfiguration_logging.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/amplifyconfiguration_logging.dart @@ -2,14 +2,10 @@ const loggingconfig = '''{ "plugins": { "cloudWatchLoggerPluginConfiguration": { "enable": true, - "logGroupName": "", - "region": "", + "logGroupName": "test-logging-log-group", + "region": "us-west-2", "localStoreMaxSizeInMB": 5, "flushIntervalInSeconds": 60, - "defaultRemoteConfiguration": { - "endpoint": "https://.execute-api..amazonaws.com/prod/loggingconstraints", - "refreshIntervalInSeconds": 1200 - }, "loggingConstraints": { "defaultLogLevel": "ERROR", "categoryLogLevel": { diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/main.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/main.dart index be44d7a642..63468ed5a1 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/main.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/main.dart @@ -8,31 +8,43 @@ import 'package:amplify_logging_cloudwatch_example/amplifyconfiguration_logging. import 'package:amplify_logging_cloudwatch_example/homepage.dart'; import 'package:flutter/material.dart'; -Future main() async { - WidgetsFlutterBinding.ensureInitialized(); - final authPlugin = AmplifyAuthCognito(); +void main() async { + runApp(const MyApp()); +} - final amplifyConfigWithLogging = AmplifyConfig.fromJson( - jsonDecode(amplifyconfig) as Map, - ).copyWith( - logging: LoggingConfig.fromJson( - jsonDecode(loggingconfig) as Map, - ), - ); +class MyApp extends StatefulWidget { + const MyApp({super.key}); - final amplifyConfig = - const JsonEncoder().convert(amplifyConfigWithLogging.toJson()); + @override + State createState() => _MyAppState(); +} - await Amplify.addPlugin(authPlugin); - await Amplify.configure(amplifyConfig); +class _MyAppState extends State { + final Future _initialization = _configureAmplify(); - runApp( - const MyApp(), - ); -} + @override + void initState() { + super.initState(); + } + + static Future _configureAmplify() async { + final authPlugin = AmplifyAuthCognito(); + + final amplifyConfigWithLogging = AmplifyConfig.fromJson( + jsonDecode(amplifyconfig) as Map, + ).copyWith( + logging: LoggingConfig.fromJson( + jsonDecode(loggingconfig) as Map, + ), + ); + + final amplifyConfig = + const JsonEncoder().convert(amplifyConfigWithLogging.toJson()); + + await Amplify.addPlugin(authPlugin); + await Amplify.configure(amplifyConfig); + } -class MyApp extends StatelessWidget { - const MyApp({super.key}); @override Widget build(BuildContext context) { return Authenticator( @@ -40,21 +52,32 @@ class MyApp extends StatelessWidget { initialRoute: '/', routes: { '/': (BuildContext context) { - return InitPage( - key: GlobalKey(), - title: 'Amplify Logging Cloudwatch Example', + return FutureBuilder( + future: _initialization, + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + Amplify.Auth.signOut(); + return const InitPage( + title: 'Amplify Logging Cloudwatch Example', + ); + } else { + return const Scaffold( + body: Center( + child: CircularProgressIndicator(), + ), + ); + } + }, ); }, '/home': (BuildContext context) { - return MyHomePage( - key: GlobalKey(), + return const MyHomePage( title: 'Amplify Logging Cloudwatch Example', ); }, '/login': (BuildContext context) { - return AuthenticatedView( + return const AuthenticatedView( child: MyHomePage( - key: GlobalKey(), title: 'Amplify Logging Cloudwatch Example', ), ); @@ -70,7 +93,6 @@ class InitPage extends StatelessWidget { final String title; @override Widget build(BuildContext context) { - Amplify.Auth.signOut(); return Scaffold( appBar: AppBar( title: Text(title), From fdb501eb174336f73bce1d9a47eba02b577da88c Mon Sep 17 00:00:00 2001 From: kyle Date: Thu, 12 Oct 2023 11:29:31 -0700 Subject: [PATCH 2/4] fix(logging app) --- .../example/lib/homepage.dart | 500 +++++------------- .../example/lib/main.dart | 4 +- 2 files changed, 138 insertions(+), 366 deletions(-) diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/homepage.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/homepage.dart index ad9301e56e..2040cef11c 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/homepage.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/homepage.dart @@ -10,40 +10,51 @@ class MyHomePage extends StatefulWidget { State createState() => _MyHomePageState(); } -class _MyHomePageState extends State { - final _plugin = AmplifyLogger().getPlugin()!; - - final _authLogger = AmplifyLogger.category(Category.auth); - LogLevel? _authLogLevel; - final TextEditingController _authLogMsgController = TextEditingController(); - - final _analyticsLogger = AmplifyLogger.category(Category.analytics); - LogLevel? _analyticsLogLevel; - final TextEditingController _analyticsLogMsgController = - TextEditingController(); - - final _apiLogger = AmplifyLogger.category(Category.api); - LogLevel? _apiLogLevel; - final TextEditingController _apiLogMsgController = TextEditingController(); +class LoggerInfo { + LoggerInfo( + this.category, + this.logger, + this.logMsgController, { + this.logLevel = LogLevel.verbose, + }); + final Category category; + final AmplifyLogger logger; + final TextEditingController logMsgController; + LogLevel? logLevel; - final _dataStoreLogger = AmplifyLogger.category(Category.dataStore); - LogLevel? _dataStoreLogLevel; - final TextEditingController _dataStoreLogMsgController = - TextEditingController(); + LoggerInfo copyWith({ + Category? category, + AmplifyLogger? logger, + TextEditingController? logMsgController, + LogLevel? logLevel, + }) { + return LoggerInfo( + category ?? this.category, + logger ?? this.logger, + logMsgController ?? this.logMsgController, + logLevel: logLevel ?? this.logLevel, + ); + } +} - final _hubLogger = AmplifyLogger.category(Category.hub); - LogLevel? _hubLogLevel; - final TextEditingController _hubLogMsgController = TextEditingController(); +class _MyHomePageState extends State { + final _loggerMap = {}; - final _pnLogger = AmplifyLogger.category(Category.pushNotifications); - LogLevel? _pnLogLevel; - final TextEditingController _pnLogMsgController = TextEditingController(); + @override + void initState() { + super.initState(); - final _storageLogger = AmplifyLogger.category(Category.storage); - LogLevel? _storageLogLevel; - final TextEditingController _storageLogMsgController = - TextEditingController(); + // Create LoggerInfo for each Category + for (final category in Category.values) { + _loggerMap[category] = LoggerInfo( + category, + AmplifyLogger.category(category), + TextEditingController(), + ); + } + } + final _plugin = AmplifyLogger().getPlugin()!; LogLevel? _customLogLevel; final TextEditingController _customLogMsgController = TextEditingController(); final TextEditingController _customLoggerNameController = @@ -60,20 +71,19 @@ class _MyHomePageState extends State { ), onPressed: () { Amplify.Auth.signOut(); - Navigator.pushNamed(context, '/'); + Navigator.popAndPushNamed(context, '/'); }, ), title: Text(widget.title), ), body: SingleChildScrollView( - scrollDirection: Axis.horizontal, + scrollDirection: Axis.vertical, child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.max, children: [ ...loggerList(), - Row( - mainAxisAlignment: MainAxisAlignment.spaceAround, + Wrap( children: [ Padding( padding: const EdgeInsets.all(4), @@ -124,364 +134,126 @@ class _MyHomePageState extends State { } void _submitLogs() { - if (_authLogLevel != null && _authLogMsgController.text != '') { - _authLogger.log(_authLogLevel!, _authLogMsgController.text); - } - if (_analyticsLogLevel != null && _analyticsLogMsgController.text != '') { - _analyticsLogger.log( - _analyticsLogLevel!, - _analyticsLogMsgController.text, - ); - } - if (_apiLogLevel != null && _apiLogMsgController.text != '') { - _apiLogger.log( - _apiLogLevel!, - _apiLogMsgController.text, - ); - } - if (_dataStoreLogLevel != null && _dataStoreLogMsgController.text != '') { - _dataStoreLogger.log( - _dataStoreLogLevel!, - _dataStoreLogMsgController.text, - ); - } - if (_hubLogLevel != null && _hubLogMsgController.text != '') { - _hubLogger.log(_hubLogLevel!, _hubLogMsgController.text); - } - if (_pnLogLevel != null && _pnLogMsgController.text != '') { - _pnLogger.log(_pnLogLevel!, _pnLogMsgController.text); - } - if (_storageLogLevel != null && _storageLogMsgController.text != '') { - _storageLogger.log(_storageLogLevel!, _storageLogMsgController.text); + for (final loggerInfo in _loggerMap.values) { + if (loggerInfo.logLevel != null && + loggerInfo.logMsgController.text != '') { + loggerInfo.logger.log( + loggerInfo.logLevel!, + loggerInfo.logMsgController.text, + ); + } } + if (_customLoggerNameController.text != '' && _customLogLevel != null && _customLogMsgController.text != '') { AmplifyLogger(_customLoggerNameController.text) .log(_customLogLevel!, _customLogMsgController.text); } - setState(() { - _authLogLevel = _analyticsLogLevel = _apiLogLevel = _dataStoreLogLevel = - _hubLogLevel = - _pnLogLevel = _storageLogLevel = _customLogLevel = null; - _authLogMsgController.text = _analyticsLogMsgController.text = - _apiLogMsgController.text = _dataStoreLogMsgController.text = - _hubLogMsgController.text = _pnLogMsgController.text = - _storageLogMsgController.text = _customLogMsgController.text = - _customLoggerNameController.text = ''; + setState(() { + for (final loggerInfo in _loggerMap.values) { + loggerInfo.logLevel = LogLevel.verbose; + loggerInfo.logMsgController.text = ''; + } }); } - List loggerList() { - return [ - Container( - padding: const EdgeInsets.all(10), - decoration: BoxDecoration( - border: Border.all(color: const Color(0xFF7F7F7F)), - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - const SizedBox( - width: 150, - child: Text('Analytics Category:'), - ), - DropdownButton( - hint: const Text('Select Log Level'), - value: _analyticsLogLevel, - items: LogLevel.values - .map( - (e) => DropdownMenuItem( - value: e, - child: Text(e.name), - ), - ) - .toList(), - onChanged: (newValue) { - setState(() { - _analyticsLogLevel = newValue; - }); - }, - ), - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 400), - hintText: 'Enter log message', - ), - controller: _analyticsLogMsgController, - ), - ], - ), - ), - Container( - padding: const EdgeInsets.all(10), - decoration: BoxDecoration( - border: Border.all(color: const Color(0xFF7F7F7F)), - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - const SizedBox( - width: 150, - child: Text('API Category:'), - ), - DropdownButton( - hint: const Text('Select Log Level'), - value: _apiLogLevel, - items: LogLevel.values - .map( - (e) => DropdownMenuItem( - value: e, - child: Text(e.name), - ), - ) - .toList(), - onChanged: (newValue) { - setState(() { - _apiLogLevel = newValue; - }); - }, - ), - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 400), - hintText: 'Enter log message', - ), - controller: _apiLogMsgController, - ), - ], - ), + Widget displayCategoryLogControls(Category category) { + final loggerInfo = _loggerMap[category]!; + return Container( + padding: const EdgeInsets.all(10), + decoration: BoxDecoration( + border: Border.all(color: const Color(0xFF7F7F7F)), ), - Container( - padding: const EdgeInsets.all(10), - decoration: BoxDecoration( - border: Border.all(color: const Color(0xFF7F7F7F)), - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - const SizedBox( - width: 150, - child: Text('Auth Category:'), - ), - DropdownButton( - hint: const Text('Select Log Level'), - value: _authLogLevel, - items: LogLevel.values - .map( - (e) => DropdownMenuItem( - value: e, - child: Text(e.name), - ), - ) - .toList(), - onChanged: (newValue) { - setState(() { - _authLogLevel = newValue; - }); - }, - ), - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 400), - hintText: 'Enter log message', + child: Row( + //mainAxisAlignment: MainAxisAlignment.start, + children: [ + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text('${category.name}:'), + DropdownButton( + hint: const Text('Select Log Level'), + value: loggerInfo.logLevel, + items: LogLevel.values + .map( + (e) => DropdownMenuItem( + value: e, + child: Text(e.name), + ), + ) + .toList(), + onChanged: (newValue) { + setState(() { + _loggerMap[category] = + loggerInfo.copyWith(logLevel: newValue); + }); + }, ), - controller: _authLogMsgController, - ), - ], - ), - ), - Container( - padding: const EdgeInsets.all(10), - decoration: BoxDecoration( - border: Border.all(color: const Color(0xFF7F7F7F)), - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - const SizedBox( - width: 150, - child: Text( - 'DataStore Category:', - ), - ), - DropdownButton( - hint: const Text('Select Log Level'), - value: _dataStoreLogLevel, - items: LogLevel.values - .map( - (e) => DropdownMenuItem( - value: e, - child: Text(e.name), - ), - ) - .toList(), - onChanged: (newValue) { - setState(() { - _dataStoreLogLevel = newValue; - }); - }, - ), - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 400), - hintText: 'Enter log message', - ), - controller: _dataStoreLogMsgController, + ], + ), + const SizedBox( + width: 10, + ), + TextField( + decoration: const InputDecoration( + constraints: BoxConstraints(maxWidth: 250), + hintText: 'Enter log message', ), - ], - ), + controller: loggerInfo.logMsgController, + ), + ], ), + ); + } + + List loggerList() { + return [ + for (final category in Category.values) + displayCategoryLogControls(category), Container( padding: const EdgeInsets.all(10), decoration: BoxDecoration( border: Border.all(color: const Color(0xFF7F7F7F)), ), child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ - const SizedBox( - width: 150, - child: Text('Hub Category:'), - ), - DropdownButton( - hint: const Text('Select Log Level'), - value: _hubLogLevel, - items: LogLevel.values - .map( - (e) => DropdownMenuItem( - value: e, - child: Text(e.name), - ), - ) - .toList(), - onChanged: (newValue) { - setState(() { - _hubLogLevel = newValue; - }); - }, - ), - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 400), - hintText: 'Enter log message', - ), - controller: _hubLogMsgController, + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + TextField( + decoration: const InputDecoration( + constraints: BoxConstraints(maxWidth: 150), + hintText: 'Enter logger name', + ), + controller: _customLoggerNameController, + ), + DropdownButton( + hint: const Text('Select Log Level'), + value: _customLogLevel, + items: LogLevel.values + .map( + (e) => DropdownMenuItem( + value: e, + child: Text(e.name), + ), + ) + .toList(), + onChanged: (newValue) { + setState(() { + _customLogLevel = newValue; + }); + }, + ), + ], ), - ], - ), - ), - Container( - padding: const EdgeInsets.all(10), - decoration: BoxDecoration( - border: Border.all(color: const Color(0xFF7F7F7F)), - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ const SizedBox( - width: 150, - child: Text('PushNotification Category:'), - ), - DropdownButton( - hint: const Text('Select Log Level'), - value: _pnLogLevel, - items: LogLevel.values - .map( - (e) => DropdownMenuItem( - value: e, - child: Text(e.name), - ), - ) - .toList(), - onChanged: (newValue) { - setState(() { - _pnLogLevel = newValue; - }); - }, - ), - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 400), - hintText: 'Enter log message', - ), - controller: _pnLogMsgController, - ), - ], - ), - ), - Container( - padding: const EdgeInsets.all(10), - decoration: BoxDecoration( - border: Border.all(color: const Color(0xFF7F7F7F)), - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - const SizedBox(width: 150, child: Text('Storage Category:')), - DropdownButton( - hint: const Text('Select Log Level'), - value: _storageLogLevel, - items: LogLevel.values - .map( - (e) => DropdownMenuItem( - value: e, - child: Text(e.name), - ), - ) - .toList(), - onChanged: (newValue) { - setState(() { - _storageLogLevel = newValue; - }); - }, - ), - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 400), - hintText: 'Enter log message', - ), - controller: _storageLogMsgController, - ), - ], - ), - ), - Container( - padding: const EdgeInsets.all(10), - decoration: BoxDecoration( - border: Border.all(color: const Color(0xFF7F7F7F)), - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 150), - hintText: 'Enter logger name', - ), - controller: _customLoggerNameController, - ), - DropdownButton( - hint: const Text('Select Log Level'), - value: _customLogLevel, - items: LogLevel.values - .map( - (e) => DropdownMenuItem( - value: e, - child: Text(e.name), - ), - ) - .toList(), - onChanged: (newValue) { - setState(() { - _customLogLevel = newValue; - }); - }, + width: 10, ), TextField( decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 400), + constraints: BoxConstraints(maxWidth: 225), hintText: 'Enter log message', ), controller: _customLogMsgController, diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/main.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/main.dart index 63468ed5a1..e473288167 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/main.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/main.dart @@ -112,13 +112,13 @@ class InitPage extends StatelessWidget { children: [ ElevatedButton( onPressed: () { - Navigator.pushNamed(context, '/login'); + Navigator.popAndPushNamed(context, '/login'); }, child: const Text('Go to Login Page'), ), ElevatedButton( onPressed: () { - Navigator.pushNamed(context, '/home'); + Navigator.popAndPushNamed(context, '/home'); }, child: const Text('Go to Home Page'), ), From 51efa5a3e0d9d584a9f8140e72c91097b88b5c22 Mon Sep 17 00:00:00 2001 From: kyle Date: Thu, 12 Oct 2023 11:40:27 -0700 Subject: [PATCH 3/4] Fix overflow --- .../example/lib/homepage.dart | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/homepage.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/homepage.dart index 2040cef11c..18e0cef6ae 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/homepage.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/homepage.dart @@ -196,12 +196,13 @@ class _MyHomePageState extends State { const SizedBox( width: 10, ), - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 250), - hintText: 'Enter log message', + Expanded( + child: TextField( + decoration: const InputDecoration( + hintText: 'Enter log message', + ), + controller: loggerInfo.logMsgController, ), - controller: loggerInfo.logMsgController, ), ], ), @@ -251,12 +252,13 @@ class _MyHomePageState extends State { const SizedBox( width: 10, ), - TextField( - decoration: const InputDecoration( - constraints: BoxConstraints(maxWidth: 225), - hintText: 'Enter log message', + Expanded( + child: TextField( + decoration: const InputDecoration( + hintText: 'Enter log message', + ), + controller: _customLogMsgController, ), - controller: _customLogMsgController, ), ], ), From 200d4fe18b74db8256aa3570970246cfd4da31d7 Mon Sep 17 00:00:00 2001 From: kyle Date: Thu, 12 Oct 2023 14:04:11 -0700 Subject: [PATCH 4/4] Revert "fix(logging): example app init logic" This reverts commit f56337e393118f4374551a4f2a2a524a1dd19085. --- .../example/lib/amplifyconfiguration_logging.dart | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/amplifyconfiguration_logging.dart b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/amplifyconfiguration_logging.dart index 9efb303398..24eb1c1719 100644 --- a/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/amplifyconfiguration_logging.dart +++ b/packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/amplifyconfiguration_logging.dart @@ -2,10 +2,14 @@ const loggingconfig = '''{ "plugins": { "cloudWatchLoggerPluginConfiguration": { "enable": true, - "logGroupName": "test-logging-log-group", - "region": "us-west-2", + "logGroupName": "", + "region": "", "localStoreMaxSizeInMB": 5, "flushIntervalInSeconds": 60, + "defaultRemoteConfiguration": { + "endpoint": "https://.execute-api..amazonaws.com/prod/loggingconstraints", + "refreshIntervalInSeconds": 1200 + }, "loggingConstraints": { "defaultLogLevel": "ERROR", "categoryLogLevel": {