From f4e210011936434c3c547bf5cccef78527274197 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 12 May 2023 14:14:41 +0300 Subject: [PATCH 1/6] Logger `onLevelChanged` --- CHANGELOG.md | 4 ++++ README.md | 8 ++++++++ lib/src/logger.dart | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 489b0eb..fcfdb38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## NEXT + +* Add notification when the log level is changed. Logger `onLevelChanged` broadcasts a stream of level values. + ## 1.1.2-wip * Require Dart 2.19. diff --git a/README.md b/README.md index 51b1ae1..5b96de1 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,14 @@ First, set the root `Level`. All messages at or above the current level are sent Then, listen on the `onRecord` stream for `LogRecord` events. The `LogRecord` class has various properties for the message, error, logger name, and more. +To listen for changed level notitfications use: + +```dart +Logger.root.onLevelChanged.listen((level) { + print('The new log level is $level'); +}); +``` + ## Logging messages Create a `Logger` with a unique name to easily identify the source of the log diff --git a/lib/src/logger.dart b/lib/src/logger.dart index f90fd06..2d9e0ce 100644 --- a/lib/src/logger.dart +++ b/lib/src/logger.dart @@ -55,6 +55,9 @@ class Logger { /// root [Logger]. StreamController? _controller; + /// Controller used to notify when the log level of this logger is changed. + StreamController? _levelChangedController; + /// Create or find a Logger by name. /// /// Calling `Logger(name)` will return the same instance whenever it is called @@ -139,7 +142,23 @@ class Logger { throw UnsupportedError( 'Cannot set the level to `null` on a logger with no parent.'); } + final isLevelChanged = _level != value; _level = value; + if (isLevelChanged) { + _levelChangedController?.add(value); + } + } + + /// Returns a stream of level values set to this [Logger]. + /// + /// You can listen for set levels using the standard stream APIs, for instance: + /// + /// ```dart + /// logger.onLevelChanged.listen((level) { ... }); + /// ``` + Stream get onLevelChanged { + _levelChangedController ??= StreamController.broadcast(sync: true); + return _levelChangedController!.stream; } /// Returns a stream of messages added to this [Logger]. From 88e05cbd59c61af791a334a4e30e7d828c21e438 Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Fri, 12 May 2023 20:40:45 +0300 Subject: [PATCH 2/6] Add test --- test/logging_test.dart | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/logging_test.dart b/test/logging_test.dart index f41723e..f2c2f4d 100644 --- a/test/logging_test.dart +++ b/test/logging_test.dart @@ -714,5 +714,15 @@ void main() { expect(records[1].error, isNotNull); expect(records[2].error, isNull); }); + + test('listen for level changed', () { + final levels = []; + root.level = Level.ALL; + root.onLevelChanged.listen(levels.add); + root.level = Level.SEVERE; + root.level = Level.WARNING; + expect(levels, hasLength(2)); + }); + }); } From 5fb234afcdfc8ab9ad16665441d3349a142c066a Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Sat, 13 May 2023 08:12:12 +0300 Subject: [PATCH 3/6] Fix CI errors --- CHANGELOG.md | 5 +---- lib/src/logger.dart | 3 ++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fcfdb38..c3d1719 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,6 @@ -## NEXT - -* Add notification when the log level is changed. Logger `onLevelChanged` broadcasts a stream of level values. - ## 1.1.2-wip +* Add notification when the log level is changed. Logger `onLevelChanged` broadcasts a stream of level values. * Require Dart 2.19. ## 1.1.1 diff --git a/lib/src/logger.dart b/lib/src/logger.dart index 2d9e0ce..414b8ab 100644 --- a/lib/src/logger.dart +++ b/lib/src/logger.dart @@ -151,7 +151,8 @@ class Logger { /// Returns a stream of level values set to this [Logger]. /// - /// You can listen for set levels using the standard stream APIs, for instance: + /// You can listen for set levels using the standard stream APIs, + /// for instance: /// /// ```dart /// logger.onLevelChanged.listen((level) { ... }); From 09e194e661c61f9e2230d150c7c028aa0431cd8c Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Wed, 17 May 2023 23:57:28 +0300 Subject: [PATCH 4/6] Code review changes --- lib/src/logger.dart | 5 ++++- test/logging_test.dart | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/src/logger.dart b/lib/src/logger.dart index 2d9e0ce..bf27f50 100644 --- a/lib/src/logger.dart +++ b/lib/src/logger.dart @@ -151,11 +151,14 @@ class Logger { /// Returns a stream of level values set to this [Logger]. /// - /// You can listen for set levels using the standard stream APIs, for instance: + /// You can listen for set levels using the standard stream APIs, + /// for instance: /// /// ```dart /// logger.onLevelChanged.listen((level) { ... }); /// ``` + /// A state error will be thrown if the level is changed + /// inside the callback. Stream get onLevelChanged { _levelChangedController ??= StreamController.broadcast(sync: true); return _levelChangedController!.stream; diff --git a/test/logging_test.dart b/test/logging_test.dart index f2c2f4d..0c9bd66 100644 --- a/test/logging_test.dart +++ b/test/logging_test.dart @@ -724,5 +724,22 @@ void main() { expect(levels, hasLength(2)); }); + test('onLevelChanged is not emited if set the level to the same value', () { + final levels = []; + root.level = Level.ALL; + root.onLevelChanged.listen(levels.add); + root.level = Level.ALL; + expect(levels, hasLength(0)); + }); + + test('setting level in a loop throws state error', () { + root.level = Level.ALL; + root.onLevelChanged.listen((event) { + // Cannot fire new event. Controller is already firing an event + expect(() => root.level = Level.SEVERE, throwsStateError); + }); + root.level = Level.WARNING; + expect(root.level, Level.SEVERE); + }); }); } From 46630af35c3a5f0ffb0c413ec188c3fc8f309acf Mon Sep 17 00:00:00 2001 From: Desislava Stefanova <95419820+desistefanova@users.noreply.github.com> Date: Wed, 17 May 2023 23:58:20 +0300 Subject: [PATCH 5/6] Update CHANGELOG.md Co-authored-by: Nate Bosch --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3d1719..46f1d32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 1.1.2-wip +## 1.2.0-wip * Add notification when the log level is changed. Logger `onLevelChanged` broadcasts a stream of level values. * Require Dart 2.19. From 0a82515e847c2fc8af63a7cd9a445d05a367ecdd Mon Sep 17 00:00:00 2001 From: Desislava Stefanova Date: Thu, 18 May 2023 08:27:46 +0300 Subject: [PATCH 6/6] set version in pubspec --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 089a114..d75e5b1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: logging -version: 1.1.2-wip +version: 1.2.0-wip description: >- Provides APIs for debugging and error logging, similar to loggers in other languages, such as the Closure JS Logger and java.util.logging.Logger.