leancodepl / leancodepl/logging_bugfender

Add `ColoredTextPrintStrategy`

Open
#8 0 comments 0 reactions 1 assignee View on GitHub

@mateusz-pietras is already working on this.

Since Nov 7, 2024.

good first issue
Dominant language
Dart
Stars
2
Forks
0
PR merge metrics
No merged PRs in 30d

Description

It'd be nice to have logs of different levels be colored appropriately.

Idea for implementation below (it was actually my take at it but I didn't have time to debug & finish it)

/// Instructs [LoggingBugfenderListener] to print colored text.
/// The color is based on the log level.
class ColoredTextPrintStrategy extends PrintStrategy {
  /// Creates a strategy that prints colored plain text.
  const ColoredTextPrintStrategy();

  static final _levelColors = <Level, String?>{
    Level.FINEST: '8',
    Level.FINER: '8',
    Level.FINE: '8',
    Level.CONFIG: null,
    Level.INFO: '12',
    Level.WARNING: '208',
    Level.SEVERE: '196',
    Level.SHOUT: '199',
  };

  @override
  String print(LogRecord record) {
    final log = StringBuffer()
      ..writeAll(
        <String>[
          '[${record.level.name}]',
          if (record.loggerName.isNotEmpty) '${record.loggerName}:',
          record.message,
        ],
        ' ',
      );

    if (record.error != null) {
      log.write('\n${record.error}');
    }
    if (record.stackTrace != null) {
      log.write('\n${record.stackTrace}');
    }

    final color = _levelColors[record.level];
    if (color != null) {
      return '\x1B[${color}m$log\x1B[0m';
    } else {
      return log.toString();
    }
  }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.