Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add onHotReloadLog #5

Merged
merged 2 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.3.0

- Add `onHotReloadLog` and `logLevel` parameters

## 1.2.0

- Added callbacks for custom logging output.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
```

Expand Down
2 changes: 1 addition & 1 deletion lib/shelf_hotreload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
library shelf_hotreload;

export 'src/with_hotreload.dart';

export 'package:logging/logging.dart' show Level, LogRecord;
30 changes: 25 additions & 5 deletions lib/src/with_hotreload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -24,26 +25,45 @@ void withHotreload(
/// If not set, it will `print()` a default message.
FutureOr<void> 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<void> Function()? onHotReloadNotAvailable,

/// Called every time a hot reload log is recorded.
FutureOr<void> 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;

/// 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
logging.hierarchicalLoggingEnabled = true;
HotReloader.logLevel = logLevel;
logging.Logger.root.onRecord.listen(onHotReloadLog);

/// Function in charge of replacing the running http server
final obtainNewServer = (FutureOr<HttpServer> Function() create) async {
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -9,6 +9,7 @@ environment:
dependencies:
hotreloader: ^3.0.4
intl: ^0.17.0
logging: ^1.0.2

dev_dependencies:
lints: ^2.0.0
Expand Down