semantic_logger 5.2.0 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/docs/api.md +160 -9
  3. data/docs/appenders.md +461 -34
  4. data/docs/config.md +543 -36
  5. data/docs/log.md +38 -7
  6. data/docs/metrics.md +139 -28
  7. data/docs/operations.md +293 -24
  8. data/docs/rails.md +319 -5
  9. data/docs/security.md +135 -107
  10. data/docs/testing.md +23 -0
  11. data/docs/upgrading.md +644 -1
  12. data/lib/semantic_logger/appender/async.rb +29 -11
  13. data/lib/semantic_logger/appender/bugsnag.rb +7 -2
  14. data/lib/semantic_logger/appender/cloudwatch_logs.rb +46 -6
  15. data/lib/semantic_logger/appender/elasticsearch_base.rb +197 -29
  16. data/lib/semantic_logger/appender/elasticsearch_http.rb +22 -5
  17. data/lib/semantic_logger/appender/file.rb +29 -14
  18. data/lib/semantic_logger/appender/graylog.rb +81 -7
  19. data/lib/semantic_logger/appender/honeybadger.rb +7 -1
  20. data/lib/semantic_logger/appender/honeybadger_insights.rb +8 -2
  21. data/lib/semantic_logger/appender/http.rb +244 -41
  22. data/lib/semantic_logger/appender/io.rb +11 -6
  23. data/lib/semantic_logger/appender/kafka.rb +192 -46
  24. data/lib/semantic_logger/appender/loki.rb +42 -6
  25. data/lib/semantic_logger/appender/mongodb.rb +94 -9
  26. data/lib/semantic_logger/appender/new_relic.rb +2 -3
  27. data/lib/semantic_logger/appender/new_relic_logs.rb +30 -8
  28. data/lib/semantic_logger/appender/open_telemetry.rb +37 -35
  29. data/lib/semantic_logger/appender/rabbitmq.rb +241 -17
  30. data/lib/semantic_logger/appender/sentry.rb +2 -3
  31. data/lib/semantic_logger/appender/sentry_ruby.rb +82 -15
  32. data/lib/semantic_logger/appender/splunk.rb +4 -4
  33. data/lib/semantic_logger/appender/splunk_http.rb +92 -1
  34. data/lib/semantic_logger/appender/syslog.rb +89 -10
  35. data/lib/semantic_logger/appender/tcp.rb +68 -98
  36. data/lib/semantic_logger/appender/text_output.rb +65 -0
  37. data/lib/semantic_logger/appender/udp.rb +130 -18
  38. data/lib/semantic_logger/appender/wrapper.rb +10 -7
  39. data/lib/semantic_logger/appender.rb +115 -10
  40. data/lib/semantic_logger/appenders.rb +136 -4
  41. data/lib/semantic_logger/backtrace_cleaning.rb +77 -0
  42. data/lib/semantic_logger/base.rb +53 -71
  43. data/lib/semantic_logger/call_site.rb +235 -0
  44. data/lib/semantic_logger/context.rb +222 -0
  45. data/lib/semantic_logger/core_ext/process.rb +20 -5
  46. data/lib/semantic_logger/core_ext/warning.rb +70 -0
  47. data/lib/semantic_logger/deprecation.rb +139 -0
  48. data/lib/semantic_logger/exception_details.rb +4 -3
  49. data/lib/semantic_logger/formatters/base.rb +139 -19
  50. data/lib/semantic_logger/formatters/color.rb +3 -4
  51. data/lib/semantic_logger/formatters/default.rb +35 -7
  52. data/lib/semantic_logger/formatters/ecs.rb +9 -4
  53. data/lib/semantic_logger/formatters/json.rb +9 -0
  54. data/lib/semantic_logger/formatters/logfmt.rb +9 -7
  55. data/lib/semantic_logger/formatters/loki/labels.rb +282 -0
  56. data/lib/semantic_logger/formatters/loki.rb +412 -24
  57. data/lib/semantic_logger/formatters/new_relic_logs.rb +15 -13
  58. data/lib/semantic_logger/formatters/one_line.rb +44 -10
  59. data/lib/semantic_logger/formatters/open_telemetry.rb +186 -29
  60. data/lib/semantic_logger/formatters/pattern.rb +60 -8
  61. data/lib/semantic_logger/formatters/raw.rb +38 -8
  62. data/lib/semantic_logger/formatters/signalfx.rb +55 -15
  63. data/lib/semantic_logger/formatters/syslog.rb +4 -2
  64. data/lib/semantic_logger/formatters/syslog_cee.rb +10 -4
  65. data/lib/semantic_logger/formatters/syslog_header.rb +49 -0
  66. data/lib/semantic_logger/formatters.rb +1 -0
  67. data/lib/semantic_logger/log.rb +236 -80
  68. data/lib/semantic_logger/loggable.rb +55 -95
  69. data/lib/semantic_logger/logger.rb +31 -5
  70. data/lib/semantic_logger/metric/new_relic.rb +32 -17
  71. data/lib/semantic_logger/metric/signalfx.rb +21 -12
  72. data/lib/semantic_logger/metric/statsd.rb +26 -8
  73. data/lib/semantic_logger/processor.rb +194 -5
  74. data/lib/semantic_logger/queue_processor/backoff.rb +140 -0
  75. data/lib/semantic_logger/queue_processor/batch_failure.rb +69 -0
  76. data/lib/semantic_logger/queue_processor/held_write.rb +62 -0
  77. data/lib/semantic_logger/queue_processor/inbox.rb +97 -0
  78. data/lib/semantic_logger/queue_processor/reopener.rb +56 -0
  79. data/lib/semantic_logger/queue_processor/reporter.rb +113 -0
  80. data/lib/semantic_logger/queue_processor/request.rb +48 -10
  81. data/lib/semantic_logger/queue_processor/throttled_counter.rb +60 -17
  82. data/lib/semantic_logger/queue_processor.rb +426 -176
  83. data/lib/semantic_logger/ractor_copy.rb +120 -0
  84. data/lib/semantic_logger/ractor_processor.rb +82 -0
  85. data/lib/semantic_logger/ractor_receiver.rb +282 -0
  86. data/lib/semantic_logger/ractor_support.rb +28 -0
  87. data/lib/semantic_logger/redactor.rb +310 -0
  88. data/lib/semantic_logger/semantic_logger.rb +313 -79
  89. data/lib/semantic_logger/subscriber.rb +169 -15
  90. data/lib/semantic_logger/sync_processor.rb +226 -26
  91. data/lib/semantic_logger/test/capture_log_events.rb +20 -2
  92. data/lib/semantic_logger/test/minitest.rb +2 -1
  93. data/lib/semantic_logger/test/rspec.rb +1 -1
  94. data/lib/semantic_logger/trap_safe.rb +113 -0
  95. data/lib/semantic_logger/utils.rb +266 -79
  96. data/lib/semantic_logger/version.rb +1 -1
  97. data/lib/semantic_logger.rb +35 -2
  98. metadata +22 -2
