logger.dart 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'dart:convert';
  2. import 'package:logger/logger.dart';
  3. class Log {
  4. static final Logger _logger = Logger(
  5. printer: PrefixPrinter(PrettyPrinter()),
  6. );
  7. // verbose
  8. static void t(dynamic message) {
  9. _logger.t(message);
  10. }
  11. // debug
  12. static void d(dynamic message) {
  13. _logger.d(message);
  14. }
  15. // info
  16. static void i(dynamic message) {
  17. _logger.i(message);
  18. }
  19. // warning
  20. static void w(dynamic message) {
  21. _logger.w(message);
  22. }
  23. // error
  24. static void e(dynamic message) {
  25. _logger.e(message);
  26. }
  27. // wtf
  28. static void f(dynamic message) {
  29. _logger.f(message);
  30. }
  31. // json
  32. static void json(Map json, {String? title}) {
  33. if (title != null) {
  34. printFullText(title);
  35. }
  36. const encoder = JsonEncoder.withIndent(' '); // 2空格缩进
  37. final prettyString = encoder.convert(json);
  38. printFullText(prettyString);
  39. }
  40. static void printFullText(String text, {String? title}) {
  41. if (title != null) {
  42. print(title);
  43. }
  44. const int maxLineLength = 800;
  45. for (int i = 0; i < text.length; i += maxLineLength) {
  46. final end = (i + maxLineLength < text.length) ? i + maxLineLength : text.length;
  47. print(text.substring(i, end));
  48. }
  49. }
  50. }