From 7627e8c027b707763c2ee161a28bee8de25794f2 Mon Sep 17 00:00:00 2001 From: Ross Wang Date: Fri, 4 Aug 2023 10:42:32 -0700 Subject: [PATCH] [server] Omit microseconds in log timestamps The default time stringization only includes microseconds if nonzero, which can make the logs look inconsistent, and the microseconds are a bit noisy anyway, so truncate them. --- server/bin/main.dart | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/bin/main.dart b/server/bin/main.dart index c84c99b..20dc4b8 100644 --- a/server/bin/main.dart +++ b/server/bin/main.dart @@ -18,7 +18,16 @@ import 'package:shelf_router/shelf_router.dart'; void initializeLogger() { Logger.root.onRecord.listen((record) { - final message = '[${record.time.toUtc()}] ${record.level} ' + // The default stringization is at microsecond resolution but omits the + // microsegment fragment if the microseconds part is 0. Both the + // inconsistency and the microseconds themselves are not ideal in the logs, + // so let's truncate them. + final formattedTime = DateTime.fromMillisecondsSinceEpoch( + record.time.millisecondsSinceEpoch, + isUtc: true, + ).toString(); + + final message = '[$formattedTime] ${record.level} ' '${record.loggerName}: ${record.message}'; if (record.level >= Level.WARNING ||