semantic_logger 5.1.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 (103) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -12
  3. data/Rakefile +15 -4
  4. data/docs/api.md +196 -19
  5. data/docs/appenders.md +579 -68
  6. data/docs/config.md +647 -38
  7. data/docs/index.md +31 -5
  8. data/docs/log.md +56 -17
  9. data/docs/metrics.md +144 -30
  10. data/docs/operations.md +339 -24
  11. data/docs/rails.md +396 -8
  12. data/docs/security.md +222 -27
  13. data/docs/testing.md +28 -3
  14. data/docs/upgrading.md +938 -7
  15. data/lib/semantic_logger/appender/async.rb +42 -11
  16. data/lib/semantic_logger/appender/bugsnag.rb +7 -2
  17. data/lib/semantic_logger/appender/cloudwatch_logs.rb +168 -33
  18. data/lib/semantic_logger/appender/elasticsearch_base.rb +221 -28
  19. data/lib/semantic_logger/appender/elasticsearch_http.rb +23 -6
  20. data/lib/semantic_logger/appender/file.rb +160 -37
  21. data/lib/semantic_logger/appender/graylog.rb +101 -11
  22. data/lib/semantic_logger/appender/honeybadger.rb +7 -1
  23. data/lib/semantic_logger/appender/honeybadger_insights.rb +8 -2
  24. data/lib/semantic_logger/appender/http.rb +308 -46
  25. data/lib/semantic_logger/appender/io.rb +11 -6
  26. data/lib/semantic_logger/appender/kafka.rb +204 -44
  27. data/lib/semantic_logger/appender/loki.rb +55 -9
  28. data/lib/semantic_logger/appender/mongodb.rb +109 -5
  29. data/lib/semantic_logger/appender/new_relic.rb +3 -4
  30. data/lib/semantic_logger/appender/new_relic_logs.rb +35 -15
  31. data/lib/semantic_logger/appender/open_telemetry.rb +37 -35
  32. data/lib/semantic_logger/appender/rabbitmq.rb +246 -17
  33. data/lib/semantic_logger/appender/sentry.rb +3 -4
  34. data/lib/semantic_logger/appender/sentry_ruby.rb +159 -18
  35. data/lib/semantic_logger/appender/splunk.rb +11 -2
  36. data/lib/semantic_logger/appender/splunk_http.rb +92 -1
  37. data/lib/semantic_logger/appender/syslog.rb +149 -32
  38. data/lib/semantic_logger/appender/tcp.rb +73 -98
  39. data/lib/semantic_logger/appender/text_output.rb +65 -0
  40. data/lib/semantic_logger/appender/udp.rb +140 -18
  41. data/lib/semantic_logger/appender/wrapper.rb +10 -7
  42. data/lib/semantic_logger/appender.rb +115 -10
  43. data/lib/semantic_logger/appenders.rb +145 -5
  44. data/lib/semantic_logger/backtrace_cleaning.rb +77 -0
  45. data/lib/semantic_logger/base.rb +115 -81
  46. data/lib/semantic_logger/call_site.rb +235 -0
  47. data/lib/semantic_logger/context.rb +222 -0
  48. data/lib/semantic_logger/core_ext/process.rb +20 -5
  49. data/lib/semantic_logger/core_ext/warning.rb +70 -0
  50. data/lib/semantic_logger/deprecation.rb +139 -0
  51. data/lib/semantic_logger/exception_details.rb +51 -0
  52. data/lib/semantic_logger/formatters/base.rb +186 -20
  53. data/lib/semantic_logger/formatters/color.rb +18 -3
  54. data/lib/semantic_logger/formatters/default.rb +53 -8
  55. data/lib/semantic_logger/formatters/ecs.rb +9 -4
  56. data/lib/semantic_logger/formatters/json.rb +9 -0
  57. data/lib/semantic_logger/formatters/logfmt.rb +155 -21
  58. data/lib/semantic_logger/formatters/loki/labels.rb +282 -0
  59. data/lib/semantic_logger/formatters/loki.rb +430 -38
  60. data/lib/semantic_logger/formatters/new_relic_logs.rb +15 -13
  61. data/lib/semantic_logger/formatters/one_line.rb +101 -3
  62. data/lib/semantic_logger/formatters/open_telemetry.rb +191 -35
  63. data/lib/semantic_logger/formatters/pattern.rb +68 -12
  64. data/lib/semantic_logger/formatters/raw.rb +40 -11
  65. data/lib/semantic_logger/formatters/signalfx.rb +55 -15
  66. data/lib/semantic_logger/formatters/syslog.rb +114 -20
  67. data/lib/semantic_logger/formatters/syslog_cee.rb +38 -19
  68. data/lib/semantic_logger/formatters/syslog_header.rb +49 -0
  69. data/lib/semantic_logger/formatters.rb +1 -0
  70. data/lib/semantic_logger/levels.rb +10 -1
  71. data/lib/semantic_logger/log.rb +384 -82
  72. data/lib/semantic_logger/loggable.rb +56 -73
  73. data/lib/semantic_logger/logger.rb +31 -5
  74. data/lib/semantic_logger/metric/new_relic.rb +33 -18
  75. data/lib/semantic_logger/metric/signalfx.rb +29 -18
  76. data/lib/semantic_logger/metric/statsd.rb +41 -15
  77. data/lib/semantic_logger/processor.rb +194 -5
  78. data/lib/semantic_logger/queue_processor/backoff.rb +140 -0
  79. data/lib/semantic_logger/queue_processor/batch_failure.rb +69 -0
  80. data/lib/semantic_logger/queue_processor/held_write.rb +62 -0
  81. data/lib/semantic_logger/queue_processor/inbox.rb +97 -0
  82. data/lib/semantic_logger/queue_processor/reopener.rb +56 -0
  83. data/lib/semantic_logger/queue_processor/reporter.rb +113 -0
  84. data/lib/semantic_logger/queue_processor/request.rb +93 -0
  85. data/lib/semantic_logger/queue_processor/throttled_counter.rb +89 -0
  86. data/lib/semantic_logger/queue_processor.rb +519 -170
  87. data/lib/semantic_logger/ractor_copy.rb +120 -0
  88. data/lib/semantic_logger/ractor_processor.rb +82 -0
  89. data/lib/semantic_logger/ractor_receiver.rb +282 -0
  90. data/lib/semantic_logger/ractor_support.rb +28 -0
  91. data/lib/semantic_logger/redactor.rb +310 -0
  92. data/lib/semantic_logger/semantic_logger.rb +328 -81
  93. data/lib/semantic_logger/subscriber.rb +250 -10
  94. data/lib/semantic_logger/sync_processor.rb +237 -26
  95. data/lib/semantic_logger/test/capture_log_events.rb +26 -5
  96. data/lib/semantic_logger/test/minitest.rb +2 -1
  97. data/lib/semantic_logger/test/rspec.rb +1 -1
  98. data/lib/semantic_logger/trap_safe.rb +113 -0
  99. data/lib/semantic_logger/utils.rb +482 -67
  100. data/lib/semantic_logger/version.rb +1 -1
  101. data/lib/semantic_logger.rb +36 -3
  102. metadata +41 -5
  103. data/lib/semantic_logger/core_ext/thread.rb +0 -30