@@ -2,22 +2,33 @@ require "time"
2
2
  module SemanticLogger
3
3
  module Formatters
4
4
  class Base
5
- attr_accessor :log, :logger, :time_format, :log_host, :log_application, :log_environment, :precision,
5
+ attr_accessor :log, :logger, :log_host, :log_application, :log_environment, :precision,
6
6
  :escape_control_chars, :log_exception_details
7
7
 
8
- # Printable escapes for the most common control characters. Any other
8
+ # Printable escapes for the most common control characters. Any other ASCII
9
9
  # control character is escaped to its hexadecimal `\xHH` form by
10
- # #escape_control_characters.
11
- CONTROL_CHAR_ESCAPES = {
12
- "\n" => "\\n",
13
- "\r" => "\\r",
14
- "\t" => "\\t",
15
- "\e" => "\\e"
16
- }.freeze
10
+ # #escape_control_characters, and a Unicode one (see UNICODE_CONTROL_CHARS)
11
+ # to its `\uXXXX` form.
12
+ CONTROL_CHAR_ESCAPES = Utils.make_shareable(
13
+ {
14
+ "\n" => "\\n",
15
+ "\r" => "\\r",
16
+ "\t" => "\\t",
17
+ "\e" => "\\e"
18
+ }.freeze
19
+ )
17
20
 
