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

Feature: Adding LogLevel to Lumberdash #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion colorize_lumberdash/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: colorize_lumberdash
description: >-
Lumberdash plugin that colors your logs on the stdout depending on
their severity level
version: 3.0.0
version: 3.1.0
authors:
- BMW Group <flutter-dev@bmwgroup.com>
repository: https://github.com/bmw-tech/lumberdash
Expand Down
2 changes: 1 addition & 1 deletion file_lumberdash/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: file_lumberdash
description: Lumberdash package that outputs your logs to the file system.
version: 1.0.0
version: 1.1.0
repository: https://github.com/bmw-tech/lumberdash
issue_tracker: https://github.com/bmw-tech/lumberdash/issues
homepage: https://bmw-tech.github.io/lumberdash/
Expand Down
2 changes: 1 addition & 1 deletion firebase_lumberdash/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: firebase_lumberdash
description: >-
Lumberdash plugin that sends your logs to Firebase Analytics
version: 3.0.0
version: 3.1.0
repository: https://github.com/bmw-tech/lumberdash
issue_tracker: https://github.com/bmw-tech/lumberdash/issues
homepage: https://bmw-tech.github.io/lumberdash/
Expand Down
86 changes: 55 additions & 31 deletions lumberdash/lib/src/lumberdash.dart
Original file line number Diff line number Diff line change
@@ -1,79 +1,103 @@
import 'client/client.dart';

/// Initialize the `lumberdashClients` internally
List<LumberdashClient> _lumberdashClients = [];
/// Possible LogLevels that can be set for a [LumberdashClient].
enum LogLevel { message, warning, fatal, error }

/// For each LogLevel there is a list of [LumberdashClient], that are used
/// when the corresponding log-method is called.
var _lumberdashClients = {
LogLevel.message: [],
LogLevel.warning: [],
LogLevel.fatal: [],
LogLevel.error: []
};

/// As it name says, it puts Lumberdash to work by setting up its
/// [LumberdashClient]. The full power of Lumberdash can
/// be achieved when you use a custom [LumberdashClient], or multiple
/// clients, that can fit your logging requirements.
/// [LumberdashClient]. This method adds the clients to all LogLevels.
/// The full power of Lumberdash can be achieved when you use a custom
/// [LumberdashClient], or multiple clients, that can fit your logging
/// requirements.
putLumberdashToWork({required List<LumberdashClient> withClients}) {
_lumberdashClients = withClients;
withClients.forEach((client) {
_lumberdashClients.forEach((_, logLevelClients) {
logLevelClients.add(client);
},);
},);
}

/// Puts Lumberdash to work by adding a
/// [LumberdashClient] to a List of LogLevels. Only these LogLevels
/// are used for logging to the LumberdashClient.
putLumberdashToWorkByLogLevel(
{required Map<LumberdashClient, List<LogLevel>> withClients}) {
withClients.forEach((keyLC, logLevelClients) {
logLevelClients.forEach((element) {
_lumberdashClients[element]?.add(keyLC);
},);
},);
}

/// It calls the `logMessage` method of the each [LumberdashClient]
/// passed to `putLumberdashToWork`, and returns the given `message`.
/// It calls the `logMessage` method of each [LumberdashClient] of
/// [LogLevel.message], and returns the given `message`.
/// If you want to avoid logging to a particular client that was
/// previously registered in `putLumberdashToWork#withClients`, use
/// `exceptFor` to filter out that client.
/// previously registered, use `exceptFor` to filter out that client.
String logMessage(
String message, {
Map<String, String>? extras,
List<Type> exceptFor = const [],
}) {
_filterOutClientsAndLog(exceptFor, (c) => c.logMessage(message, extras));
_filterOutClientsAndLog(
exceptFor, (c) => c.logMessage(message, extras), LogLevel.message,);
return message;
}

/// It calls the `logWarning` method of the each [LumberdashClient]
/// passed to `putLumberdashToWork`, and returns the given `message`.
/// It calls the `logWarning` method of each [LumberdashClient] of
/// [LogLevel.warning] and returns the given `message`.
/// If you want to avoid logging to a particular client that was
/// previously registered in `putLumberdashToWork#withClients`, use
/// `exceptFor` to filter out that client.
/// previously registered, use `exceptFor` to filter out that client.
String logWarning(
String message, {
Map<String, String>? extras,
List<Type> exceptFor = const [],
}) {
_filterOutClientsAndLog(exceptFor, (c) => c.logWarning(message, extras));
_filterOutClientsAndLog(
exceptFor, (c) => c.logWarning(message, extras), LogLevel.warning,);
return message;
}

/// It calls the `logFatal` method of the each [LumberdashClient]
/// passed to `putLumberdashToWork`, and returns the given `message`.
/// It calls the `logFatal` method of each [LumberdashClient] of
/// [LogLevel.fatal] and returns the given `message`.
/// If you want to avoid logging to a particular client that was
/// previously registered in `putLumberdashToWork#withClients`, use
/// `exceptFor` to filter out that client.
/// previously registered, use `exceptFor` to filter out that client.
String logFatal(
String message, {
Map<String, String>? extras,
List<Type> exceptFor = const [],
}) {
_filterOutClientsAndLog(exceptFor, (c) => c.logFatal(message, extras));
_filterOutClientsAndLog(
exceptFor, (c) => c.logFatal(message, extras), LogLevel.fatal,);
return message;
}

