From 627e6bdadd09a45daa85fc8fe9de36392efebc5d Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Mon, 30 May 2022 16:04:50 +0200 Subject: [PATCH] Add timestamp to log entries. --- lib/app/logging.dart | 10 +++++++++- lib/desktop/init.dart | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/app/logging.dart b/lib/app/logging.dart index 93fde962..7ab9432b 100755 --- a/lib/app/logging.dart +++ b/lib/app/logging.dart @@ -4,6 +4,13 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:logging/logging.dart'; +String _pad(int value, int zeroes) => value.toString().padLeft(zeroes, '0'); + +extension DateTimeFormat on DateTime { + String get logFormat => + '${_pad(hour, 2)}:${_pad(minute, 2)}:${_pad(second, 2)}.${_pad(millisecond, 3)}'; +} + class Levels { /// Key for tracing information ([value] = 500). static const Level TRAFFIC = Level('TRAFFIC', 500); @@ -45,7 +52,8 @@ class LogLevelNotifier extends StateNotifier { final List _buffer = []; LogLevelNotifier() : super(Logger.root.level) { Logger.root.onRecord.listen((record) { - _buffer.add('[${record.loggerName}] ${record.level}: ${record.message}'); + _buffer.add( + '${record.time.logFormat} [${record.loggerName}] ${record.level}: ${record.message}'); if (record.error != null) { _buffer.add('${record.error}'); } diff --git a/lib/desktop/init.dart b/lib/desktop/init.dart index 4a4f42a7..3ee5e7ea 100755 --- a/lib/desktop/init.dart +++ b/lib/desktop/init.dart @@ -131,7 +131,8 @@ Future initialize(List argv) async { void _initLogging(List argv) { Logger.root.onRecord.listen((record) { - stderr.writeln('[${record.loggerName}] ${record.level}: ${record.message}'); + stderr.writeln( + '${record.time.logFormat} [${record.loggerName}] ${record.level}: ${record.message}'); if (record.error != null) { stderr.writeln(record.error); }