18
21
  # Matches C0 control characters (including newlines and the ANSI escape) and DEL.
19
22
  CONTROL_CHARS = /[\x00-\x1f\x7f]/
20
23
 
24
+ # Matches the characters of CONTROL_CHARS, and the Unicode characters that control how text
25
+ # is shown: the C1 control characters (U+0080 to U+009F, including the next line U+0085),
26
+ # which some terminals act on, the line and paragraph separators (U+2028, U+2029), at which
27
+ # editors, browsers, and some log viewers start a new line, and the bidirectional controls
28
+ # (U+202A to U+202E, U+2066 to U+2069), which can make logged text read in a different order
29
+ # than it was written. Escaped in the `\uXXXX` form.
30
+ UNICODE_CONTROL_CHARS = /[\x00-\x1f\x7f\u0080-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]/
31
+
21
32
  # Time precision varies by Ruby interpreter
22
33
  # JRuby 9.1.8.0 supports microseconds
23
34
  PRECISION =
@@ -49,14 +60,17 @@ module SemanticLogger
49
60
  # How many fractional digits to log times with.
50
61
  # Default: PRECISION (6, except on older JRuby, where 3)
51
62
  # escape_control_chars: [Boolean]
52
- # Replace control characters (newlines, the ANSI escape, etc.) in
63
+ # Replace control characters (newlines, the ANSI escape, etc., and the Unicode
64
+ # characters that control how text is shown, such as U+2028 and U+202E) in
53
65
  # untrusted log data (logger name, thread name, message, payload, tags,
54
66
  # named tags, and the message and backtrace lines of the exception and
55
67
  # of each exception that caused it) with a printable escaped form, e.g. "\n".
56
68
  # This prevents log forging and terminal escape-sequence injection
57
69
  # when logging data that may contain attacker-controlled content.
58
70
  # Note: Has no effect on structured formatters such as :json, which
59
- # already escape control characters via JSON encoding.
71
+ # already escape control characters via JSON encoding, except that
72
+ # :loki and :open_telemetry, which write the exception's stack trace
73
+ # as text, escape its backtrace lines and the messages of its causes.
60
74
  # Default: false (preserve newlines and ANSI colors in text output)
61
75
  # log_exception_details: [true|false|:with_source]
62
76
  # Whether to log the detail that Ruby's Exception#detailed_message adds to an
@@ -69,20 +83,60 @@ module SemanticLogger
69
83
  # time it is asked, and logging the details asks for it twice more, so turn this off when
70
84
  # inspecting the receivers of logged exceptions is slow.
71
85
  # Default: true for the Default, Color, and Pattern text formatters, false otherwise.
86
+ # backtrace_cleaner: [true|false|Object]
87
+ # Cleans the backtrace of the logged exception, and of each exception that caused it,
88
+ # before it is written, leaving out or shortening lines. Only this formatter's output
89
+ # changes: the exceptions are not.
90
+ # true: Use the application's cleaner, SemanticLogger.backtrace_cleaner, when one is set.
91
+ # false: Write the full backtrace.
92
+ # Or a cleaner of this formatter's own, which responds to `#clean` or `#call` (see
93
+ # SemanticLogger.backtrace_cleaner=).
94
+ # Default: true for the text formatters (Default and those built on it) and the Json
95
+ # formatters, false otherwise.
72
96
  def initialize(time_format: nil,
73
97
  log_host: true,
74
98
  log_application: true,
75
99
  log_environment: true,
76
100
  precision: PRECISION,
77
101
  escape_control_chars: false,
78
- log_exception_details: default_log_exception_details)
79
- @time_format = time_format || self.class.build_time_format(precision)
102
+ log_exception_details: default_log_exception_details,
103
+ backtrace_cleaner: default_backtrace_cleaner)
104
+ self.time_format = time_format || self.class.build_time_format(precision)
80
105
  @log_host = log_host
81
106
  @log_application = log_application
82
107
  @log_environment = log_environment
83
108
  @precision = precision
84
109
  @escape_control_chars = escape_control_chars
85
110
  @log_exception_details = log_exception_details