/// It calls the `logError` method of the each [LumberdashClient]
/// passed to `putLumberdashToWork`, and returns the given `exception`.
/// It calls the `logError` method of each [LumberdashClient] of
/// [LogLevel.error] and returns the given `exception`.
/// If you want to avoid logging to a particular client that was
/// previously registered in `putLumberdashToWork#withClients`, use
/// `exceptFor` to filter out that client.
/// previously registered, use `exceptFor` to filter out that client.
dynamic logError(
dynamic exception, {
dynamic stacktrace,
List<Type> exceptFor = const [],
}) {
_filterOutClientsAndLog(exceptFor, (c) => c.logError(exception, stacktrace));
_filterOutClientsAndLog(
exceptFor, (c) => c.logError(exception, stacktrace), LogLevel.error,);
return exception;
}

typedef Logger = Function(LumberdashClient client);

void _filterOutClientsAndLog(
List<Type> clientsToFilterOut,
Logger logger,
) {
_lumberdashClients
.where((client) => !clientsToFilterOut.contains(client.runtimeType))
List<Type> clientsToFilterOut, Logger logger, LogLevel logLevel,) {
_lumberdashClients[logLevel]
?.where((client) => !clientsToFilterOut.contains(client.runtimeType))
.forEach((c) => logger(c));
}
2 changes: 1 addition & 1 deletion lumberdash/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: >-
Do you need logs? Lumberdash is the answer! Simple and extensible
logging API, it allows you to create and consume different that will
cover all your logging needs.
version: 3.0.0
version: 3.1.0
authors:
- BMW Group <flutter-dev@bmwgroup.com>
repository: https://github.com/bmw-tech/lumberdash
Expand Down
90 changes: 90 additions & 0 deletions lumberdash/test/lumberdash_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,94 @@ void main() {
verifyZeroInteractions(mockClient2);
});
});

group('LogLevel handling', () {
test('should use the the given client with LogLevel error', () {
final mockClient = MockClient();

putLumberdashToWorkByLogLevel(withClients: {
mockClient: [LogLevel.fatal]
});
logMessage('Message');
logWarning('Warning');
logFatal('Fatal');
logError('Error');

verifyNever(mockClient.logMessage('Error'));
verifyNever(mockClient.logWarning('Error'));
verify(mockClient.logFatal('Fatal')).called(1);
verifyNever(mockClient.logError('Error'));
});

test(
'should use the the given client with LogLevel message, warning, error',
() {
final mockClient = MockClient();

putLumberdashToWorkByLogLevel(withClients: {
mockClient: [LogLevel.message, LogLevel.warning, LogLevel.error]
});
logMessage('Message');
logWarning('Warning');
logFatal('Fatal');
logError('Error');

verify(mockClient.logMessage('Message')).called(1);
verify(mockClient.logWarning('Warning')).called(1);
verifyNever(mockClient.logFatal('Fatal'));
verify(mockClient.logError('Error')).called(1);
});

test('use LogLevel set for each client', () {
final mockClient1 = MockClient();
final mockClient2 = MockClient();

putLumberdashToWorkByLogLevel(withClients: {
mockClient1: [LogLevel.warning, LogLevel.error],
mockClient2: [LogLevel.message, LogLevel.fatal]
});
logMessage('Message');
logWarning('Warning');
logFatal('Fatal');
logError('Error');

verifyNever(mockClient1.logMessage('Message'));
verify(mockClient1.logWarning('Warning')).called(1);
verifyNever(mockClient1.logFatal('Fatal'));
verify(mockClient1.logError('Error')).called(1);
verify(mockClient2.logMessage('Message')).called(1);
verifyNever(mockClient2.logWarning('Warning'));
verify(mockClient2.logFatal('Fatal')).called(1);
verifyNever(mockClient2.logError('Error'));
});

test('use all LogLevels', () {
final mockClient = MockClient();

putLumberdashToWork(withClients: [mockClient]);
logMessage('Message');
logWarning('Warning');
logFatal('Fatal');
logError('Error');

verify(mockClient.logMessage('Message')).called(1);
verify(mockClient.logWarning('Warning')).called(1);
verify(mockClient.logFatal('Fatal')).called(1);
verify(mockClient.logError('Error')).called(1);
});

test('filter client of a specific LogLevel', () {
final mockClient = FilterOutClient();

putLumberdashToWorkByLogLevel(withClients: {
mockClient: [LogLevel.message]
});
logMessage('Message', exceptFor: [FilterOutClient]);
logWarning('Warning');
logFatal('Fatal');
logError('Error');

verifyZeroInteractions(mockClient);
});
});
}
2 changes: 1 addition & 1 deletion print_lumberdash/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: print_lumberdash
description: >-
Lumberdash plugin that uses print() to output your logs
version: 3.0.0
version: 3.1.0
authors:
- BMW Group <flutter-dev@bmwgroup.com>
repository: https://github.com/bmw-tech/lumberdash
Expand Down
2 changes: 1 addition & 1 deletion sentry_lumberdash/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: sentry_lumberdash
description: >-
Lumberdash plugin that sends your logs to Sentry with the proper
severity level
version: 3.2.0
version: 3.3.0
repository: https://github.com/bmw-tech/lumberdash
issue_tracker: https://github.com/bmw-tech/lumberdash/issues
homepage: https://bmw-tech.github.io/lumberdash/
Expand Down