Skip to content

Commit

Permalink
feat(logging):send amplify library logs to cloudwatch (#3893)
Browse files Browse the repository at this point in the history
feat(logging):send amplify library logs to cloudwatch
  • Loading branch information
NikaHsn authored Oct 6, 2023
1 parent cf09aaa commit 6e733e9
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:meta/meta.dart';
/// {@macro amplify_core.logger.amplify_logging_cloudwatch}
class AmplifyFlutterLogging extends AmplifyLogging {
static AmplifyCloudWatchLoggerPlugin? _plugin;
static final _logger = AmplifyLogger();
static final _logger = AWSLogger();

@override
void configure(
Expand Down Expand Up @@ -44,7 +44,9 @@ class AmplifyFlutterLogging extends AmplifyLogging {
credentialsProvider: credentialsProvider,
pluginConfig: pluginConfig,
);
_logger.registerPlugin(_plugin!);
_logger
..registerPlugin(_plugin!)
..logLevel = LogLevel.verbose;
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import 'package:meta/meta.dart';

/// {@template amplify_core.logger.amplify_logging_cloudwatch}
/// It configures the [CloudWatchLoggerPlugin] from [AmplifyConfig] to sends
/// [AmplifyLogger]'s logs to the CloudWatch.
/// [AWSLogger]'s logs to the CloudWatch.
/// {@endtemplate}
class AmplifyLogging {
static CloudWatchLoggerPlugin? _plugin;
static final _logger = AmplifyLogger();
static final _logger = AWSLogger();

void configure(
AmplifyConfig amplifyConfig,
Expand Down Expand Up @@ -43,7 +43,9 @@ class AmplifyLogging {
credentialsProvider: credentialsProvider,
pluginConfig: pluginConfig,
);
_logger.registerPlugin(_plugin!);
_logger
..registerPlugin(_plugin!)
..logLevel = LogLevel.verbose;
}

@visibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ abstract class StateMachineManager<
E extends StateMachineEvent,
S extends StateMachineState,
Manager extends StateMachineManager<E, S, Manager>>
with Dispatcher<E, S>, AWSDebuggable, AWSLoggerMixin
with Dispatcher<E, S>, AWSDebuggable, AmplifyLoggerMixin
implements DependencyManager, Closeable {
/// {@macro amplify_core.state_machinedispatcher}
StateMachineManager(
Expand Down
9 changes: 9 additions & 0 deletions packages/amplify_datastore/lib/amplify_datastore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ class AmplifyDataStore extends DataStorePluginInterface

@override
String get runtimeTypeName => 'AmplifyDataStore';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.dataStore);
}

class _NativeAmplifyAuthCognito
Expand Down Expand Up @@ -296,6 +299,9 @@ class _NativeAmplifyAuthCognito

@override
String get runtimeTypeName => '_NativeAmplifyAuthCognito';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.dataStore);
}

class _NativeAmplifyApi
Expand Down Expand Up @@ -325,4 +331,7 @@ class _NativeAmplifyApi

@override
String get runtimeTypeName => '_NativeAmplifyApi';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.dataStore);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

import 'package:amplify_analytics_pinpoint_dart/src/impl/analytics_client/event_client/queued_item_store/index_db/in_memory_queued_item_store.dart';
import 'package:amplify_analytics_pinpoint_dart/src/impl/analytics_client/event_client/queued_item_store/index_db/indexed_db_adapter.dart';

import 'package:amplify_analytics_pinpoint_dart/src/impl/analytics_client/event_client/queued_item_store/queued_item_store.dart';
import 'package:aws_common/aws_common.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:meta/meta.dart';

/// {@macro amplify_analytics_pinpoint_dart.dart_queued_item_store}
class DartQueuedItemStore
with AWSDebuggable, AWSLoggerMixin
with AWSDebuggable, AmplifyLoggerMixin
implements QueuedItemStore, Closeable {
/// {@macro amplify_analytics_pinpoint_dart.index_db_queued_item_store}
// ignore: avoid_unused_constructor_parameters
Expand All @@ -30,6 +29,9 @@ class DartQueuedItemStore
@override
String get runtimeTypeName => 'DartQueuedItemStore';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.analytics);

@override
Future<void> addItem(String string) async {
final db = await _database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class SubscriptionBloc<T>
@override
String get runtimeTypeName => 'WsSubscriptionBloc';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.api);

Stream<WsSubscriptionState<T>> _startAck(
SubscriptionStartAckEvent event,
) async* {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class WebSocketBloc with AWSDebuggable, AmplifyLoggerMixin {
@override
String get runtimeTypeName => 'WebSocketBloc';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.api);

/// Default timeout response for polling
static const Duration _pollResponseTimeout = Duration(seconds: 5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,7 @@ class AmplifyWebSocketService

@override
String get runtimeTypeName => 'WebSocketService';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.api);
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ class _NativeAmplifyAuthCognito

@override
String get runtimeTypeName => '_NativeAmplifyAuthCognito';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.auth);
}

