log_utils.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'dart:convert' as convert;
  2. import 'package:common_utils/common_utils.dart';
  3. import 'package:ftrecruiter/comm/constants/app_constant.dart';
  4. /// 输出Log工具类
  5. class Log {
  6. static const String tag = 'DEER-LOG';
  7. static void init() {
  8. LogUtil.init(isDebug: !AppConstant.inProduction);
  9. }
  10. static void d(String msg, {String tag = tag}) {
  11. if (!AppConstant.inProduction) {
  12. LogUtil.v(msg, tag: tag);
  13. }
  14. }
  15. static void e(String msg, {String tag = tag}) {
  16. if (!AppConstant.inProduction) {
  17. LogUtil.e(msg, tag: tag);
  18. }
  19. }
  20. static void json(String msg, {String tag = tag}) {
  21. if (!AppConstant.inProduction) {
  22. try {
  23. final dynamic data = convert.json.decode(msg);
  24. if (data is Map) {
  25. _printMap(data);
  26. } else if (data is List) {
  27. _printList(data);
  28. } else
  29. LogUtil.v(msg, tag: tag);
  30. } catch(e) {
  31. LogUtil.e(msg, tag: tag);
  32. }
  33. }
  34. }
  35. // https://github.com/Milad-Akarie/pretty_dio_logger
  36. static void _printMap(Map data, {String tag = tag, int tabs = 1, bool isListItem = false, bool isLast = false}) {
  37. final bool isRoot = tabs == 1;
  38. final String initialIndent = _indent(tabs);
  39. tabs++;
  40. if (isRoot || isListItem) {
  41. LogUtil.v('$initialIndent{', tag: tag);
  42. }
  43. data.keys.toList().asMap().forEach((index, dynamic key) {
  44. final bool isLast = index == data.length - 1;
  45. dynamic value = data[key];
  46. if (value is String) {
  47. value = '"$value"';
  48. }
  49. if (value is Map) {
  50. if (value.isEmpty)
  51. LogUtil.v('${_indent(tabs)} $key: $value${!isLast ? ',' : ''}', tag: tag);
  52. else {
  53. LogUtil.v('${_indent(tabs)} $key: {', tag: tag);
  54. _printMap(value, tabs: tabs);
  55. }
  56. } else if (value is List) {
  57. if (value.isEmpty) {
  58. LogUtil.v('${_indent(tabs)} $key: ${value.toString()}', tag: tag);
  59. } else {
  60. LogUtil.v('${_indent(tabs)} $key: [', tag: tag);
  61. _printList(value, tabs: tabs);
  62. LogUtil.v('${_indent(tabs)} ]${isLast ? '' : ','}', tag: tag);
  63. }
  64. } else {
  65. final msg = value.toString().replaceAll('\n', '');
  66. LogUtil.v('${_indent(tabs)} $key: $msg${!isLast ? ',' : ''}', tag: tag);
  67. }
  68. });
  69. LogUtil.v('$initialIndent}${isListItem && !isLast ? ',' : ''}', tag: tag);
  70. }
  71. static void _printList(List list, {String tag = tag, int tabs = 1}) {
  72. list.asMap().forEach((i, dynamic e) {
  73. final bool isLast = i == list.length - 1;
  74. if (e is Map) {
  75. if (e.isEmpty) {
  76. LogUtil.v('${_indent(tabs)} $e${!isLast ? ',' : ''}', tag: tag);
  77. } else {
  78. _printMap(e, tabs: tabs + 1, isListItem: true, isLast: isLast);
  79. }
  80. } else {
  81. LogUtil.v('${_indent(tabs + 2)} $e${isLast ? '' : ','}', tag: tag);
  82. }
  83. });
  84. }
  85. static String _indent([int tabCount = 1]) => ' ' * tabCount;
  86. }