@@ -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,
6
- :escape_control_chars
5
+ attr_accessor :log, :logger, :log_host, :log_application, :log_environment, :precision,
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,26 +60,83 @@ 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
53
- # untrusted log data (message, tags, named tags, and exception
54
- # message) with a printable escaped form, e.g. "\n".
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
65
+ # untrusted log data (logger name, thread name, message, payload, tags,
66
+ # named tags, and the message and backtrace lines of the exception and
67
+ # of each exception that caused it) with a printable escaped form, e.g. "\n".
55
68
  # This prevents log forging and terminal escape-sequence injection
56
69
  # when logging data that may contain attacker-controlled content.
57
70
  # Note: Has no effect on structured formatters such as :json, which
58
- # 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.
59
74
  # Default: false (preserve newlines and ANSI colors in text output)
75
+ # log_exception_details: [true|false|:with_source]
76
+ # Whether to log the detail that Ruby's Exception#detailed_message adds to an
77
+ # exception's message, such as did_you_mean suggestions ("Did you mean? upcase") or
78
+ # the detail an exception class supplies by overriding #detailed_message.
79
+ # :with_source also logs error_highlight's snippet of the failing source line. It is
80
+ # about 100 times slower, since it re-parses that source file, and it copies source
81
+ # code into the log.
82
+ # On Ruby 3.2 a NameError or NoMethodError builds its message from receiver.inspect each
83
+ # time it is asked, and logging the details asks for it twice more, so turn this off when
84
+ # inspecting the receivers of logged exceptions is slow.
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.
60
96
  def initialize(time_format: nil,
61
97
  log_host: true,
62
98
  log_application: true,
63
99
  log_environment: true,
64
100
  precision: PRECISION,
65
- escape_control_chars: false)
66
- @time_format = time_format || self.class.build_time_format(precision)
101
+ escape_control_chars: false,
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)
67
105
  @log_host = log_host
