simc / logger

Small, easy to use and extensible logger which prints beautiful logs.
https://pub.dev/packages/logger
MIT License
1.07k stars 129 forks source link

Support Stacktrace filters #129

Closed mirland closed 1 year ago

mirland commented 2 years ago

Description:

If you wrap the logger in a custom logger file, the first line of the stack trace is your wrapper class (and it's not useful).

So if you have this:

import 'package:logger/logger.dart' as dart_log;

abstract class Logger {
  static final dart_log.Logger _instance = dart_log.Logger(
    printer: dart_log.PrettyPrinter(),
  );

  static void v(dynamic message, [dynamic error, StackTrace? stackTrace]) =>
      _instance.log(dart_log.Level.verbose, message, error, stackTrace);

  static void d(dynamic message, [dynamic error, StackTrace? stackTrace]) =>
      _instance.log(dart_log.Level.debug, message, error, stackTrace);

Then you call Logger.d(), the first stacktrace entry logger is not useful.

To solve that, I added a stacktraceFilters, which enables you to define custom filters. So, now you can do something like:

  static final dart_log.Logger _instance = dart_log.Logger(
    printer: dart_log.PrettyPrinter(
      stacktraceFilters: [
        RegExp(r'^(packages/.*/core/source/common/logger.dart).*')
      ],
    ),
  );
haarts commented 2 years ago

I'm not convinced adding regexes to the PrettyPrinter class is warranted given this use case. What about using a custom LogPrinter instead?

mirland commented 2 years ago

Hey, thanks for reviewing my pr! I see your point. Maybe it's a particular case. However, if I want to do that, I have to copy-paste the printer (because the methods are private). What do you think about creating a public method to filter the log entries? So we can override the class and extend the features.

I will create a new commit with the change so you can check it. It's just an idea, if you think that it's not necessary or it's not aligned with the project I can close the pr.

mirland commented 2 years ago

@haarts , with this change we can create a custom printer like:

class CustomPrettyPrinter extends PrettyPrinter {
  final List<RegExp> stacktraceFilters;

  CustomPrettyPrinter({
    stackTraceBeginIndex = 0,
    methodCount = 2,
    errorMethodCount = 8,
    lineLength = 120,
    colors = true,
    printEmojis = true,
    printTime = false,
    excludeBox = const {},
    noBoxingByDefault = false,
    this.stacktraceFilters = const [],
  }) : super(
          stackTraceBeginIndex: stackTraceBeginIndex,
          methodCount: methodCount,
          errorMethodCount: errorMethodCount,
          lineLength: lineLength,
          colors: colors,
          printEmojis: printEmojis,
          printTime: printTime,
          excludeBox: excludeBox,
          noBoxingByDefault: noBoxingByDefault,
        );

  bool _discardUserStacktraceLine(String line) =>
      stacktraceFilters.any((element) => element.hasMatch(line));

  @override
  bool includeStacktraceLine(String line) =>
      super.includeStacktraceLine(line) && !_discardUserStacktraceLine(line);
}

Again, if you think it's not aligned with the project you can just close the pr!