log_util.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:logger/logger.dart';
  4. import 'package:o2oa_all_platform/common/extension/index.dart';
  5. import 'package:o2oa_all_platform/environment_config.dart';
  6. import 'o2_file_path_util.dart';
  7. class OLogger {
  8. static void writeFromGetx(String text, {bool isError = false}) {
  9. Future.microtask(()=> isError ? OLogger.e('from GetX: $text') : OLogger.i('from GetX: $text'));
  10. }
  11. static final OLoggerPrinter _printer = OLoggerPrinter();
  12. static void v(dynamic message) {
  13. _printer.v(message);
  14. }
  15. static void d(dynamic message) {
  16. _printer.d(message);
  17. }
  18. static void i(dynamic message) {
  19. _printer.i(message);
  20. }
  21. static void w(dynamic message) {
  22. _printer.w(message);
  23. }
  24. static void e(dynamic message, [dynamic error, StackTrace? stackTrace]) {
  25. _printer.e(message, error, stackTrace);
  26. }
  27. static void wtf(dynamic message) {
  28. _printer.wtf(message);
  29. }
  30. }
  31. class OLoggerPrinter {
  32. OLoggerPrinter() {
  33. initLogFileOut();
  34. }
  35. Logger _logger = Logger(
  36. filter: ProductionFilter(),
  37. printer: PrefixPrinter(PrettyPrinter(methodCount: 3, printTime: true)),
  38. level: EnvironmentConfig.isRelease() ? Level.info : Level.verbose
  39. );
  40. Future<void> initLogFileOut() async {
  41. String todayFile = '${DateTime.now().ymd()}.log';
  42. String? filePath = await O2FilePathUtil.getLogFilePath(todayFile);
  43. if (filePath != null) {
  44. var file = File(filePath);
  45. if (!file.existsSync()) {
  46. file.createSync();
  47. }
  48. _logger = Logger(
  49. filter: ProductionFilter(),
  50. output: MultiOutput([ConsoleOutput(), FileOutput(file: file)]),
  51. printer: PrefixPrinter(PrettyPrinter(methodCount: 3, printTime: true)),
  52. level: EnvironmentConfig.isRelease() ? Level.info : Level.verbose
  53. );
  54. }
  55. }
  56. void v(dynamic message) {
  57. _logger.v(message);
  58. }
  59. void d(dynamic message) {
  60. _logger.d(message);
  61. }
  62. void i(dynamic message) {
  63. _logger.i(message);
  64. }
  65. void w(dynamic message) {
  66. _logger.w(message);
  67. }
  68. void e(dynamic message, [dynamic error, StackTrace? stackTrace]) {
  69. _logger.e(message, error, stackTrace);
  70. }
  71. void wtf(dynamic message) {
  72. _logger.wtf(message);
  73. }
  74. }
  75. /// Writes the log output to a file.
  76. class FileOutput extends LogOutput {
  77. final File file;
  78. final bool overrideExisting;
  79. final Encoding encoding;
  80. IOSink? _sink;
  81. FileOutput({
  82. required this.file,
  83. this.overrideExisting = false,
  84. this.encoding = utf8,
  85. });
  86. @override
  87. void init() {
  88. _sink = file.openWrite(
  89. mode: overrideExisting ? FileMode.writeOnly : FileMode.writeOnlyAppend,
  90. encoding: encoding,
  91. );
  92. }
  93. @override
  94. void output(OutputEvent event) {
  95. _sink?.writeAll(event.lines, '\n');
  96. _sink?.writeln();
  97. }
  98. @override
  99. void destroy() async {
  100. await _sink?.flush();
  101. await _sink?.close();
  102. }
  103. }