Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement _isLoggable method #3894

Merged
merged 16 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1917,15 +1917,27 @@ updates:
update-types:
- "version-update:semver-patch"
# Ignore all repo packages
- dependency-name: "amplify_auth_cognito"
- dependency-name: "amplify_analytics_pinpoint"
- dependency-name: "amplify_analytics_pinpoint_dart"
- dependency-name: "amplify_core"
- dependency-name: "aws_common"
- dependency-name: "amplify_lints"
# Group dependencies which have a constraint set in the global "pubspec.yaml"
groups:
build_runner:
patterns:
- "build_runner"
build_web_compilers:
patterns:
- "build_web_compilers"
- dependency-name: "aws_logging_cloudwatch"
- dependency-name: "aws_signature_v4"
- dependency-name: "smithy"
- dependency-name: "smithy_aws"
- dependency-name: "amplify_db_common_dart"
- dependency-name: "amplify_secure_storage_dart"
- dependency-name: "worker_bee"
- dependency-name: "worker_bee_builder"
- dependency-name: "amplify_db_common"
- dependency-name: "amplify_secure_storage"
- dependency-name: "amplify_auth_cognito_dart"
- dependency-name: "smithy_codegen"
- dependency-name: "amplify_flutter"
- dependency-name: "amplify_logging_cloudwatch"
- dependency-name: "amplify_authenticator"
- package-ecosystem: "pub"
directory: "packages/logging_cloudwatch/amplify_logging_cloudwatch/example"
schedule:
Expand All @@ -1936,15 +1948,27 @@ updates:
update-types:
- "version-update:semver-patch"
# Ignore all repo packages
- dependency-name: "amplify_auth_cognito"
- dependency-name: "amplify_analytics_pinpoint"
- dependency-name: "amplify_analytics_pinpoint_dart"
- dependency-name: "amplify_core"
- dependency-name: "aws_common"
- dependency-name: "amplify_lints"
# Group dependencies which have a constraint set in the global "pubspec.yaml"
groups:
build_runner:
patterns:
- "build_runner"
build_web_compilers:
patterns:
- "build_web_compilers"
- dependency-name: "aws_logging_cloudwatch"
- dependency-name: "aws_signature_v4"
- dependency-name: "smithy"
- dependency-name: "smithy_aws"
- dependency-name: "amplify_db_common_dart"
- dependency-name: "amplify_secure_storage_dart"
- dependency-name: "worker_bee"
- dependency-name: "worker_bee_builder"
- dependency-name: "amplify_db_common"
- dependency-name: "amplify_secure_storage"
- dependency-name: "amplify_auth_cognito_dart"
- dependency-name: "smithy_codegen"
- dependency-name: "amplify_flutter"
- dependency-name: "amplify_logging_cloudwatch"
- dependency-name: "amplify_authenticator"
- package-ecosystem: "pub"
directory: "packages/logging_cloudwatch/aws_logging_cloudwatch"
schedule:
Expand Down
86 changes: 83 additions & 3 deletions .github/workflows/amplify_logging_cloudwatch_example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,103 @@ on:
- stable
paths:
- '.github/workflows/amplify_logging_cloudwatch_example.yaml'
- '.github/workflows/dart_vm.yaml'
- '.github/workflows/flutter_vm.yaml'
- 'packages/amplify/amplify_flutter/lib/**/*.dart'
- 'packages/amplify/amplify_flutter/pubspec.yaml'
- 'packages/amplify_core/lib/**/*.dart'
- 'packages/amplify_core/pubspec.yaml'
- 'packages/amplify_lints/lib/**/*.yaml'
- 'packages/amplify_lints/pubspec.yaml'
- 'packages/analytics/amplify_analytics_pinpoint/lib/**/*.dart'
- 'packages/analytics/amplify_analytics_pinpoint/pubspec.yaml'
- 'packages/analytics/amplify_analytics_pinpoint_dart/lib/**/*.dart'
- 'packages/analytics/amplify_analytics_pinpoint_dart/pubspec.yaml'
- 'packages/auth/amplify_auth_cognito/lib/**/*.dart'
- 'packages/auth/amplify_auth_cognito/pubspec.yaml'
- 'packages/auth/amplify_auth_cognito_dart/lib/**/*.dart'
- 'packages/auth/amplify_auth_cognito_dart/pubspec.yaml'
- 'packages/authenticator/amplify_authenticator/lib/**/*.dart'
- 'packages/authenticator/amplify_authenticator/pubspec.yaml'
- 'packages/aws_common/lib/**/*.dart'
- 'packages/aws_common/pubspec.yaml'
- 'packages/aws_signature_v4/lib/**/*.dart'
- 'packages/aws_signature_v4/pubspec.yaml'
- 'packages/common/amplify_db_common/lib/**/*.dart'
- 'packages/common/amplify_db_common/pubspec.yaml'
- 'packages/common/amplify_db_common_dart/lib/**/*.dart'
- 'packages/common/amplify_db_common_dart/pubspec.yaml'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/example/**/*.dart'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/example/**/*.yaml'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/**/*'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/example/test/**/*'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/**/*.dart'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/pubspec.yaml'
- 'packages/logging_cloudwatch/aws_logging_cloudwatch/lib/**/*.dart'
- 'packages/logging_cloudwatch/aws_logging_cloudwatch/pubspec.yaml'
- 'packages/secure_storage/amplify_secure_storage/lib/**/*.dart'
- 'packages/secure_storage/amplify_secure_storage/pubspec.yaml'
- 'packages/secure_storage/amplify_secure_storage_dart/lib/**/*.dart'
- 'packages/secure_storage/amplify_secure_storage_dart/pubspec.yaml'
- 'packages/smithy/smithy/lib/**/*.dart'
- 'packages/smithy/smithy/pubspec.yaml'
- 'packages/smithy/smithy_aws/lib/**/*.dart'
- 'packages/smithy/smithy_aws/pubspec.yaml'
- 'packages/smithy/smithy_codegen/lib/**/*.dart'
- 'packages/smithy/smithy_codegen/pubspec.yaml'
- 'packages/worker_bee/worker_bee/lib/**/*.dart'
- 'packages/worker_bee/worker_bee/pubspec.yaml'
- 'packages/worker_bee/worker_bee_builder/lib/**/*.dart'
- 'packages/worker_bee/worker_bee_builder/pubspec.yaml'
pull_request:
paths:
- '.github/workflows/amplify_logging_cloudwatch_example.yaml'
- '.github/workflows/dart_vm.yaml'
- '.github/workflows/flutter_vm.yaml'
- 'packages/amplify/amplify_flutter/lib/**/*.dart'
- 'packages/amplify/amplify_flutter/pubspec.yaml'
- 'packages/amplify_core/lib/**/*.dart'
- 'packages/amplify_core/pubspec.yaml'
- 'packages/amplify_lints/lib/**/*.yaml'
- 'packages/amplify_lints/pubspec.yaml'
- 'packages/analytics/amplify_analytics_pinpoint/lib/**/*.dart'
- 'packages/analytics/amplify_analytics_pinpoint/pubspec.yaml'
- 'packages/analytics/amplify_analytics_pinpoint_dart/lib/**/*.dart'
- 'packages/analytics/amplify_analytics_pinpoint_dart/pubspec.yaml'
- 'packages/auth/amplify_auth_cognito/lib/**/*.dart'
- 'packages/auth/amplify_auth_cognito/pubspec.yaml'
- 'packages/auth/amplify_auth_cognito_dart/lib/**/*.dart'
- 'packages/auth/amplify_auth_cognito_dart/pubspec.yaml'
- 'packages/authenticator/amplify_authenticator/lib/**/*.dart'
- 'packages/authenticator/amplify_authenticator/pubspec.yaml'
- 'packages/aws_common/lib/**/*.dart'
- 'packages/aws_common/pubspec.yaml'
- 'packages/aws_signature_v4/lib/**/*.dart'
- 'packages/aws_signature_v4/pubspec.yaml'
- 'packages/common/amplify_db_common/lib/**/*.dart'
- 'packages/common/amplify_db_common/pubspec.yaml'
- 'packages/common/amplify_db_common_dart/lib/**/*.dart'
- 'packages/common/amplify_db_common_dart/pubspec.yaml'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/example/**/*.dart'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/example/**/*.yaml'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/example/lib/**/*'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/example/test/**/*'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/lib/**/*.dart'
- 'packages/logging_cloudwatch/amplify_logging_cloudwatch/pubspec.yaml'
- 'packages/logging_cloudwatch/aws_logging_cloudwatch/lib/**/*.dart'
- 'packages/logging_cloudwatch/aws_logging_cloudwatch/pubspec.yaml'
- 'packages/secure_storage/amplify_secure_storage/lib/**/*.dart'
- 'packages/secure_storage/amplify_secure_storage/pubspec.yaml'
- 'packages/secure_storage/amplify_secure_storage_dart/lib/**/*.dart'
- 'packages/secure_storage/amplify_secure_storage_dart/pubspec.yaml'
- 'packages/smithy/smithy/lib/**/*.dart'
- 'packages/smithy/smithy/pubspec.yaml'
- 'packages/smithy/smithy_aws/lib/**/*.dart'
- 'packages/smithy/smithy_aws/pubspec.yaml'
- 'packages/smithy/smithy_codegen/lib/**/*.dart'
- 'packages/smithy/smithy_codegen/pubspec.yaml'
- 'packages/worker_bee/worker_bee/lib/**/*.dart'
- 'packages/worker_bee/worker_bee/pubspec.yaml'
- 'packages/worker_bee/worker_bee_builder/lib/**/*.dart'
- 'packages/worker_bee/worker_bee_builder/pubspec.yaml'
schedule:
- cron: "0 6,12 * * *" # Every day at 06:00 and 12:00
defaults:
Expand All @@ -40,7 +120,7 @@ concurrency:

jobs:
test:
uses: ./.github/workflows/dart_vm.yaml
uses: ./.github/workflows/flutter_vm.yaml
with:
package-name: amplify_logging_cloudwatch_example
working-directory: packages/logging_cloudwatch/amplify_logging_cloudwatch/example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class UserLogLevel
_$UserLogLevelFromJson(json);

final LogLevel defaultLogLevel;
final Map<String, String> categoryLogLevel;
final Map<String, LogLevel> categoryLogLevel;

@override
List<Object?> get props => [defaultLogLevel, categoryLogLevel];
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin
if (event.type == AuthHubEventType.signedOut ||
event.type == AuthHubEventType.userDeleted ||
event.type == AuthHubEventType.sessionExpired) {
_userId = null;
await _clearLogs();
}
if (event.type == AuthHubEventType.signedIn) {
_userId = event.payload?.userId;
}
});
}

Expand All @@ -122,11 +126,15 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin
if (event.type == AuthHubEventType.signedOut ||
event.type == AuthHubEventType.userDeleted ||
event.type == AuthHubEventType.sessionExpired) {
_userId = null;
await _clearLogs();
}
khatruong2009 marked this conversation as resolved.
Show resolved Hide resolved
if (event.type == AuthHubEventType.signedIn) {
_userId = event.payload?.userId;
}
});
}