111
+ self.backtrace_cleaner = backtrace_cleaner
112
+ end
113
+
114
+ # Returns [String|Symbol] the `time_format` option. See #initialize.
115
+ attr_reader :time_format
116
+
117
+ # Sets the `time_format` option. See #initialize.
118
+ #
119
+ # A format String is converted once, here, as #joinable_text converts the formatter's own
120
+ # text: a format with characters outside ASCII in another encoding, such as ISO-8859-1, could
121
+ # not otherwise be joined with the entry's other text.
122
+ def time_format=(time_format)
123
+ @time_format = joinable_text(time_format)
124
+ end
125
+
126
+ # Returns [true|false|Object] the `backtrace_cleaner` option. See #initialize.
127
+ attr_reader :backtrace_cleaner
128
+
129
+ # Sets the `backtrace_cleaner` option. See #initialize.
130
+ # Raises ArgumentError for any other value, so that a mistake surfaces here rather than on
131
+ # every log entry.
132
+ #
133
+ # Compared by identity, since a cleaner, such as a test double, need not define #== or #nil?.
134
+ def backtrace_cleaner=(cleaner)
135
+ unless true.equal?(cleaner) || false.equal?(cleaner) || nil.equal?(cleaner)
136
+ BacktraceCleaning.validate!(cleaner, expected: "true, false, or a backtrace cleaner")
137
+ end
138
+
139
+ @backtrace_cleaner = cleaner
86
140
  end
87
141
 
88
142
  # Return default time format string
@@ -110,7 +164,7 @@ module SemanticLogger
110
164
  # When `escape_control_chars` is enabled, return a copy of the supplied
111
165
  # value with any control characters replaced by a printable escaped form
112
166
  # so that untrusted log data cannot forge log entries or inject terminal
113
- # escape sequences. Otherwise the value is returned unchanged.
167
+ # escape sequences.
114
168
  #
115
169
  # Note: This escapes (preserves) control characters, and is opt-in. It is
116
170
  # distinct from Log#cleansed_message, which unconditionally *strips* ANSI
@@ -120,11 +174,40 @@ module SemanticLogger
120
174
  # untrusted binary input, or in another encoding, is first converted to valid UTF-8
121
175
  # (dropping the invalid bytes), since matching against it would raise and the log entry
122
176
  # would be lost.
177
+ #
178
+ # Otherwise the value is returned unchanged, unless it is a String that cannot be joined with
179
+ # UTF-8 text: one that holds bytes outside ASCII in another encoding, such as a binary
180
+ # response body or ISO-8859-1 text, or that is not ASCII-compatible, such as UTF-16. Joining
181
+ # it with the entry's other text, such as an accented logger name, would raise and lose the
182
+ # entry, so it is converted to valid UTF-8 (see Utils.encode_utf8_string), and this
183
+ # formatter's output is UTF-8. A String tagged UTF-8 that holds invalid bytes joins with
184
+ # UTF-8 text, so it is still written as it is.
123
185
  def escape_control_characters(value)
124
- return value unless escape_control_chars && value
186
+ unless escape_control_chars
187
+ # As #joinable_text, written out here since every field of every entry passes through,
188
+ # and almost every one is UTF-8 or ASCII.
189
+ return value unless value.is_a?(String) && value.encoding != Encoding::UTF_8 && !value.ascii_only?
190
+
191
+ return Utils.encode_utf8_string(value)
192
+ end
193
+ return value unless value
125
194
 
126
195
  string = Utils.encode_utf8_string(value.to_s)
127
- string.gsub(CONTROL_CHARS) { |char| CONTROL_CHAR_ESCAPES[char] || format("\\x%02x", char.ord) }
196
+ # Most text is ASCII, which only the ASCII control characters need to be looked for in.
197
+ string.gsub(string.ascii_only? ? CONTROL_CHARS : UNICODE_CONTROL_CHARS) do |char|
198
+ CONTROL_CHAR_ESCAPES[char] || format(char.ord < 0x80 ? "\\x%02x" : "\\u%04x", char.ord)
199
+ end
200
+ end
201
+
202
+ # Returns the value as text that can be joined with this formatter's UTF-8 output: a String
203
+ # that holds bytes outside ASCII in another encoding converted to UTF-8 (see
204
+ # Utils.encode_utf8_string), and anything else as it is, including a UTF-8 String that holds
205
+ # invalid bytes. For the formatter's own text, such as its time format, which is not escaped.
206
+ # #escape_control_characters applies the same rule to the entry's fields.
207
+ def joinable_text(value)
208
+ return value unless value.is_a?(String) && value.encoding != Encoding::UTF_8 && !value.ascii_only?
209
+
210
+ Utils.encode_utf8_string(value)
128
211
  end
