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

log too long #10

Open hongqinghe opened 5 years ago

hongqinghe commented 5 years ago

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

haarts commented 4 years ago

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.

Nstd commented 4 years ago

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

haarts commented 4 years ago

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.

Nstd commented 4 years ago

image

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

haarts commented 4 years ago

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.

Yongle-Fu commented 3 years ago
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();
    }
  }
}
liudonghua123 commented 3 years ago

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