fartstack/shared_models/lib/fart_logger.dart

55 lines
1.8 KiB
Dart

import 'dart:io';
import 'package:logging/logging.dart';
class FartLogger {
static void listen({required bool isDevelopment}) {
final logLevel = Platform.environment['LOG_LEVEL'] ?? (isDevelopment ? 'FINEST' : 'INFO');
Logger.root.level =
Level.LEVELS.firstWhere((l) => l.name == logLevel, orElse: () => Level.INFO); // defaults to Level.INFO
Logger.root.onRecord.listen((record) {
_writeLogRecord(record, record.level.value >= Level.SEVERE.value ? stderr : stdout);
});
}
static void _writeLogRecord(LogRecord record, IOSink iosink) {
// Write the basic log message with colored level
iosink.writeln(
'[${_getColoredLevel(record.level.name)}]:[${record.loggerName}] '
'${record.time}: ${record.message}',
);
// Additional details for severe logs
if (record.level.value >= Level.SEVERE.value) {
iosink.writeln(
'[${_getColoredLevel(record.level.name)}]:[${record.loggerName}] '
'${record.error?.toString() ?? "No error provided"}\n'
'${record.stackTrace?.toString() ?? "No trace provided"}',
);
}
}
static void printLevels() {
for (final lvl in Level.LEVELS) {
_writeLogRecord(LogRecord(lvl, 'Test message', 'main'), stdout);
}
}
static const Map<String, String> _levelColors = {
'FINEST': '\x1B[1;37m', // White
'FINER': '\x1B[1;38m', // Gray
'FINE': '\x1B[1;35m', // Purple
'CONFIG': '\x1B[1;36m', // Cyan
'INFO': '\x1B[1;32m', // Green
'WARNING': '\x1B[1;33m', // Yellow
'SEVERE': '\x1B[1;31m', // Red
'SHOUT': '\x1B[1;38;5;52m\x1B[1;48;5;213m', // Red on pink
};
static const String _resetColor = '\x1B[0m';
static String _getColoredLevel(String levelName) {
return '${_levelColors[levelName] ?? ''}$levelName$_resetColor';
}
}