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/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<LogRecord> 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/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 diff --git a/test/logging_test.dart b/test/logging_test.dart index 34f4292..2db7af7 100644 --- a/test/logging_test.dart +++ b/test/logging_test.dart @@ -167,6 +167,50 @@ void main() { expect(shout.stackTrace, isNull); }); + group( + 'cancelLogging', + () { + final root = Logger.root; + final records = <LogRecord>[]; + + setUp(() { + records.clear(); + recordStackTraceAtLevel = Level.ALL; + }); + + 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', () { var root = Logger.root; @@ -597,6 +641,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 = [];