68
106
  @log_application = log_application
69
107
  @log_environment = log_environment
70
108
  @precision = precision
71
- @escape_control_chars = escape_control_chars
109
+ @escape_control_chars = escape_control_chars
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
72
140
  end
73
141
 
74
142
  # Return default time format string
@@ -96,15 +164,113 @@ module SemanticLogger
96
164
  # When `escape_control_chars` is enabled, return a copy of the supplied
97
165
  # value with any control characters replaced by a printable escaped form
98
166
  # so that untrusted log data cannot forge log entries or inject terminal
99
- # escape sequences. Otherwise the value is returned unchanged.
167
+ # escape sequences.
100
168
  #
101
169
  # Note: This escapes (preserves) control characters, and is opt-in. It is
102
170
  # distinct from Log#cleansed_message, which unconditionally *strips* ANSI
103
171
  # colorization from the message for structured (JSON/Loki) output.
172
+ #
173
+ # A string with invalid byte sequences, for example an exception message that quotes
174
+ # untrusted binary input, or in another encoding, is first converted to valid UTF-8
175
+ # (dropping the invalid bytes), since matching against it would raise and the log entry
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.
104
185
  def escape_control_characters(value)
105
- 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
106
194
 
107
- value.to_s.gsub(CONTROL_CHARS) { |char| CONTROL_CHAR_ESCAPES[char] || format("\\x%02x", char.ord) }
195
+ string = Utils.encode_utf8_string(value.to_s)
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)
211
+ end
212
+
213
+ # Whether this formatter logs exception details unless told otherwise. See #initialize.
214
+ def default_log_exception_details
215
+ false
216
+ end
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
+
236
+ # Returns [String] the detail Ruby adds to the message of the supplied exception (see
237
+ # Log#exception_details), or nil when there is none or this formatter does not log it.
238
+ def exception_details_for(exception)
239
+ return unless log_exception_details
240
+
241
+ log.exception_details(exception, source: log_exception_details == :with_source)
242
+ end
243
+
244
+ # Returns [String] the backtrace of the logged exception, followed by each exception
245
+ # that caused it, as laid out by Log#backtrace_to_s, and cleaned by the backtrace cleaner
246
+ # (see #initialize).
247
+ #
248
+ # When `escape_control_chars` is enabled, the message of each cause and each backtrace
249
+ # line is passed through #escape_control_characters, the same as the message of the
250
+ # logged exception: causes are often parser or validation errors that quote untrusted
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.
256
+ def exception_backtrace
257
+ cleaner = active_backtrace_cleaner
258
+ unless escape_control_chars
259
+ text = joinable_backtrace(cleaner)
260
+ return text if text
261
+ end
262
+
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?
108
274
  end
109
275
 
110
276
  # Return the Time as a formatted string
