Add timestamp to log entries.

This commit is contained in:
Dain Nilsson 2022-05-30 16:04:50 +02:00
parent 9fae52b48b
commit 627e6bdadd
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
2 changed files with 11 additions and 2 deletions

View File

@ -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<Level> {
final List<String> _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}');
}

View File

@ -131,7 +131,8 @@ Future<Widget> initialize(List<String> argv) async {
void _initLogging(List<String> 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);
}