diff --git a/colorize_lumberdash/pubspec.yaml b/colorize_lumberdash/pubspec.yaml index 9689fd4..adc2514 100644 --- a/colorize_lumberdash/pubspec.yaml +++ b/colorize_lumberdash/pubspec.yaml @@ -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 repository: https://github.com/bmw-tech/lumberdash diff --git a/file_lumberdash/pubspec.yaml b/file_lumberdash/pubspec.yaml index 2fc1517..5bb03aa 100644 --- a/file_lumberdash/pubspec.yaml +++ b/file_lumberdash/pubspec.yaml @@ -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/ diff --git a/firebase_lumberdash/pubspec.yaml b/firebase_lumberdash/pubspec.yaml index d29b84f..72247ba 100644 --- a/firebase_lumberdash/pubspec.yaml +++ b/firebase_lumberdash/pubspec.yaml @@ -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/ diff --git a/lumberdash/lib/src/lumberdash.dart b/lumberdash/lib/src/lumberdash.dart index 010a705..51076e4 100644 --- a/lumberdash/lib/src/lumberdash.dart +++ b/lumberdash/lib/src/lumberdash.dart @@ -1,79 +1,103 @@ import 'client/client.dart'; -/// Initialize the `lumberdashClients` internally -List _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 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> 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? extras, List 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? extras, List 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? extras, List 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 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 clientsToFilterOut, - Logger logger, -) { - _lumberdashClients - .where((client) => !clientsToFilterOut.contains(client.runtimeType)) + List clientsToFilterOut, Logger logger, LogLevel logLevel,) { + _lumberdashClients[logLevel] + ?.where((client) => !clientsToFilterOut.contains(client.runtimeType)) .forEach((c) => logger(c)); } diff --git a/lumberdash/pubspec.yaml b/lumberdash/pubspec.yaml index c8d5385..ea396b3 100644 --- a/lumberdash/pubspec.yaml +++ b/lumberdash/pubspec.yaml @@ -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 repository: https://github.com/bmw-tech/lumberdash diff --git a/lumberdash/test/lumberdash_test.dart b/lumberdash/test/lumberdash_test.dart index 9712fbd..836c7b9 100644 --- a/lumberdash/test/lumberdash_test.dart +++ b/lumberdash/test/lumberdash_test.dart @@ -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); + }); + }); } diff --git a/print_lumberdash/pubspec.yaml b/print_lumberdash/pubspec.yaml index 33b871a..0d906d0 100644 --- a/print_lumberdash/pubspec.yaml +++ b/print_lumberdash/pubspec.yaml @@ -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 repository: https://github.com/bmw-tech/lumberdash diff --git a/sentry_lumberdash/pubspec.yaml b/sentry_lumberdash/pubspec.yaml index 92b50cb..8ece2c7 100644 --- a/sentry_lumberdash/pubspec.yaml +++ b/sentry_lumberdash/pubspec.yaml @@ -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/