|
| 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 | + return _format( |
| 17 | + templateBody, |
| 18 | + arguments, |
| 19 | + attributes, |
| 20 | + (formattedBody, allAttributes) { |
| 21 | + return _logger.trace(formattedBody, attributes: allAttributes); |
| 22 | + }, |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + FutureOr<void> debug( |
| 27 | + String templateBody, |
| 28 | + List<dynamic> arguments, { |
| 29 | + Map<String, SentryLogAttribute>? attributes, |
| 30 | + }) { |
| 31 | + return _format( |
| 32 | + templateBody, |
| 33 | + arguments, |
| 34 | + attributes, |
| 35 | + (formattedBody, allAttributes) { |
| 36 | + return _logger.debug(formattedBody, attributes: allAttributes); |
| 37 | + }, |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + FutureOr<void> info( |
| 42 | + String templateBody, |
| 43 | + List<dynamic> arguments, { |
| 44 | + Map<String, SentryLogAttribute>? attributes, |
| 45 | + }) { |
| 46 | + return _format( |
| 47 | + templateBody, |
| 48 | + arguments, |
| 49 | + attributes, |
| 50 | + (formattedBody, allAttributes) { |
| 51 | + return _logger.info(formattedBody, attributes: allAttributes); |
| 52 | + }, |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + FutureOr<void> warn( |
| 57 | + String templateBody, |
| 58 | + List<dynamic> arguments, { |
| 59 | + Map<String, SentryLogAttribute>? attributes, |
| 60 | + }) { |
| 61 | + return _format( |
| 62 | + templateBody, |
| 63 | + arguments, |
| 64 | + attributes, |
| 65 | + (formattedBody, allAttributes) { |
| 66 | + return _logger.warn(formattedBody, attributes: allAttributes); |
| 67 | + }, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + FutureOr<void> error( |
| 72 | + String templateBody, |
| 73 | + List<dynamic> arguments, { |
| 74 | + Map<String, SentryLogAttribute>? attributes, |
| 75 | + }) { |
| 76 | + return _format( |
| 77 | + templateBody, |
| 78 | + arguments, |
| 79 | + attributes, |
| 80 | + (formattedBody, allAttributes) { |
| 81 | + return _logger.error(formattedBody, attributes: allAttributes); |
| 82 | + }, |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + FutureOr<void> fatal( |
| 87 | + String templateBody, |
| 88 | + List<dynamic> arguments, { |
| 89 | + Map<String, SentryLogAttribute>? attributes, |
| 90 | + }) { |
| 91 | + return _format( |
| 92 | + templateBody, |
| 93 | + arguments, |
| 94 | + attributes, |
| 95 | + (formattedBody, allAttributes) { |
| 96 | + return _logger.fatal(formattedBody, attributes: allAttributes); |
| 97 | + }, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + // Helper |
| 102 | + |
| 103 | + FutureOr<void> _format( |
| 104 | + String templateBody, |
| 105 | + List<dynamic> arguments, |
| 106 | + Map<String, SentryLogAttribute>? attributes, |
| 107 | + FutureOr<void> Function(String, Map<String, SentryLogAttribute>) callback, |
| 108 | + ) { |
| 109 | + final templateString = SentryTemplateString(templateBody, arguments); |
| 110 | + final formattedBody = templateString.format(); |
| 111 | + final templateAttributes = _getAllAttributes(templateBody, arguments); |
| 112 | + if (attributes != null) { |
| 113 | + templateAttributes.addAll(attributes); |
| 114 | + } |
| 115 | + return callback(formattedBody, templateAttributes); |
| 116 | + } |
| 117 | + |
| 118 | + Map<String, SentryLogAttribute> _getAllAttributes( |
| 119 | + String templateBody, |
| 120 | + List<dynamic> args, |
| 121 | + ) { |
| 122 | + final templateAttributes = { |
| 123 | + 'sentry.message.template': SentryLogAttribute.string(templateBody), |
| 124 | + }; |
| 125 | + for (var i = 0; i < args.length; i++) { |
| 126 | + final argument = args[i]; |
| 127 | + final key = 'sentry.message.parameter.$i'; |
| 128 | + if (argument is String) { |
| 129 | + templateAttributes[key] = SentryLogAttribute.string(argument); |
| 130 | + } else if (argument is int) { |
| 131 | + templateAttributes[key] = SentryLogAttribute.int(argument); |
| 132 | + } else if (argument is bool) { |
| 133 | + templateAttributes[key] = SentryLogAttribute.bool(argument); |
| 134 | + } else if (argument is double) { |
| 135 | + templateAttributes[key] = SentryLogAttribute.double(argument); |
| 136 | + } else { |
| 137 | + try { |
| 138 | + templateAttributes[key] = |
| 139 | + SentryLogAttribute.string(argument.toString()); |
| 140 | + } catch (e) { |
| 141 | + templateAttributes[key] = SentryLogAttribute.string(""); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + return templateAttributes; |
| 146 | + } |
| 147 | +} |
0 commit comments