129
212
 
130
213
  # Whether this formatter logs exception details unless told otherwise. See #initialize.
@@ -132,6 +215,24 @@ module SemanticLogger
132
215
  false
133
216
  end
134
217
 
218
+ # Whether this formatter uses the application's backtrace cleaner unless told otherwise.
219
+ # See #initialize.
220
+ def default_backtrace_cleaner
221
+ false
222
+ end
223
+
224
+ # Returns the backtrace cleaner to use for this log entry, or nil when backtraces are written
225
+ # in full.
226
+ #
227
+ # The application's cleaner is looked up for each entry, rather than when this formatter is
228
+ # built, so that it can be set after the appenders are added.
229
+ def active_backtrace_cleaner
230
+ cleaner = backtrace_cleaner
231
+ return SemanticLogger.backtrace_cleaner if true.equal?(cleaner)
232
+
233
+ cleaner || nil
234
+ end
235
+
135
236
  # Returns [String] the detail Ruby adds to the message of the supplied exception (see
136
237
  # Log#exception_details), or nil when there is none or this formatter does not log it.
137
238
  def exception_details_for(exception)
@@ -141,16 +242,35 @@ module SemanticLogger
141
242
  end
142
243
 
143
244
  # Returns [String] the backtrace of the logged exception, followed by each exception
144
- # that caused it, as laid out by Log#backtrace_to_s.
245
+ # that caused it, as laid out by Log#backtrace_to_s, and cleaned by the backtrace cleaner
246
+ # (see #initialize).
145
247
  #
146
248
  # When `escape_control_chars` is enabled, the message of each cause and each backtrace
147
249
  # line is passed through #escape_control_characters, the same as the message of the
148
250
  # logged exception: causes are often parser or validation errors that quote untrusted
149
251
  # input, and a backtrace can be set from untrusted data.
252
+ #
253
+ # Otherwise they are written as they are, unless the text cannot be joined with UTF-8 text,
254
+ # such as when a cause's message is ISO-8859-1 text: then each is converted as
255
+ # #escape_control_characters converts the other fields.
150
256
  def exception_backtrace
151
- return log.backtrace_to_s unless escape_control_chars
257
+ cleaner = active_backtrace_cleaner
258
+ unless escape_control_chars
259
+ text = joinable_backtrace(cleaner)
260
+ return text if text
261
+ end
152
262
 
153
- log.backtrace_to_s { |text| escape_control_characters(text) }
263
+ log.backtrace_to_s(cleaner: cleaner) { |text| escape_control_characters(text) }
264
+ end
265
+
266
+ # Returns [String] the backtrace as Log#backtrace_to_s writes it, or nil when that text
267
+ # cannot be joined with UTF-8 text (see #escape_control_characters).
268
+ #
269
+ # The text is checked once, rather than each of what is usually dozens of lines, which
270
+ # would double the time it takes to format an exception.
271
+ def joinable_backtrace(cleaner)
272
+ text = log.backtrace_to_s(cleaner: cleaner)
273
+ text if text.encoding == Encoding::UTF_8 || text.ascii_only?
154
274
  end
155
275
 
156
276
  # Return the Time as a formatted string
@@ -130,10 +130,9 @@ module SemanticLogger
130
130
  def exception
131
131
  return unless log.exception
132
132
 
133
- details = exception_details
134
- "-- Exception: #{color}#{log.exception.class}: " \
135
- "#{escape_control_characters(log.exception.message)}#{color_map.clear}\n" \
136
- "#{"#{details}\n" if details}#{exception_backtrace}"
133
+ name = Utils.exception_name(log.exception)
134
+ message = escape_control_characters(Utils.exception_message(log.exception))
135
+ "-- Exception: #{color}#{name}: #{message}#{color_map.clear}#{exception_detail_lines}"
137
136
  end
