Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter stack traces when filtering messages that have stack traces #243

Open
BenjiFarquhar opened this issue Jun 24, 2024 · 0 comments
Open

Comments

@BenjiFarquhar
Copy link

BenjiFarquhar commented Jun 24, 2024

Is your feature request related to a problem? Please describe.
I'm frustrated when I filter out a message, but its stack trace is not filtered out.

Describe the solution you'd like
When a message is filtered out, its stack trace should also be filtered out.

Describe alternatives you've considered
I've had to extend TalkerLogger to override log which I don't think is normal usage of this package.

Additional context
This is the print (generated by a code generation package) that I want filtered out:

      debugPrintStack(
          label:
              '[${path ?? 'NickNameForm'}]\n┗━ Avoid calling `model` on invalid form. Possible exceptions for non-nullable fields which should be guarded by `required` validator.');

this is what I had to do to filter it out completely:

// We had to extend TalkerLogger due to the message we wanted to filter
// occurring over a large amount of messages due to the stack trace being split
//into multiple subsequent messages.
class CustomTalkerLogger extends TalkerLogger {
  bool _ignoreNextStackTrace = false;

  CustomTalkerLogger({
    super.settings,
    LoggerFormatter? formatter,
    LoggerFilter? filter,
    super.output,
  }) : super(
          formatter: formatter ?? const ExtendedLoggerFormatter(),
          filter: filter ?? LogLevelFilter(settings?.level ?? LogLevel.debug),
        );

  @override
  CustomTalkerLogger copyWith({
    TalkerLoggerSettings? settings,
    LoggerFormatter? formatter,
    LoggerFilter? filter,
    Function(String message)? output,
  }) {
    return CustomTalkerLogger(
      settings: settings ?? this.settings,
      formatter: formatter ?? this.formatter,
      filter: filter ?? LogLevelFilter(settings?.level ?? LogLevel.debug),
      output: output ?? log_output.outputLog,
    );
  }

  @override
  void log(msg, {LogLevel? level, AnsiPen? pen}) {
    if (msg.toString().contains('Avoid calling `model` on invalid form')) {
      _ignoreNextStackTrace = true;
      return;
    }

    if (_ignoreNextStackTrace) {
      if (level == LogLevel.debug && msg.toString().startsWith('#')) {
        return;
      } else {
        _ignoreNextStackTrace = false;
      }
    }

    super.log($msg, level: level, pen: pen);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant