From 85a7a0d17642d151a404e9036ccbcddaee1a966f Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 7 Aug 2022 10:38:18 +0300 Subject: [PATCH 1/5] Add cancelLogging method --- lib/src/logger.dart | 26 ++++++++++- test/logging_test.dart | 100 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/lib/src/logger.dart b/lib/src/logger.dart index f702c73..417aa99 100644 --- a/lib/src/logger.dart +++ b/lib/src/logger.dart @@ -143,10 +143,32 @@ class Logger { /// ``` Stream get onRecord => _getStream(); + /// Allow to cancel logging for current logger and its children + /// if [hierarchicalLoggingEnabled] is true. + /// To start listen for messages again with current logger + /// you should create new subscription(and for any child if required): + /// + /// ```dart + /// logger.onRecord.listen((record) { ... }); + /// ``` + /// + void cancelLogging() { + _clearListeners(); + if (hierarchicalLoggingEnabled) { + for (final logger in _children.values) { + logger.cancelLogging(); + } + } + } + + void _clearListeners() { + _controller?.close(); + _controller = null; + } + void clearListeners() { if (hierarchicalLoggingEnabled || parent == null) { - _controller?.close(); - _controller = null; + _clearListeners(); } else { root.clearListeners(); } diff --git a/test/logging_test.dart b/test/logging_test.dart index 34f4292..d027ab3 100644 --- a/test/logging_test.dart +++ b/test/logging_test.dart @@ -167,6 +167,31 @@ void main() { expect(shout.stackTrace, isNull); }); + test('could not add event when logging is canceled', () { + final root = Logger.root; + final records = []; + recordStackTraceAtLevel = Level.ALL; + root.onRecord.listen(records.add); + + root.cancelLogging(); + + root.severe('hello'); + root.warning('hello'); + root.info('hello'); + expect(records, hasLength(0)); + }); + + test('new events added well when logging is restored', () { + final root = Logger.root; + final records = []; + recordStackTraceAtLevel = Level.ALL; + root.onRecord.listen(records.add); + root.severe('hello'); + root.warning('hello'); + root.info('hello'); + expect(records, hasLength(3)); + }); + group('zone gets recorded to LogRecord', () { test('root zone', () { var root = Logger.root; @@ -597,6 +622,81 @@ void main() { ])); }); + test('message logging - with hierarchy and cancel logging', () { + hierarchicalLoggingEnabled = true; + + root.cancelLogging(); + + root.level = Level.ALL; + + final rootMessages = []; + final bMessages = []; + final aMessages = []; + final cMessages = []; + root.onRecord.listen((record) { + rootMessages.add('${record.level}: ${record.message}'); + }); + a.onRecord.listen((record) { + aMessages.add('${record.level}: ${record.message}'); + }); + c.onRecord.listen((record) { + cMessages.add('${record.level}: ${record.message}'); + }); + + root.info('1'); + root.fine('2'); + root.shout('3'); + + b.info('4'); + + a.cancelLogging(); + + b.severe('5'); + b.warning('6'); + + b.onRecord.listen((record) { + bMessages.add('${record.level}: ${record.message}'); + }); + + b.fine('7'); + + c.fine('8'); + c.warning('9'); + c.shout('10'); + + expect( + rootMessages, + equals([ + 'INFO: 1', + 'FINE: 2', + 'SHOUT: 3', + 'INFO: 4', + 'SEVERE: 5', + 'WARNING: 6', + 'FINE: 7', + 'FINE: 8', + 'WARNING: 9', + 'SHOUT: 10' + ])); + + expect( + aMessages, + equals([ + 'INFO: 4', + ])); + + expect(cMessages, equals([])); + + expect( + bMessages, + equals([ + 'FINE: 7', + 'FINE: 8', + 'WARNING: 9', + 'SHOUT: 10', + ])); + }); + test('message logging - lazy functions', () { root.level = Level.INFO; var messages = []; From c4855c8fb93ad8f4f079b71063f2e82677e93fc0 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 7 Aug 2022 11:07:11 +0300 Subject: [PATCH 2/5] Add description to CHANGELOG.md and bump version --- CHANGELOG.md | 5 +++++ pubspec.yaml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6746eb4..e7a0ba2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.2.0 + +* Add `Logger.cancelLogging` which clear listeners for the current logger, + and, when `hierarchicalLoggingEnabled` is true, for all children of the current logger. + ## 1.1.0 * Add `Logger.attachedLoggers` which exposes all loggers created with the diff --git a/pubspec.yaml b/pubspec.yaml index 0fcd812..9289ef7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: logging -version: 1.1.0 +version: 1.2.0 description: >- Provides APIs for debugging and error logging, similar to loggers in other From 58dbdfa66cb7324928943a30c22887eb84a4af58 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 13 Aug 2022 11:39:13 +0300 Subject: [PATCH 3/5] Bump version to 2.0.0 --- CHANGELOG.md | 2 +- pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7a0ba2..02ac511 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 1.2.0 +## 2.0.0 * Add `Logger.cancelLogging` which clear listeners for the current logger, and, when `hierarchicalLoggingEnabled` is true, for all children of the current logger. diff --git a/pubspec.yaml b/pubspec.yaml index 9289ef7..f56b542 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: logging -version: 1.2.0 +version: 2.0.0 description: >- Provides APIs for debugging and error logging, similar to loggers in other From 1cac4ca7cd0e7f52e4a87ac175acc265c0121dad Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 30 Aug 2022 22:26:31 +0300 Subject: [PATCH 4/5] Rollback major version to minor due to `test` transitive dependency on `logging ^1.0.0` --- CHANGELOG.md | 2 +- pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02ac511..e7a0ba2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 2.0.0 +## 1.2.0 * Add `Logger.cancelLogging` which clear listeners for the current logger, and, when `hierarchicalLoggingEnabled` is true, for all children of the current logger. diff --git a/pubspec.yaml b/pubspec.yaml index f56b542..9289ef7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: logging -version: 2.0.0 +version: 1.2.0 description: >- Provides APIs for debugging and error logging, similar to loggers in other From ac90151faa625bcc28ec8e3af17fc9626362d1ce Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 31 Aug 2022 10:54:16 +0300 Subject: [PATCH 5/5] fix tests, grouped tests, add setup,teardown --- test/logging_test.dart | 65 +++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/test/logging_test.dart b/test/logging_test.dart index d027ab3..2db7af7 100644 --- a/test/logging_test.dart +++ b/test/logging_test.dart @@ -167,30 +167,49 @@ void main() { expect(shout.stackTrace, isNull); }); - test('could not add event when logging is canceled', () { - final root = Logger.root; - final records = []; - recordStackTraceAtLevel = Level.ALL; - root.onRecord.listen(records.add); - - root.cancelLogging(); - - root.severe('hello'); - root.warning('hello'); - root.info('hello'); - expect(records, hasLength(0)); - }); + group( + 'cancelLogging', + () { + final root = Logger.root; + final records = []; + + setUp(() { + records.clear(); + recordStackTraceAtLevel = Level.ALL; + }); - test('new events added well when logging is restored', () { - final root = Logger.root; - final records = []; - recordStackTraceAtLevel = Level.ALL; - root.onRecord.listen(records.add); - root.severe('hello'); - root.warning('hello'); - root.info('hello'); - expect(records, hasLength(3)); - }); + tearDown(() { + recordStackTraceAtLevel = Level.OFF; + root.clearListeners(); + }); + + test('could not add event when logging is canceled', () { + root.onRecord.listen(records.add); + + root.cancelLogging(); + + root.severe('hello'); + root.warning('hello'); + root.info('hello'); + + expect(records, hasLength(0)); + }); + + test('new events added well when logging is restored', () { + root.onRecord.listen(records.add); + + root.cancelLogging(); + + root.onRecord.listen(records.add); + + root.severe('hello'); + root.warning('hello'); + root.info('hello'); + + expect(records, hasLength(3)); + }); + }, + ); group('zone gets recorded to LogRecord', () { test('root zone', () {