138
137
 
139
138
  def call(log, logger)
@@ -10,18 +10,29 @@ module SemanticLogger
10
10
  log.level_to_s
11
11
  end
12
12
 
13
- # Name of the thread that logged the message.
13
+ # Name of the thread that logged the message, shortened before it is escaped, so that an
14
+ # escape is never cut in two.
14
15
  def thread_name
15
- escape_control_characters(format("%.30s", log.thread_name))
16
+ escape_control_characters(shortened_thread_name)
16
17
  end
17
18
 
18
- # Ruby file name and line number that logged the message.
19
+ # Ruby file name and line number that logged the message, followed by the method when the
20
+ # entry has a call site (see SemanticLogger.call_site_level), such as "order.rb:42 Order#submit".
19
21
  #
20
22
  # The file name is escaped like the other fields, since it comes from the first line of the
21
23
  # backtrace, which a log call can supply from untrusted data.
22
24
  def file_name_and_line
23
25
  file, line = log.file_name_and_line(true)
24
- "#{escape_control_characters(file)}:#{line}" if file
26
+ return unless file
27
+
28
+ label = method_name
29
+ label ? "#{escape_control_characters(file)}:#{line} #{label}" : "#{escape_control_characters(file)}:#{line}"
30
+ end
31
+
32
+ # The method that logged the message, such as "Order#submit", when the entry has a call site.
33
+ # Escaped like the file name, which it comes from.
34
+ def method_name
35
+ escape_control_characters(log.call_site&.label)
25
36
  end
26
37
 
27
38
  # Returns [String] the available process info
@@ -79,9 +90,8 @@ module SemanticLogger
79
90
  def exception
80
91
  return unless log.exception
81
92
 
82
- details = exception_details
83
- "-- Exception: #{log.exception.class}: #{escape_control_characters(log.exception.message)}\n" \
84
- "#{"#{details}\n" if details}#{exception_backtrace}"
93
+ message = escape_control_characters(Utils.exception_message(log.exception))
94
+ "-- Exception: #{Utils.exception_name(log.exception)}: #{message}#{exception_detail_lines}"
85
95
  end
86
96
 
87
97
  # The detail Ruby adds to the exception's message, such as did_you_mean suggestions.
@@ -102,10 +112,28 @@ module SemanticLogger
102
112
 
103
113
  private
104
114
 
115
+ # Returns [String] the name of the thread, shortened to 30 characters. A name that holds bytes
116
+ # outside ASCII in another encoding, such as a binary name that holds UTF-8, is converted
117
+ # first (see #joinable_text), so that it is shortened by characters, rather than by bytes,
118
+ # which would split a character in two.
119
+ def shortened_thread_name = format("%.30s", joinable_text(log.thread_name))
120
+
121
+ # Returns [String] the exception details and backtrace that follow the exception line, each
122
+ # starting on a new line. Empty when there are neither, such as for an exception that was
123
+ # never raised, so that no blank line follows the exception.
124
+ def exception_detail_lines
125
+ [exception_details, exception_backtrace].filter_map { |text| "\n#{text}" unless text.nil? || text.empty? }.join
126
+ end
127
+
105
128
  # Text output is read by people, who benefit most from exception details.
106
129
  def default_log_exception_details
107
130
  true
108
131
  end
132
+
133
+ # Text output is read by people, who are best served by the frames of their own code.
134
+ def default_backtrace_cleaner
135
+ true
136
+ end
109
137
  end
110
138
  end
111
139
  end
@@ -22,6 +22,7 @@ module SemanticLogger
22
22
  # level -> log.level
23
23
  # name -> log.logger
24
24
  # file_name / line -> log.origin.file.name / log.origin.file.line
25
+ # method -> log.origin.function
25
26
  # message -> message
26
27
  # thread_name -> process.thread.name
27
28
  # pid -> process.pid
@@ -34,6 +35,7 @@ module SemanticLogger
34
35
  # named_tags -> labels.* (scalar key/value pairs)
35
36
  # payload -> <namespace>.* (or labels.* when namespace is nil)
