From 371ff51b4816531d0f686e063a13bc33107cbf42 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Mon, 13 Jun 2022 11:51:34 -0500 Subject: [PATCH 1/2] feat: add onHotReloadLog --- CHANGELOG.md | 4 ++++ README.md | 2 ++ lib/shelf_hotreload.dart | 2 +- lib/src/with_hotreload.dart | 30 +++++++++++++++++++++++++----- pubspec.yaml | 3 ++- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2009f3b..842ac67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.3.0 + +- Add `onHotReloadLog` and `logLevel` parameters + ## 1.2.0 - Added callbacks for custom logging output. diff --git a/README.md b/README.md index d524ac7..acf9e1b 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ withHotreload( onReloaded: () => myLogger.log('Reload!'), onHotReloadNotAvailable: () => myLogger.log('No hot-reload :('), onHotReloadAvailable: () => myLogger.log('Yay! Hot-reload :)'), + onHotReloadLog: (log) => myLogger.log('Reload Log: ${log.message}'), + logLevel: Level.INFO, ); ``` diff --git a/lib/shelf_hotreload.dart b/lib/shelf_hotreload.dart index 2147a19..21e0109 100644 --- a/lib/shelf_hotreload.dart +++ b/lib/shelf_hotreload.dart @@ -2,4 +2,4 @@ library shelf_hotreload; export 'src/with_hotreload.dart'; - +export 'package:logging/logging.dart' show Level, LogRecord; diff --git a/lib/src/with_hotreload.dart b/lib/src/with_hotreload.dart index 0eaeef1..9551efc 100644 --- a/lib/src/with_hotreload.dart +++ b/lib/src/with_hotreload.dart @@ -5,6 +5,7 @@ import 'dart:io'; import 'package:hotreloader/hotreloader.dart'; import 'package:intl/intl.dart'; +import 'package:logging/logging.dart' as logging; /// Provides hot-reloading ability to code that providers an http server. /// @@ -24,10 +25,17 @@ void withHotreload( /// If not set, it will `print()` a default message. FutureOr Function()? onHotReloadAvailable, - /// Called once if hot-reload is not available (ee.g. no VM service available) + /// Called once if hot-reload is not available (e.g. no VM service available) /// /// If not set, it will `print()` a default message. FutureOr Function()? onHotReloadNotAvailable, + + /// Called every time a hot reload log is recorded. + FutureOr Function(logging.LogRecord log)? onHotReloadLog, + + /// The log level to use when calling `onHotReloadLog`. + /// By default logging is turned off. + logging.Level logLevel = logging.Level.OFF, }) async { /// Current server instance HttpServer? runningServer; @@ -35,15 +43,27 @@ void withHotreload( /// Set default messages onReloaded ??= () { final time = DateFormat.Hms().format(DateTime.now()); - print('[hotreload] $time - Application reloaded.'); + stdout.writeln('[hotreload] $time - Application reloaded.'); }; onHotReloadAvailable ??= () { - print('[hotreload] Hot reload is enabled.'); + stdout.writeln('[hotreload] Hot reload is enabled.'); }; onHotReloadNotAvailable ??= () { - print( - '[hotreload] Hot reload not enabled. Run this app with --enable-vm-service (or use debug run) in order to enable hot reload.'); + stdout.writeln( + '[hotreload] Hot reload not enabled. Run this app with --enable-vm-service (or use debug run) in order to enable hot reload.', + ); }; + onHotReloadLog ??= (log) { + final time = DateFormat.Hms().format(log.time); + (log.level < logging.Level.SEVERE ? stdout : stderr).writeln( + '[hotreload] $time - ${log.message}', + ); + }; + + /// Configure logging + HotReloader.logLevel = logLevel; + logging.hierarchicalLoggingEnabled = true; + logging.Logger.root.onRecord.listen(onHotReloadLog); /// Function in charge of replacing the running http server final obtainNewServer = (FutureOr Function() create) async { diff --git a/pubspec.yaml b/pubspec.yaml index 8259682..88412a1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: shelf_hotreload description: Wrapper to easily enable hot-reload for shelf applications. -version: 1.2.0 +version: 1.3.0 repository: https://github.com/felixblaschke/shelf_hotreload environment: @@ -9,6 +9,7 @@ environment: dependencies: hotreloader: ^3.0.4 intl: ^0.17.0 + logging: ^1.0.2 dev_dependencies: lints: ^2.0.0 From cc63c22ac666985b45ce23e1b75d7fe88507499c Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Mon, 13 Jun 2022 12:36:32 -0500 Subject: [PATCH 2/2] tweak --- lib/src/with_hotreload.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/with_hotreload.dart b/lib/src/with_hotreload.dart index 9551efc..1e82ec5 100644 --- a/lib/src/with_hotreload.dart +++ b/lib/src/with_hotreload.dart @@ -61,8 +61,8 @@ void withHotreload( }; /// Configure logging - HotReloader.logLevel = logLevel; logging.hierarchicalLoggingEnabled = true; + HotReloader.logLevel = logLevel; logging.Logger.root.onRecord.listen(onHotReloadLog); /// Function in charge of replacing the running http server