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 11 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
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,38 @@ class CloudWatchLoggerPlugin extends AWSLoggerPlugin
if (!_enabled) {
return false;
}

final loggingConstraint = _getLoggingConstraint();
if (loggingConstraint.userLogLevel.containsKey(_userId)) {
final userLogLevel = loggingConstraint.userLogLevel[_userId]!;
final userCategoryLogLevel =
_getCategoryLogLevel(userLogLevel.categoryLogLevel, logEntry);
if (userCategoryLogLevel != null) {
return logEntry.level >= userCategoryLogLevel;
}
return logEntry.level >= userLogLevel.defaultLogLevel;
}

final categoryLogLevel =
_getCategoryLogLevel(loggingConstraint.categoryLogLevel, logEntry);
if (categoryLogLevel != null) {
return logEntry.level >= categoryLogLevel;
}
return logEntry.level >= loggingConstraint.defaultLogLevel;
}
khatruong2009 marked this conversation as resolved.
Show resolved Hide resolved

LogLevel? _getCategoryLogLevel(
khatruong2009 marked this conversation as resolved.
Show resolved Hide resolved
Map<String, LogLevel> categoryLogLevel,
LogEntry logEntry,
) {
for (final entry in categoryLogLevel.entries) {
if (logEntry.loggerName.toLowerCase().contains(entry.key.toLowerCase())) {
return entry.value;
}
}
return null;
}

@override
Future<void> handleLogEntry(LogEntry logEntry) async {
if (!(_isLoggable(logEntry))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ import 'package:test/test.dart';

import 'mocks.dart';

class MockRemoteLoggingConstraintProvider
extends RemoteLoggingConstraintProvider {
@override
LoggingConstraints get loggingConstraint {
return const LoggingConstraints(
userLogLevel: {
'user1': UserLogLevel(
defaultLogLevel: LogLevel.warn,
categoryLogLevel: {},
),
},
// ... other properties ...
);
}
}

void main() {
late MockCloudWatchLogsClient mockCloudWatchLogsClient;
late MockQueuedItemStore mockQueuedItemStore;
Expand Down Expand Up @@ -798,7 +814,7 @@ void main() {
plugin.handleLogEntry(errorLog),
completes,
);
hubEventController.add(AuthHubEvent.signedIn(MockAuhtUser()));
hubEventController.add(AuthHubEvent.signedIn(MockAuthUser()));
await Future<void>.delayed(Duration.zero);

verify(
Expand All @@ -813,4 +829,160 @@ void main() {
);
});
});

group('Test _isLoggable method', () {
final hubEventController = StreamController<AuthHubEvent>.broadcast();
Amplify.Hub.addChannel(HubChannel.Auth, hubEventController.stream);
const loggingConstraints2 = LoggingConstraints(
categoryLogLevel: {
'Amplify': LogLevel.debug,
'Auth': LogLevel.warn,
},
defaultLogLevel: LogLevel.warn,
);
const pluginConfig = CloudWatchPluginConfig(
logGroupName: 'logGroupName',
region: 'region',
loggingConstraints: loggingConstraints2,
enable: true,
);
setUp(() {
mockCloudWatchLogsClient = MockCloudWatchLogsClient();
mockQueuedItemStore = MockQueuedItemStore();
mockCloudWatchLogStreamProvider = MockCloudWatchLogStreamProvider();
plugin = CloudWatchLoggerPlugin.testPlugin(
client: mockCloudWatchLogsClient,
pluginConfig: pluginConfig,
logStore: mockQueuedItemStore,
logStreamProvider: mockCloudWatchLogStreamProvider,
);

when(() => mockQueuedItemStore.isFull(any())).thenReturn(false);
});
tearDownAll(hubEventController.close);

test('logs when log level meets default constraint', () async {
final entry = LogEntry(
level: LogLevel.error,
loggerName: '',
message: '',
);

await plugin.handleLogEntry(entry);
verify(() => mockQueuedItemStore.addItem(any(), any())).called(1);
});

test('does not log when log level is below default constraint', () async {
final entry = LogEntry(
level: LogLevel.debug,
loggerName: '',
message: '',
);

await plugin.handleLogEntry(entry);
verifyNever(() => mockQueuedItemStore.addItem(any(), any()));
});

test('logs when log level meets category override', () async {
final entry = LogEntry(
level: LogLevel.debug,
loggerName: 'Amplify',
message: '',
);

await plugin.handleLogEntry(entry);
verify(() => mockQueuedItemStore.addItem(any(), any())).called(1);
});

test('does not log when category override blocks log', () async {
final entry = LogEntry(
level: LogLevel.info,
loggerName: 'Auth',
message: '',
);

await plugin.handleLogEntry(entry);
verifyNever(() => mockQueuedItemStore.addItem(any(), any()));
});

test('applies userLogLevel filter', () {
const loggingConstraints3 = LoggingConstraints(
userLogLevel: {
'mockUserId': UserLogLevel(
defaultLogLevel: LogLevel.warn,
categoryLogLevel: {},
),
},
);
const config2 = CloudWatchPluginConfig(
logGroupName: 'logGroupName',
region: 'region',
loggingConstraints: loggingConstraints3,
enable: true,
);

hubEventController.add(
AuthHubEvent.signedIn(
MockAuthUser(),
),
);

plugin = CloudWatchLoggerPlugin.testPlugin(
client: mockCloudWatchLogsClient,
pluginConfig: config2,
logStore: mockQueuedItemStore,
logStreamProvider: mockCloudWatchLogStreamProvider,
);

final entry = LogEntry(level: LogLevel.info, loggerName: '', message: '');
plugin.handleLogEntry(entry);
verifyNever(() => mockQueuedItemStore.addItem(any(), any()));
});

test('logs at default level', () async {
final entry = LogEntry(
level: LogLevel.warn,
message: '',
loggerName: '',
);

await plugin.handleLogEntry(entry);
verify(() => mockQueuedItemStore.addItem(any(), any())).called(1);
});

test('does not log below default level', () async {
final entry =
LogEntry(level: LogLevel.debug, message: '', loggerName: '');

await plugin.handleLogEntry(entry);
verifyNever(() => mockQueuedItemStore.addItem(any(), any()));
});

test('logs at category override level', () async {
const pluginConfig = CloudWatchPluginConfig(
loggingConstraints: LoggingConstraints(
categoryLogLevel: {'Amplify': LogLevel.debug},
),
region: 'region',
logGroupName: 'logGroupName',
);

plugin = CloudWatchLoggerPlugin.testPlugin(
client: mockCloudWatchLogsClient,
pluginConfig: pluginConfig,
logStore: mockQueuedItemStore,
logStreamProvider: mockCloudWatchLogStreamProvider,
);

final entry = LogEntry(
level: LogLevel.debug,
loggerName: 'Amplify',
message: '',
);

await plugin.handleLogEntry(entry);

verify(() => mockQueuedItemStore.addItem(any(), any())).called(1);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ class MockCloudWatchLogStreamProvider extends Mock
class MockRemoteLoggingConstraintProvider extends Mock
implements RemoteLoggingConstraintProvider {}

class MockAuhtUser extends Mock implements AuthUser {}
class MockAuthUser extends Mock implements AuthUser {
@override
String get userId => 'mockUserId';
}