@@ -71,6 +71,8 @@ module SemanticLogger
71
71
  #
72
72
  # Note: The option :multiline is set to false if not supplied.
73
73
  # Note: Has no effect if Awesome Print is not installed.
74
+ # Note: With `escape_control_chars: true` the payload is always rendered plain
75
+ # (without colors) and on a single line, then escaped.
74
76
  #
75
77
  # color_map: [Hash | SemanticLogger::Formatters::Color::ColorMap]
76
78
  # ColorMaps each of the log levels to a color
@@ -116,7 +118,7 @@ module SemanticLogger
116
118
 
117
119
  if log.payload.respond_to?(:ai)
118
120
  begin
119
- "-- #{log.payload.ai(@ai_options)}"
121
+ "-- #{escape_control_characters(log.payload_to_s { |payload| payload.ai(ai_options) })}"
120
122
  rescue StandardError
121
123
  super
122
124
  end
@@ -128,14 +130,27 @@ module SemanticLogger
128
130
  def exception
129
131
  return unless log.exception
130
132
 
131
- "-- Exception: #{color}#{log.exception.class}: " \
132
- "#{escape_control_characters(log.exception.message)}#{color_map.clear}\n#{log.backtrace_to_s}"
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}"
133
136
  end
134
137
 
135
138
  def call(log, logger)
136
139
  self.color = color_map[log.level]
137
140
  super
138
141
  end
142
+
143
+ private
144
+
145
+ # Returns [Hash] the Amazing Print options to render the payload with.
146
+ #
147
+ # When `escape_control_chars` is enabled the payload is rendered plain, on one line:
148
+ # the colors and line breaks Amazing Print adds cannot be told apart from control
149
+ # characters in the data (for example an object whose #inspect returns them), and
150
+ # escaping them all leaves no colors or line breaks to keep.
151
+ def ai_options
152
+ escape_control_chars ? @ai_options.merge(plain: true, multiline: false) : @ai_options
153
+ end
139
154
  end
140
155
  end
141
156
  end
@@ -10,15 +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
- 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".
21
+ #
22
+ # The file name is escaped like the other fields, since it comes from the first line of the
23
+ # backtrace, which a log call can supply from untrusted data.
19
24
  def file_name_and_line
20
25
  file, line = log.file_name_and_line(true)
21
- "#{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)
22
36
  end
23
37
 
24
38
  # Returns [String] the available process info
@@ -56,7 +70,7 @@ module SemanticLogger
56
70
 
57
71
  # Class / app name
58
72
  def name
59
- log.name
73
+ escape_control_characters(log.name)
60
74
  end
61
75
 
62
76
  # Log message
@@ -69,15 +83,21 @@ module SemanticLogger
69
83
  pl = log.payload_to_s
70
84
  return unless pl
71
85
 
72
- "-- #{pl}"
86
+ "-- #{escape_control_characters(pl)}"
73
87
  end
74
88
 
75
89
  # Exception
76
90
  def exception
77
91
  return unless log.exception
78
92
 
79
- "-- Exception: #{log.exception.class}: #{escape_control_characters(log.exception.message)}\n" \
80
- "#{log.backtrace_to_s}"
93
+ message = escape_control_characters(Utils.exception_message(log.exception))
94
+ "-- Exception: #{Utils.exception_name(log.exception)}: #{message}#{exception_detail_lines}"
95
+ end
96
+
97
+ # The detail Ruby adds to the exception's message, such as did_you_mean suggestions.
98
+ # See Base#initialize for the `log_exception_details` option.
99
+ def exception_details
100
+ escape_control_characters(exception_details_for(log.exception)) if log.exception
81
101
  end
82
102
 
83
103
  # Default text log format
@@ -89,6 +109,31 @@ module SemanticLogger
89
109
 
90
110
  [time, level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(" ")
91
111
  end
112
+
113
+ private
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
+
128
+ # Text output is read by people, who benefit most from exception details.
129
+ def default_log_exception_details
130
+ true
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
92
137
  end
93
138
  end
94
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