logger icon indicating copy to clipboard operation
logger copied to clipboard

log too long

Open hongqinghe opened this issue 6 years ago • 7 comments
trafficstars

Android studio log is too long to display incomplete, can you support it?

hongqinghe avatar Aug 28 '19 03:08 hongqinghe

Android Studio seems to have a maximum length on a log line. It would be good for us to detect we're printing to Android logcat and split the lines. But I have no idea if that can be done. In the meanwhile please check out this SO answer, it might help.

haarts avatar Dec 04 '19 11:12 haarts

@hongqinghe you can extend PrettyPrinter and override stringifyMessage() method, to add line break character if message is to long.

Nstd avatar Dec 10 '19 08:12 Nstd

There's even a lineLength constructor argument which you could use! Better yet, there's a terminalColumns property which you could use. This, too, I'd like to implement but it is not terribly high on my todo list.

haarts avatar Dec 10 '19 09:12 haarts

image

lineLength maybe just used to set divider length, not the log length.

Nstd avatar Dec 16 '19 03:12 Nstd

I'm sure it is only used for the divider length. I've been thinking a bit about this and hard wrapping is only desirable if the terminal/console truncates the lines. This only happens with the IDE consoles. What would be best is to use the terminalColums in combination with some way of detecting that we're printing in an IDE console. A good heuristic might be using stdoutSupportsAnsi. The IDE consoles are the only consoles who do NOT support ANSI escapes.

haarts avatar Dec 16 '19 08:12 haarts

class FocusPrinter extends LogPrinter {
  static final levelPrefixes = {
    Level.verbose: '[V]',
    Level.debug: '[D]',
    Level.info: '[I]',
    Level.warning: '[W]',
    Level.error: '[E]',
    Level.wtf: '[WTF]',
  };

  final bool printTime;
  final formatter = DateFormat('HH:mm:ss');

  FocusPrinter({this.printTime = true});

  @override
  List<String> log(LogEvent event) {
    var messageStr = _stringifyMessage(event.message);
    var errorStr = event.error != null ? '  ERROR: ${event.error}' : '';
    return ['${_labelFor(event.level)} $messageStr$errorStr'];
  }

  String _labelFor(Level level) {
    var prefix = levelPrefixes[level];
    var timeStr = printTime ? '${formatter.format(DateTime.now())}' : '';
    var stackTraceStr = StackTrace.current
        .toString()
        .split('\n')[4]
        .replaceFirst(RegExp(r'#\d+\s+'), '');
    return '$prefix $timeStr, $stackTraceStr,';
  }

  String _stringifyMessage(dynamic message) {
    if (message is Map || message is Iterable) {
      var encoder = JsonEncoder.withIndent(null);
      return encoder.convert(message);
    } else {
      return message.toString();
    }
  }
}

Yongle-Fu avatar Sep 30 '20 08:09 Yongle-Fu

In my code, I use var logger = Logger(printer: SimplePrinter(printTime: true), filter: ProductionFilter());;

liudonghua123 avatar Apr 07 '21 07:04 liudonghua123