|
| 1 | +import 'dart:async'; |
| 2 | +import 'protocol/sentry_log_attribute.dart'; |
| 3 | +import 'sentry_template_string.dart'; |
| 4 | +import 'sentry_logger.dart'; |
| 5 | + |
| 6 | +class SentryLoggerFormatter { |
| 7 | + SentryLoggerFormatter(this._logger); |
| 8 | + |
| 9 | + final SentryLogger _logger; |
| 10 | + |
| 11 | + FutureOr<void> trace( |
| 12 | + String templateBody, |
| 13 | + List<dynamic> arguments, { |
| 14 | + Map<String, SentryLogAttribute>? attributes, |
| 15 | + }) { |
| 16 | + final templateString = SentryTemplateString(templateBody); |
| 17 | + final interpolatedBody = templateString.format(arguments); |
| 18 | + final allAttributes = _getTemplateAttributes(templateBody, arguments); |
| 19 | + if (attributes != null) { |
| 20 | + allAttributes.addAll(attributes); |
| 21 | + } |
| 22 | + return _logger.trace(interpolatedBody, attributes: allAttributes); |
| 23 | + } |
| 24 | + |
| 25 | + FutureOr<void> debug( |
| 26 | + String templateBody, |
| 27 | + List<dynamic> arguments, { |
| 28 | + Map<String, SentryLogAttribute>? attributes, |
| 29 | + }) { |
| 30 | + final templateString = SentryTemplateString(templateBody); |
| 31 | + final interpolatedBody = templateString.format(arguments); |
| 32 | + final allAttributes = _getTemplateAttributes(templateBody, arguments); |
| 33 | + if (attributes != null) { |
| 34 | + allAttributes.addAll(attributes); |
| 35 | + } |
| 36 | + return _logger.debug(interpolatedBody, attributes: allAttributes); |
| 37 | + } |
| 38 | + |
| 39 | + FutureOr<void> info( |
| 40 | + String templateBody, |
| 41 | + List<dynamic> arguments, { |
| 42 | + Map<String, SentryLogAttribute>? attributes, |
| 43 | + }) { |
| 44 | + final templateString = SentryTemplateString(templateBody); |
| 45 | + final interpolatedBody = templateString.format(arguments); |
| 46 | + final allAttributes = _getTemplateAttributes(templateBody, arguments); |
| 47 | + if (attributes != null) { |
| 48 | + allAttributes.addAll(attributes); |
| 49 | + } |
| 50 | + return _logger.info(interpolatedBody, attributes: allAttributes); |
| 51 | + } |
| 52 | + |
| 53 | + FutureOr<void> warn( |
| 54 | + String templateBody, |
| 55 | + List<dynamic> arguments, { |
| 56 | + Map<String, SentryLogAttribute>? attributes, |
| 57 | + }) { |
| 58 | + final templateString = SentryTemplateString(templateBody); |
| 59 | + final interpolatedBody = templateString.format(arguments); |
| 60 | + final allAttributes = _getTemplateAttributes(templateBody, arguments); |
| 61 | + if (attributes != null) { |
| 62 | + allAttributes.addAll(attributes); |
| 63 | + } |
| 64 | + return _logger.warn(interpolatedBody, attributes: allAttributes); |
| 65 | + } |
| 66 | + |
| 67 | + FutureOr<void> error( |
| 68 | + String templateBody, |
| 69 | + List<dynamic> arguments, { |
| 70 | + Map<String, SentryLogAttribute>? attributes, |
| 71 | + }) { |
| 72 | + final templateString = SentryTemplateString(templateBody); |
| 73 | + final interpolatedBody = templateString.format(arguments); |
| 74 | + final allAttributes = _getTemplateAttributes(templateBody, arguments); |
| 75 | + if (attributes != null) { |
| 76 | + allAttributes.addAll(attributes); |
| 77 | + } |
| 78 | + return _logger.error(interpolatedBody, attributes: allAttributes); |
| 79 | + } |
| 80 | + |
| 81 | + FutureOr<void> fatal( |
| 82 | + String templateBody, |
| 83 | + List<dynamic> arguments, { |
| 84 | + Map<String, SentryLogAttribute>? attributes, |
| 85 | + }) { |
| 86 | + final templateString = SentryTemplateString(templateBody); |
| 87 | + final interpolatedBody = templateString.format(arguments); |
| 88 | + final allAttributes = _getTemplateAttributes(templateBody, arguments); |
| 89 | + if (attributes != null) { |
| 90 | + allAttributes.addAll(attributes); |
| 91 | + } |
| 92 | + return _logger.fatal(interpolatedBody, attributes: allAttributes); |
| 93 | + } |
| 94 | + |
| 95 | + // Helper |
| 96 | + |
| 97 | + Map<String, SentryLogAttribute> _getTemplateAttributes( |
| 98 | + String templateBody, List<dynamic> args) { |
| 99 | + final templateAttributes = { |
| 100 | + 'sentry.message.template': SentryLogAttribute.string(templateBody), |
| 101 | + }; |
| 102 | + for (var i = 0; i < args.length; i++) { |
| 103 | + final argument = args[i]; |
| 104 | + final key = 'sentry.message.parameter.$i'; |
| 105 | + if (argument is String) { |
| 106 | + templateAttributes[key] = SentryLogAttribute.string(argument); |
| 107 | + } else if (argument is int) { |
| 108 | + templateAttributes[key] = SentryLogAttribute.int(argument); |
| 109 | + } else if (argument is bool) { |
| 110 | + templateAttributes[key] = SentryLogAttribute.bool(argument); |
| 111 | + } else if (argument is double) { |
| 112 | + templateAttributes[key] = SentryLogAttribute.double(argument); |
| 113 | + } else { |
| 114 | + try { |
| 115 | + templateAttributes[key] = |
| 116 | + SentryLogAttribute.string(argument.toString()); |
| 117 | + } catch (e) { |
| 118 | + templateAttributes[key] = SentryLogAttribute.string(""); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return templateAttributes; |
| 124 | + } |
| 125 | +} |
0 commit comments