Skip to content

Commit

Permalink
Update README for ColoredTextPrintStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Xezolpl committed Nov 7, 2024
1 parent 5f10685 commit d5abfe9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,47 @@ void setupLogger(bool debugMode) {
}
```

### Using `ColoredTextPrintStrategy`

To enable log level-based text coloring in console output, consider using
[ColoredTextPrintStrategy]. Note that this strategy may not function
correctly depending on the output terminal and target device.

For optimal results, test this print strategy in your chosen IDE and
output device combination. Consider enabling this feature conditionally,
for example, using dart defines.

1. Prepare code setup

```dart
// ...
void setupLogger(bool debugMode) {
if (debugMode) {
// During debugging, you'll usually want to log everything
Logger.root.level = Level.ALL;
const useColoredText = bool.fromEnvironment("USE_COLORED_TEXT_LOGGING");
LoggingBugfenderListener(
config.bugfenderKey,
consolePrintStrategy: useColoredText
? const ColoredTextPrintStrategy()
: const PlainTextPrintStrategy(),
).listen(Logger.root);
} else {
// ...
}
}
```

2. Pass the flag using `--dart-define`

```
flutter run --dart-define="USE_COLORED_TEXT_LOGGING=true"
```

Note: You can also use --dart-define-from-file which is introduced in Flutter 3.7.

### Custom data

You can also add and remove custom data.
Expand Down
15 changes: 10 additions & 5 deletions lib/src/print_strategy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ class PlainTextPrintStrategy extends PrintStrategy {
}
}

/// Instructs [LoggingBugfenderListener] to print colored text.
/// The color is based on the log level.
/// The colored text works only if the output terminal supports ANSI escape
/// sequences. In most cases you can check if ANSI escapes are supported, using
/// `kIsWeb || io.stdout.supportsAnsiEscapes`.
/// Instructs [LoggingBugfenderListener] to print color-coded text based
/// on log levels. Color display depends on the terminal’s support for ANSI
/// escape sequences, which theoretically can be verified using
/// `io.stdout.supportsAnsiEscapes`. However, this check often fails to return
/// `true` even when ANSI escapes are supported by the terminal.
///
/// For optimal results, test this print strategy in your chosen
/// IDE and output device combination. Consider enabling this feature
/// conditionally, such as through an environment variable.
class ColoredTextPrintStrategy extends PlainTextPrintStrategy {
/// Creates a strategy that prints colored plain text.
const ColoredTextPrintStrategy();
Expand Down

0 comments on commit d5abfe9

Please sign in to comment.