From 0cc7564841e801649e76d2ed3a2ca7673ce105d6 Mon Sep 17 00:00:00 2001 From: Kha Truong <64438356+khatruong2009@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:37:12 -0700 Subject: [PATCH] feat: implement _isLoggable and corrected categoryLogLevel in UserLogLevel class --- .../logging/cloudwatch_logging_config.dart | 2 +- .../lib/src/cloudwatch_logger_plugin.dart | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/amplify_core/lib/src/config/logging/cloudwatch_logging_config.dart b/packages/amplify_core/lib/src/config/logging/cloudwatch_logging_config.dart index 3012fc94a2..581da428de 100644 --- a/packages/amplify_core/lib/src/config/logging/cloudwatch_logging_config.dart +++ b/packages/amplify_core/lib/src/config/logging/cloudwatch_logging_config.dart @@ -161,7 +161,7 @@ class UserLogLevel _$UserLogLevelFromJson(json); final LogLevel defaultLogLevel; - final Map categoryLogLevel; + final Map categoryLogLevel; @override List get props => [defaultLogLevel, categoryLogLevel]; diff --git a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/cloudwatch_logger_plugin.dart b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/cloudwatch_logger_plugin.dart index 074f69e906..949a74ff47 100644 --- a/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/cloudwatch_logger_plugin.dart +++ b/packages/logging_cloudwatch/aws_logging_cloudwatch/lib/src/cloudwatch_logger_plugin.dart @@ -304,7 +304,38 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin if (!_enabled) { return false; } + final loggingConstraint = _getLoggingConstraint(); + + String? userId; + + Amplify.Hub.listen(HubChannel.Auth, (AuthHubEvent event) async { + if (event.type == AuthHubEventType.signedOut || + event.type == AuthHubEventType.userDeleted || + event.type == AuthHubEventType.sessionExpired) { + userId = null; + } + if (event.type == AuthHubEventType.signedIn) { + userId = event.payload?.userId; + } + }); + + if (loggingConstraint.userLogLevel.containsKey(userId)) { + final userLevel = loggingConstraint.userLogLevel[userId]!; + + if (userLevel.categoryLogLevel.containsKey(logEntry.loggerName)) { + return logEntry.level >= + userLevel.categoryLogLevel[logEntry.loggerName]!; + } + + return logEntry.level >= userLevel.defaultLogLevel; + } + + if (loggingConstraint.categoryLogLevel.containsKey(logEntry.loggerName)) { + return logEntry.level >= + loggingConstraint.categoryLogLevel[logEntry.loggerName]!; + } + return logEntry.level >= loggingConstraint.defaultLogLevel; }