logger
logger copied to clipboard
Add ability or an example on how configure logging filters per file/folder
Would be nice to be able to see an example or capability that filters out logging levels of different files much like most Java logging packages.
ie: logger.<folder/file or class/method>.level = Debug logger.<folder/file or class/method>.level = Warn
I get more of these kinds of requests.
The idea I'm currently tossing around is that you will have to use different loggers. Then you can have different log levels thresholds for each part of your program. Somewhere in the future I'm going to add names to a Logger instance so it is evident where the log message originated from.
any solution to this issue?
Thank you for bringing this up again. I promise to add something along these lines next week.
@haarts I have implemented this in the meanwhile... waiting your solution
class AppLogger {
Logger _logger;
static const String tag = "";
bool _active;
AppLogger({Type type, String tag}) {
assert(tag != null || type != null, 'Class runTimeType or Class Tag should be defined');
final String _tag = type?.toString() ?? tag;
_active = LoggerWhiteList.whiteList.firstWhere((element) => element == _tag, orElse: () => null) != null;
this._logger = Logger(
printer: PrettyPrinter(),
);
}
void info(String message, {String context = ''}) {
if (!this._active) {
return;
}
_logger.i(tag + ' ' + (context != "" ? context + ":" : context) + message);
}
void debug(String message, {data, String context: ''}) {
if (this._active) {
_logger.d(tag + message);
}
if (this._active && data != null) {
_logger.d((context != "" ? context + ":" : '') + data.toString());
}
}
void warn(String message, {dynamic data, String context: ''}) {
if (this._active) {
_logger.w(tag + ' ' + (context != "" ? context + ":" : '') + message);
}
}
void error(String message, error, {String context: ''}) {
if (this._active) {
_logger.e((context != "" ? context + ":" : '') + message, error);
}
}
}
I've been implementing this morning when I came to a realization.
The PrefixPrinter already is capable of prefixing a log message with a fixed string.
So you could tackle this issue NOW with a combination of different loggers and the PrefixLogger:
import 'package:logger/logger.dart';
void main() {
final mainLogger = Logger(
printer: PrefixPrinter(SimplePrinter()),
level: Level.debug,
);
final subsystemLogger = Logger(
printer: PrefixPrinter(SimplePrinter()),
level: Level.debug,
);
mainLogger.d('hi from mainLogger');
subsystemLogger.w('hi from subsystemLogger');
}
Running this:
❯ dart --enable-asserts bin/sheep.dart
DEBUG [D] hi from mainLogger
WARNING [W] hi from subsystemLogger
These prefixes DEBUG and WARNING can be configured via the constructor like so PrefixPrinter(debug: 'mainlogger'). At the moment this is a bit annoying since you want to prefix all log entries with the same prefix. But that would only entail a small change to the PrefixLogger.
Would that serve your needs?
This doesn't solve the problem that TAG solves. Idk why don't we want to add tag argument to logging functions. See, when I'm trying to log and then trace some specific data I don't want to create separate logger, prefix printer, etc each time for each type of data I want to trace. It works perfectly with Android https://developer.android.com/reference/android/util/Log.
I don't see a reason why can't we keep it simple and just add tagging capability to the logger. There is a closed PR for this feature as well so as to me it would be perfect just to merge it.
@haarts, WDYT?