String? _userId;
final CloudWatchPluginConfig _pluginConfig;
final CloudWatchLogsClient _client;
final CloudWatchLogStreamProvider _logStreamProvider;
Expand Down Expand Up @@ -304,10 +312,58 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin
if (!_enabled) {
return false;
}

final loggingConstraint = _getLoggingConstraint();

if (loggingConstraint.userLogLevel.containsKey(_userId)) {
final userLevel = loggingConstraint.userLogLevel[_userId]!;
if (_checkLogLevel(userLevel.categoryLogLevel, logEntry)) {
return true;
}
return logEntry.level >= userLevel.defaultLogLevel;
}
final isLoggableByCategory =
_checkLogLevel(loggingConstraint.categoryLogLevel, logEntry);
if (isLoggableByCategory) {
return true;
} else if (loggingConstraint.categoryLogLevel.isNotEmpty) {
return false;
}
return logEntry.level >= loggingConstraint.defaultLogLevel;
}

bool _checkLogLevel(
Map<String, LogLevel> categoryLogLevel,
LogEntry logEntry,
) {
for (final entry in categoryLogLevel.entries) {
if (logEntry.loggerName.toLowerCase().contains(entry.key.toLowerCase())) {
return logEntry.level >= entry.value;
}
}
return false;
}

khatruong2009 marked this conversation as resolved.
Show resolved Hide resolved
/// Test the _isLoggable method.
@protected
@visibleForTesting
bool testIsLoggable(LogEntry logEntry) => _isLoggable(logEntry);

/// Get user id for testing.
@protected
@visibleForTesting
String? get testUserId => _userId;

/// Set user id for testing.
@protected
@visibleForTesting
set testUserId(String? userId) => _userId = userId;

/// Get logging constraint for testing.
@protected
@visibleForTesting
LoggingConstraints get testLoggingConstraint => _getLoggingConstraint();

khatruong2009 marked this conversation as resolved.
Show resolved Hide resolved
@override
Future<void> handleLogEntry(LogEntry logEntry) async {
if (!(_isLoggable(logEntry))) {
Expand Down
Loading