Skip to content

Commit 22d92ea

Browse files
committed
refactor(server): move logger setup to eager entrypoint
Relocates the global logger configuration from the root middleware (`routes/_middleware.dart`) to the new eager-loading entrypoint (`bin/main.dart`). This is an architectural improvement that ensures the logger is configured exactly once when the application process starts, rather than within a middleware that could be re-instantiated. It makes the startup sequence more robust and simplifies the root middleware's responsibilities.
1 parent fb0795c commit 22d92ea

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

bin/main.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ import '../.dart_frog/server.dart' as server;
2121
/// "fail-fast" approach suitable for production environments.
2222
Future<void> main(List<String> args) async {
2323
// Use a local logger for startup-specific messages.
24+
// This is also the ideal place to configure the root logger for the entire
25+
// application, as it's guaranteed to run only once at startup.
26+
Logger.root.level = Level.ALL;
27+
Logger.root.onRecord.listen((record) {
28+
// A more detailed logger that includes the error and stack trace.
29+
// ignore: avoid_print
30+
print(
31+
'${record.level.name}: ${record.time}: ${record.loggerName}: '
32+
'${record.message}',
33+
);
34+
if (record.error != null) {
35+
// ignore: avoid_print
36+
print(' ERROR: ${record.error}');
37+
}
38+
if (record.stackTrace != null) {
39+
// ignore: avoid_print
40+
print(' STACK TRACE: ${record.stackTrace}');
41+
}
42+
});
43+
2444
final log = Logger('EagerEntrypoint');
2545

2646
try {

routes/_middleware.dart

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,12 @@ import 'package:mongo_dart/mongo_dart.dart';
2222
// --- Middleware Definition ---
2323
final _log = Logger('RootMiddleware');
2424

25-
// A flag to ensure the logger is only configured once for the application's
26-
// entire lifecycle.
27-
bool _loggerConfigured = false;
28-
2925
Handler middleware(Handler handler) {
3026
// This is the root middleware for the entire API. It's responsible for
3127
// providing all shared dependencies to the request context.
3228
// The order of `.use()` calls is important: the last one in the chain
3329
// runs first.
3430

35-
// This check ensures that the logger is configured only once.
36-
if (!_loggerConfigured) {
37-
Logger.root.level = Level.ALL;
38-
Logger.root.onRecord.listen((record) {
39-
// A more detailed logger that includes the error and stack trace.
40-
// ignore: avoid_print
41-
print(
42-
'${record.level.name}: ${record.time}: ${record.loggerName}: '
43-
'${record.message}',
44-
);
45-
if (record.error != null) {
46-
// ignore: avoid_print
47-
print(' ERROR: ${record.error}');
48-
}
49-
if (record.stackTrace != null) {
50-
// ignore: avoid_print
51-
print(' STACK TRACE: ${record.stackTrace}');
52-
}
53-
});
54-
_loggerConfigured = true;
55-
}
56-
5731
return handler
5832
// --- Core Middleware ---
5933
// These run after all dependencies have been provided.

0 commit comments

Comments
 (0)