final class _NativeASFDeviceInfoCollector extends ASFDeviceInfoCollector {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:async/async.dart';
/// Provides user context data as required for Cognito's advanced security features
/// (ASF) functionality.
/// {@endtemplate}
final class ASFContextDataProvider with AWSDebuggable, AWSLoggerMixin {
final class ASFContextDataProvider with AWSDebuggable, AmplifyLoggerMixin {
/// {@macro amplify_auth_cognito_dart.asf.asf_context_data_provider}
ASFContextDataProvider(this._dependencyManager);

Expand Down Expand Up @@ -101,4 +101,7 @@ final class ASFContextDataProvider with AWSDebuggable, AWSLoggerMixin {

@override
String get runtimeTypeName => 'ASFContextDataProvider';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.auth);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ abstract base class ASFDeviceInfoCollector {
/// {@endtemplate}
@internal
abstract base class NativeASFDeviceInfoCollector extends ASFDeviceInfoCollector
with AWSDebuggable, AWSLoggerMixin {
with AWSDebuggable, AmplifyLoggerMixin {
/// {@macro amplify_auth_cognito_dart.asf.asf_device_info}
const NativeASFDeviceInfoCollector() : super.base();

Expand Down Expand Up @@ -158,4 +158,7 @@ abstract base class NativeASFDeviceInfoCollector extends ASFDeviceInfoCollector

@override
String get runtimeTypeName => 'NativeASFDeviceInfoCollector';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.auth);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class StateMachineBloc
@override
String get runtimeTypeName => 'StateMachineBloc';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.auth);

/// State controller.
final StreamController<AuthState> _authStateController =
StreamController<AuthState>.broadcast();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Future<void> main() async {

await Amplify.addPlugin(authPlugin);
await Amplify.configure(amplifyConfig);
AmplifyLogger().logLevel = LogLevel.verbose;

runApp(
const MyApp(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

// ignore_for_file: implementation_imports

import 'package:amplify_core/amplify_core.dart';
import 'package:amplify_logging_cloudwatch/src/queued_item_store/index_db/indexed_db_adapter.dart';
import 'package:aws_common/aws_common.dart';
import 'package:aws_logging_cloudwatch/src/queued_item_store/in_memory_queued_item_store.dart';
import 'package:aws_logging_cloudwatch/src/queued_item_store/queued_item_store.dart';
import 'package:meta/meta.dart';

/// {@macro amplify_logging_cloudwatch.dart_queued_item_store}
class DartQueuedItemStore
with AWSDebuggable, AWSLoggerMixin
with AWSDebuggable, AmplifyLoggerMixin
implements QueuedItemStore, Closeable {
/// {@macro amplify_logging_cloudwatch.index_db_queued_item_store}
// ignore: avoid_unused_constructor_parameters
Expand All @@ -31,6 +31,9 @@ class DartQueuedItemStore
@override
String get runtimeTypeName => 'DartQueuedItemStore';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.logging);

@override
Future<void> addItem(
String string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ typedef _LogBatch = (List<QueuedItem> logQueues, List<InputLogEvent> logEvents);
/// An [AWSLoggerPlugin] for sending logs to AWS CloudWatch Logs.
/// {@endtemplate}
class CloudWatchLoggerPlugin extends AWSLoggerPlugin
with AWSDebuggable, AWSLoggerMixin {
with AWSDebuggable, AmplifyLoggerMixin {
/// {@macro aws_logging_cloudwatch.cloudwatch_logger_plugin}
CloudWatchLoggerPlugin({
required AWSCredentialsProvider credentialsProvider,
Expand Down Expand Up @@ -369,6 +369,9 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin
Future<void> _clearLogs() async {
await _logStore.clear();
}

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.logging);
}

extension on QueuedItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class RemoteLoggingConstraintProvider {
/// [LoggingConstraints] from a remote location and cache it.
/// {@endtemplate}
base class BaseRemoteLoggingConstraintProvider
with AWSDebuggable, AWSLoggerMixin
with AWSDebuggable, AmplifyLoggerMixin
implements RemoteLoggingConstraintProvider, Closeable {
/// {@macro aws_logging_cloudwatch.base_remote_constraints_provider}
BaseRemoteLoggingConstraintProvider({
Expand Down Expand Up @@ -66,6 +66,9 @@ base class BaseRemoteLoggingConstraintProvider
@override
String get runtimeTypeName => 'BaseRemoteConstraintsProvider';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.logging);

/// Initializes the [BaseRemoteLoggingConstraintProvider] by fetching
/// the constraint from the endpoint initially and then
/// starting the refresh timer afterwards.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool get _zIsTest => Zone.current[zIsTest] as bool? ?? false;
/// The Dart Storage S3 plugin for the Amplify Storage Category.
/// {@endtemplate}
class AmplifyStorageS3Dart extends StoragePluginInterface
with AWSDebuggable, AWSLoggerMixin {
with AWSDebuggable, AmplifyLoggerMixin {
/// {@macro amplify_storage_s3_dart.amplify_storage_s3_plugin_dart}
AmplifyStorageS3Dart({
String? delimiter,
Expand Down Expand Up @@ -478,6 +478,9 @@ class AmplifyStorageS3Dart extends StoragePluginInterface

@override
String get runtimeTypeName => 'AmplifyStorageS3Dart';

@override
AmplifyLogger get logger => AmplifyLogger.category(Category.storage);
}

class _AmplifyStorageS3DartPluginKey
Expand Down

0 comments on commit 6e733e9

Please sign in to comment.