36
37
  # metric/metric_amount -> <namespace>.metric / <namespace>.metric_amount
38
+ # dimensions -> <namespace>.dimensions
37
39
  #
38
40
  # == Reference
39
41
  # * https://www.elastic.co/docs/reference/ecs
@@ -44,7 +46,7 @@ module SemanticLogger
44
46
 
45
47
  # namespace: [String|Symbol|nil]
46
48
  # Top-level field set used to hold SemanticLogger-specific data that has
47
- # no native ECS home (payload, metric, metric_amount). A proper-noun
49
+ # no native ECS home (payload, metric, metric_amount, dimensions). A proper-noun
48
50
  # namespace is guaranteed never to collide with a current or future ECS
49
51
  # field. Set to nil to merge the payload into ECS `labels` instead.
50
52
  # Default: "semantic_logger"
@@ -110,7 +112,9 @@ module SemanticLogger
110
112
  def add_origin(result, hash)
111
113
  return unless hash[:file]
112
114
 
113
- result[:log][:origin] = {file: {name: hash[:file], line: hash[:line]}.compact}
115
+ origin = {file: {name: hash[:file], line: hash[:line]}.compact}
116
+ origin[:function] = hash[:method] if hash[:method]
117
+ result[:log][:origin] = origin
114
118
  end
115
119
 
116
120
  # ECS event.duration is measured in nanoseconds.
@@ -126,11 +130,11 @@ module SemanticLogger
126
130
  result[:error] = {
127
131
  type: hash[:exception][:name],
128
132
  message: hash[:exception][:message],
129
- stack_trace: Array(hash[:exception][:stack_trace]).join("\n")
133
+ stack_trace: stack_trace(Array(hash[:exception][:stack_trace]))
130
134
  }
131
135
  end
132
136
 
133
- # Place SemanticLogger-specific data (payload, metric) that has no native
137
+ # Place SemanticLogger-specific data (payload, metric, dimensions) that has no native
134
138
  # ECS home into the configured namespace, plus named_tags into labels.
135
139
  def add_extras(result, hash)
136
140
  labels = hash[:named_tags].is_a?(Hash) ? hash[:named_tags].dup : {}
@@ -139,6 +143,7 @@ module SemanticLogger
139
143
  extra[:payload] = hash[:payload] if hash[:payload]
140
144
  extra[:metric] = hash[:metric] if hash[:metric]
141
145
  extra[:metric_amount] = hash[:metric_amount] if hash[:metric_amount]
146
+ extra[:dimensions] = hash[:dimensions] if hash[:dimensions]
142
147
 
143
148
  unless extra.empty?
144
149
  namespace ? result[namespace] = extra : labels.merge!(extra)
@@ -16,6 +16,15 @@ module SemanticLogger
16
16
  def batch(logs, logger)
17
17
  "[#{logs.map { |log| call(log, logger) }.join(',')}]"
18
18
  end
19
+
20
+ private
21
+
22
+ # Cleans backtraces with the application's cleaner, as the text formatters do. Error services
23
+ # receive the exception itself rather than this output, and the formatters written for a
24
+ # particular service, such as Ecs, are not built on this one.
25
+ def default_backtrace_cleaner
26
+ true
27
+ end
19
28
  end
20
29
  end
21
30
  end
@@ -37,13 +37,15 @@ module SemanticLogger
37
37
 
38
38
  # The exception's fields in the Raw hash, mapped to their logfmt names, so that the exception's
39
39
  # name and message do not replace the entry's own.
40
- EXCEPTION_FIELDS = {
41
- name: "exception_name",
42
- message: "exception_message",
43
- details: "exception_details",
44
- stack_trace: "stack_trace",
45
- cause: "cause"
46
- }.freeze
40
+ EXCEPTION_FIELDS = Utils.make_shareable(
41
+ {
42
+ name: "exception_name",
43
+ message: "exception_message",
44
+ details: "exception_details",
45
+ stack_trace: "stack_trace",
46
+ cause: "cause"
47
+ }.freeze
48
+ )
47
49
 
48
50
  # Prefixes that mark a key from logged data with its source, when the key alone would name
49
51
  # one of the entry's own fields, or look like a key marked this way.