Logger constructor
Logger(
- EnvContract _env
Implementation
Logger(this._env) {
final level = _env.get(AppEnv.logLevel);
final dartEnv = _env.get(AppEnv.dartEnv);
final bool logLevel = LogLevel.values
.map((level) => level.name)
.contains(level.toUpperCase());
if (!logLevel) {
throw Exception(
'Invalid LOG_LEVEL environment variable, please include in ${LogLevel.values.map((level) => level.name.toLowerCase())}');
}
logging.Logger.root.level = LogLevel.values
.firstWhere((element) => element.name == level.toUpperCase())
.value;
logging.Logger.root.onRecord.listen((record) {
final time = '[${DateFormat.Hms().format(record.time)}]';
List<Sequence> makeMessage(
String messageType, Color messageColor, List<Sequence> message) {
return [
SetStyles(Style.foreground(Color.brightBlack)),
Print(time),
SetStyles(Style.foreground(messageColor)),
Print(' $messageType'),
SetStyles.reset,
Print(': '),
...message,
SetStyles.reset,
AsciiControl.lineFeed,
];
}
final message = switch (record.level) {
logging.Level.FINEST => makeMessage('trace', Color.white, [
SetStyles(Style.foreground(Color.brightBlack)),
Print(record.message)
]),
logging.Level.SHOUT =>
makeMessage('fatal', Color.brightRed, [Print(record.message)]),
logging.Level.SEVERE =>
makeMessage('error', Color.red, [Print(record.message)]),
logging.Level.WARNING =>
makeMessage('warn', Color.yellow, [Print(record.message)]),
logging.Level.INFO => makeMessage(
'info', Color.fromRGB(140, 169, 238), [Print(record.message)]),
_ => makeMessage('unknown', Color.blue, [Print(record.message)]),
};
if (dartEnv == 'production') {
message.writeWithoutAnsi();
return;
}
if (stdout.supportsAnsiEscapes) {
stdout.writeAnsiAll(message);
return;
}
message.writeWithoutAnsi();
});
}