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.
- checksums.yaml +4 -4
- data/README.md +13 -12
- data/Rakefile +15 -4
- data/docs/api.md +196 -19
- data/docs/appenders.md +579 -68
- data/docs/config.md +647 -38
- data/docs/index.md +31 -5
- data/docs/log.md +56 -17
- data/docs/metrics.md +144 -30
- data/docs/operations.md +339 -24
- data/docs/rails.md +396 -8
- data/docs/security.md +222 -27
- data/docs/testing.md +28 -3
- data/docs/upgrading.md +938 -7
- data/lib/semantic_logger/appender/async.rb +42 -11
- data/lib/semantic_logger/appender/bugsnag.rb +7 -2
- data/lib/semantic_logger/appender/cloudwatch_logs.rb +168 -33
- data/lib/semantic_logger/appender/elasticsearch_base.rb +221 -28
- data/lib/semantic_logger/appender/elasticsearch_http.rb +23 -6
- data/lib/semantic_logger/appender/file.rb +160 -37
- data/lib/semantic_logger/appender/graylog.rb +101 -11
- data/lib/semantic_logger/appender/honeybadger.rb +7 -1
- data/lib/semantic_logger/appender/honeybadger_insights.rb +8 -2
- data/lib/semantic_logger/appender/http.rb +308 -46
- data/lib/semantic_logger/appender/io.rb +11 -6
- data/lib/semantic_logger/appender/kafka.rb +204 -44
- data/lib/semantic_logger/appender/loki.rb +55 -9
- data/lib/semantic_logger/appender/mongodb.rb +109 -5
- data/lib/semantic_logger/appender/new_relic.rb +3 -4
- data/lib/semantic_logger/appender/new_relic_logs.rb +35 -15
- data/lib/semantic_logger/appender/open_telemetry.rb +37 -35
- data/lib/semantic_logger/appender/rabbitmq.rb +246 -17
- data/lib/semantic_logger/appender/sentry.rb +3 -4
- data/lib/semantic_logger/appender/sentry_ruby.rb +159 -18
- data/lib/semantic_logger/appender/splunk.rb +11 -2
- data/lib/semantic_logger/appender/splunk_http.rb +92 -1
- data/lib/semantic_logger/appender/syslog.rb +149 -32
- data/lib/semantic_logger/appender/tcp.rb +73 -98
- data/lib/semantic_logger/appender/text_output.rb +65 -0
- data/lib/semantic_logger/appender/udp.rb +140 -18
- data/lib/semantic_logger/appender/wrapper.rb +10 -7
- data/lib/semantic_logger/appender.rb +115 -10
- data/lib/semantic_logger/appenders.rb +145 -5
- data/lib/semantic_logger/backtrace_cleaning.rb +77 -0
- data/lib/semantic_logger/base.rb +115 -81
- data/lib/semantic_logger/call_site.rb +235 -0
- data/lib/semantic_logger/context.rb +222 -0
- data/lib/semantic_logger/core_ext/process.rb +20 -5
- data/lib/semantic_logger/core_ext/warning.rb +70 -0
- data/lib/semantic_logger/deprecation.rb +139 -0
- data/lib/semantic_logger/exception_details.rb +51 -0
- data/lib/semantic_logger/formatters/base.rb +186 -20
- data/lib/semantic_logger/formatters/color.rb +18 -3
- data/lib/semantic_logger/formatters/default.rb +53 -8
- data/lib/semantic_logger/formatters/ecs.rb +9 -4
- data/lib/semantic_logger/formatters/json.rb +9 -0
- data/lib/semantic_logger/formatters/logfmt.rb +155 -21
- data/lib/semantic_logger/formatters/loki/labels.rb +282 -0
- data/lib/semantic_logger/formatters/loki.rb +430 -38
- data/lib/semantic_logger/formatters/new_relic_logs.rb +15 -13
- data/lib/semantic_logger/formatters/one_line.rb +101 -3
- data/lib/semantic_logger/formatters/open_telemetry.rb +191 -35
- data/lib/semantic_logger/formatters/pattern.rb +68 -12
- data/lib/semantic_logger/formatters/raw.rb +40 -11
- data/lib/semantic_logger/formatters/signalfx.rb +55 -15
- data/lib/semantic_logger/formatters/syslog.rb +114 -20
- data/lib/semantic_logger/formatters/syslog_cee.rb +38 -19
- data/lib/semantic_logger/formatters/syslog_header.rb +49 -0
- data/lib/semantic_logger/formatters.rb +1 -0
- data/lib/semantic_logger/levels.rb +10 -1
- data/lib/semantic_logger/log.rb +384 -82
- data/lib/semantic_logger/loggable.rb +56 -73
- data/lib/semantic_logger/logger.rb +31 -5
- data/lib/semantic_logger/metric/new_relic.rb +33 -18
- data/lib/semantic_logger/metric/signalfx.rb +29 -18
- data/lib/semantic_logger/metric/statsd.rb +41 -15
- data/lib/semantic_logger/processor.rb +194 -5
- data/lib/semantic_logger/queue_processor/backoff.rb +140 -0
- data/lib/semantic_logger/queue_processor/batch_failure.rb +69 -0
- data/lib/semantic_logger/queue_processor/held_write.rb +62 -0
- data/lib/semantic_logger/queue_processor/inbox.rb +97 -0
- data/lib/semantic_logger/queue_processor/reopener.rb +56 -0
- data/lib/semantic_logger/queue_processor/reporter.rb +113 -0
- data/lib/semantic_logger/queue_processor/request.rb +93 -0
- data/lib/semantic_logger/queue_processor/throttled_counter.rb +89 -0
- data/lib/semantic_logger/queue_processor.rb +519 -170
- data/lib/semantic_logger/ractor_copy.rb +120 -0
- data/lib/semantic_logger/ractor_processor.rb +82 -0
- data/lib/semantic_logger/ractor_receiver.rb +282 -0
- data/lib/semantic_logger/ractor_support.rb +28 -0
- data/lib/semantic_logger/redactor.rb +310 -0
- data/lib/semantic_logger/semantic_logger.rb +328 -81
- data/lib/semantic_logger/subscriber.rb +250 -10
- data/lib/semantic_logger/sync_processor.rb +237 -26
- data/lib/semantic_logger/test/capture_log_events.rb +26 -5
- data/lib/semantic_logger/test/minitest.rb +2 -1
- data/lib/semantic_logger/test/rspec.rb +1 -1
- data/lib/semantic_logger/trap_safe.rb +113 -0
- data/lib/semantic_logger/utils.rb +482 -67
- data/lib/semantic_logger/version.rb +1 -1
- data/lib/semantic_logger.rb +36 -3
- metadata +41 -5
- data/lib/semantic_logger/core_ext/thread.rb +0 -30
|
@@ -47,98 +47,19 @@ module SemanticLogger
|
|
|
47
47
|
# Create TCP log appender.
|
|
48
48
|
#
|
|
49
49
|
# Net::TCPClient Parameters:
|
|
50
|
+
# Every keyword argument other than the appender parameters below goes to Net::TCPClient.new,
|
|
51
|
+
# which raises ArgumentError for a keyword it does not take. See Net::TCPClient#initialize for
|
|
52
|
+
# them and their defaults, such as connect_timeout:, write_timeout:, connect_retry_count:,
|
|
53
|
+
# retry_count:, policy:, and ssl:
|
|
54
|
+
# https://github.com/reidmorrison/net_tcp_client/blob/master/lib/net/tcp_client/tcp_client.rb
|
|
55
|
+
#
|
|
50
56
|
# :server [String]
|
|
51
|
-
#
|
|
52
|
-
#
|
|
53
|
-
#
|
|
57
|
+
# The server to connect to, as "host:port", such as 'localhost:2000' or '192.168.1.10:80'.
|
|
58
|
+
# Net::TCPClient connects over IPv4 only, so the host must be an IPv4 address or a host name
|
|
59
|
+
# that has one.
|
|
54
60
|
#
|
|
55
61
|
# :servers [Array of String]
|
|
56
|
-
#
|
|
57
|
-
# ['server1:2000', 'server2:2000']
|
|
58
|
-
#
|
|
59
|
-
# The second server will only be attempted once the first server
|
|
60
|
-
# cannot be connected to or has timed out on connect
|
|
61
|
-
# A read failure or timeout will not result in switching to the second
|
|
62
|
-
# server, only a connection failure or during an automatic reconnect
|
|
63
|
-
#
|
|
64
|
-
# :connect_timeout [Float]
|
|
65
|
-
# Time in seconds to timeout when trying to connect to the server
|
|
66
|
-
# A value of -1 will cause the connect wait time to be infinite
|
|
67
|
-
# Default: Half of the :read_timeout ( 30 seconds )
|
|
68
|
-
#
|
|
69
|
-
# :read_timeout [Float]
|
|
70
|
-
# Time in seconds to timeout on read
|
|
71
|
-
# Can be overridden by supplying a timeout in the read call
|
|
72
|
-
# Default: 60
|
|
73
|
-
#
|
|
74
|
-
# :write_timeout [Float]
|
|
75
|
-
# Time in seconds to timeout on write
|
|
76
|
-
# Can be overridden by supplying a timeout in the write call
|
|
77
|
-
# Default: 60
|
|
78
|
-
#
|
|
79
|
-
# :log_level [Symbol]
|
|
80
|
-
# Optional: Set the logging level for the TCPClient
|
|
81
|
-
# Any valid SemanticLogger log level:
|
|
82
|
-
# :trace, :debug, :info, :warn, :error, :fatal
|
|
83
|
-
# Default: SemanticLogger.default_level
|
|
84
|
-
#
|
|
85
|
-
# :buffered [Boolean]
|
|
86
|
-
# Whether to use Nagle's Buffering algorithm (http://en.wikipedia.org/wiki/Nagle's_algorithm)
|
|
87
|
-
# Recommend disabling for RPC style invocations where we don't want to wait for an
|
|
88
|
-
# ACK from the server before sending the last partial segment
|
|
89
|
-
# Buffering is recommended in a browser or file transfer style environment
|
|
90
|
-
# where multiple sends are expected during a single response
|
|
91
|
-
# Default: true
|
|
92
|
-
#
|
|
93
|
-
# :connect_retry_count [Integer]
|
|
94
|
-
# Number of times to retry connecting when a connection fails
|
|
95
|
-
# Default: 10
|
|
96
|
-
#
|
|
97
|
-
# :connect_retry_interval [Float]
|
|
98
|
-
# Number of seconds between connection retry attempts after the first failed attempt
|
|
99
|
-
# Default: 0.5
|
|
100
|
-
#
|
|
101
|
-
# :retry_count [Integer]
|
|
102
|
-
# Number of times to retry when calling #retry_on_connection_failure
|
|
103
|
-
# This is independent of :connect_retry_count which still applies with
|
|
104
|
-
# connection failures. This retry controls upto how many times to retry the
|
|
105
|
-
# supplied block should a connection failure occurr during the block
|
|
106
|
-
# Default: 3
|
|
107
|
-
#
|
|
108
|
-
# :on_connect [Proc]
|
|
109
|
-
# Directly after a connection is established and before it is made available
|
|
110
|
-
# for use this Block is invoked.
|
|
111
|
-
# Typical Use Cases:
|
|
112
|
-
# - Initialize per connection session sequence numbers
|
|
113
|
-
# - Pass any authentication information to the server
|
|
114
|
-
# - Perform a handshake with the server
|
|
115
|
-
#
|
|
116
|
-
# :policy [Symbol|Proc]
|
|
117
|
-
# Specify the policy to use when connecting to servers.
|
|
118
|
-
# :ordered
|
|
119
|
-
# Select a server in the order supplied in the array, with the first
|
|
120
|
-
# having the highest priority. The second server will only be connected
|
|
121
|
-
# to if the first server is unreachable
|
|
122
|
-
# :random
|
|
123
|
-
# Randomly select a server from the list every time a connection
|
|
124
|
-
# is established, including during automatic connection recovery.
|
|
125
|
-
# :ping_time
|
|
126
|
-
# FUTURE - Not implemented yet - Pull request anyone?
|
|
127
|
-
# The server with the lowest ping time will be tried first
|
|
128
|
-
# Proc:
|
|
129
|
-
# When a Proc is supplied, it will be called passing in the list
|
|
130
|
-
# of servers. The Proc must return one server name
|
|
131
|
-
# Example:
|
|
132
|
-
# :policy => Proc.new do |servers|
|
|
133
|
-
# servers.last
|
|
134
|
-
# end
|
|
135
|
-
# Default: :ordered
|
|
136
|
-
#
|
|
137
|
-
# :close_on_error [True|False]
|
|
138
|
-
# To prevent the connection from going into an inconsistent state
|
|
139
|
-
# automatically close the connection if an error occurs
|
|
140
|
-
# This includes a Read Timeout
|
|
141
|
-
# Default: true
|
|
62
|
+
# Instead of :server, several servers, each as "host:port".
|
|
142
63
|
#
|
|
143
64
|
# Appender Parameters:
|
|
144
65
|
# separator: [String]
|
|
@@ -157,18 +78,24 @@ module SemanticLogger
|
|
|
157
78
|
#
|
|
158
79
|
# level: [:trace | :debug | :info | :warn | :error | :fatal]
|
|
159
80
|
# Override the log level for this appender.
|
|
160
|
-
# Default:
|
|
81
|
+
# Default: none (:trace), so it writes every entry that a logger emits; each logger
|
|
82
|
+
# filters by its own level.
|
|
161
83
|
#
|
|
162
|
-
# formatter: [Object|Proc]
|
|
84
|
+
# formatter: [Object|Proc|Symbol|Hash]
|
|
163
85
|
# An instance of a class that implements #call, or a Proc to be used to format
|
|
164
86
|
# the output from this appender
|
|
165
|
-
# Default:
|
|
87
|
+
# Default: :json
|
|
166
88
|
#
|
|
167
89
|
# filter: [Regexp|Proc]
|
|
168
90
|
# RegExp: Only include log messages where the class name matches the supplied.
|
|
169
91
|
# regular expression. All other messages will be ignored.
|
|
170
92
|
# Proc: Only include log messages where the supplied Proc returns true
|
|
171
93
|
# The Proc must return true or false.
|
|
94
|
+
#
|
|
95
|
+
# metrics: [Boolean]
|
|
96
|
+
# Also send metric-only entries: a metric without a message or an exception.
|
|
97
|
+
# Default: false
|
|
98
|
+
#
|
|
172
99
|
# Example:
|
|
173
100
|
# SemanticLogger.add_appender(
|
|
174
101
|
# appender: :tcp,
|
|
@@ -197,17 +124,28 @@ module SemanticLogger
|
|
|
197
124
|
reopen
|
|
198
125
|
end
|
|
199
126
|
|
|
200
|
-
#
|
|
127
|
+
# Closes the connection, and connects to the server again, such as after a log rotation, or a fork.
|
|
128
|
+
#
|
|
129
|
+
# After a fork, the child first forgets the client it inherited, without closing its connection, which
|
|
130
|
+
# belongs to the parent: closing a TLS connection sends the server a close_notify, which ends it for the
|
|
131
|
+
# parent too. The child closes only its own copy of the socket's file descriptor, which sends the server
|
|
132
|
+
# nothing, so that the server sees the connection end once the parent closes it, rather than once the
|
|
133
|
+
# child exits, or collects the client. When connecting fails in the child, the appender has no client
|
|
134
|
+
# until the next write connects again (see #log), rather than writing through the parent's connection.
|
|
201
135
|
def reopen
|
|
136
|
+
forget_inherited
|
|
202
137
|
close
|
|
203
|
-
|
|
138
|
+
connect
|
|
204
139
|
end
|
|
205
140
|
|
|
206
|
-
# Write the log using the specified protocol and server.
|
|
141
|
+
# Write the log using the specified protocol and server. Connects first when the appender has no
|
|
142
|
+
# client, since connecting failed when it was reopened after a fork, or it was closed in a forked
|
|
143
|
+
# child.
|
|
207
144
|
def log(log)
|
|
208
|
-
message
|
|
209
|
-
@tcp_client
|
|
210
|
-
|
|
145
|
+
message = format_log(log)
|
|
146
|
+
tcp_client = @tcp_client || connect
|
|
147
|
+
tcp_client.retry_on_connection_failure do
|
|
148
|
+
tcp_client.write("#{message}#{separator}")
|
|
211
149
|
end
|
|
212
150
|
true
|
|
213
151
|
end
|
|
@@ -218,12 +156,49 @@ module SemanticLogger
|
|
|
218
156
|
end
|
|
219
157
|
|
|
220
158
|
# Close is called during shutdown, or with reopen
|
|
159
|
+
#
|
|
160
|
+
# In a forked child that has not reopened, such as one that removes the appender before its reopen has
|
|
161
|
+
# run, it forgets the client it inherited instead, without closing its connection, which belongs to the
|
|
162
|
+
# parent, and the next write connects again (see #reopen).
|
|
221
163
|
def close
|
|
164
|
+
forget_inherited
|
|
222
165
|
@tcp_client&.close
|
|
223
166
|
end
|
|
224
167
|
|
|
168
|
+
# Only the servers: the other options can hold TLS keys and passwords. See Subscriber#inspect.
|
|
169
|
+
def inspect_details
|
|
170
|
+
"server=#{(@tcp_client_args[:server] || @tcp_client_args[:servers]).inspect}"
|
|
171
|
+
end
|
|
172
|
+
|
|
225
173
|
private
|
|
226
174
|
|
|
175
|
+
# Returns [Net::TCPClient] a new client, connected to the server, after making it the appender's.
|
|
176
|
+
# Records the process that built it, which owns its connection (see #reopen).
|
|
177
|
+
def connect
|
|
178
|
+
@tcp_client = Net::TCPClient.new(**tcp_client_options)
|
|
179
|
+
@pid = ::Process.pid
|
|
180
|
+
@tcp_client
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# In a child process, forgets the client built in the parent before a fork, and then closes the child's
|
|
184
|
+
# copy of its socket's file descriptor, without a TLS shutdown: SSLSocket#to_io is the TCPSocket under
|
|
185
|
+
# it. See #reopen.
|
|
186
|
+
def forget_inherited
|
|
187
|
+
return if @pid == ::Process.pid
|
|
188
|
+
|
|
189
|
+
inherited = @tcp_client
|
|
190
|
+
@tcp_client = nil
|
|
191
|
+
socket = inherited&.socket&.to_io
|
|
192
|
+
socket.close unless socket.nil? || socket.closed?
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# Returns [Hash] a copy of the options for a new Net::TCPClient (see Subscriber#copy_options), since it
|
|
196
|
+
# takes handshake_timeout: out of the ssl: Hash it is given: each client gets a copy of its own, which
|
|
197
|
+
# keeps the timeout for the next one, such as after a reopen.
|
|
198
|
+
def tcp_client_options
|
|
199
|
+
copy_options(@tcp_client_args)
|
|
200
|
+
end
|
|
201
|
+
|
|
227
202
|
# Returns [SemanticLogger::Formatters::Default] formatter default for this Appender
|
|
228
203
|
def default_formatter
|
|
229
204
|
SemanticLogger::Formatters::Json.new
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module SemanticLogger
|
|
2
|
+
module Appender
|
|
3
|
+
# Writes the formatted text of log entries to an IO, such as a log file or standard output, for
|
|
4
|
+
# the appenders that write to one (Appender::File and Appender::IO).
|
|
5
|
+
#
|
|
6
|
+
# An IO with an external encoding converts what it writes to that encoding, and raises an
|
|
7
|
+
# EncodingError for text that it cannot convert, which would lose the entry. A file opened with an
|
|
8
|
+
# `encoding:` such as UTF-16LE refuses invalid UTF-8, binary text that holds bytes outside ASCII,
|
|
9
|
+
# which a custom formatter can return, and a character that its encoding does not have. An IO
|
|
10
|
+
# that converts to UTF-8, such as one opened after Rails set `Encoding.default_internal`, refuses
|
|
11
|
+
# only such binary text: it writes text tagged UTF-8 that holds invalid bytes as it is.
|
|
12
|
+
module TextOutput
|
|
13
|
+
# Writes the text to the IO, or, when the IO cannot convert it to its encoding, the text
|
|
14
|
+
# converted first (see .writable_text). Only text that the IO refuses is converted.
|
|
15
|
+
#
|
|
16
|
+
# Only an IO that converts the whole text before it writes any of it is written to again (see
|
|
17
|
+
# .converts_whole_text?). Another object that responds to #write, such as one that writes to
|
|
18
|
+
# two IOs, could have written part of the text before it raised, which would then be written
|
|
19
|
+
# twice, so its error is raised as before.
|
|
20
|
+
#
|
|
21
|
+
# Returns what IO#write returns: the number of bytes written, in the IO's encoding.
|
|
22
|
+
def self.write(io, text)
|
|
23
|
+
io.write(text)
|
|
24
|
+
rescue EncodingError
|
|
25
|
+
raise unless converts_whole_text?(io)
|
|
26
|
+
|
|
27
|
+
io.write(writable_text(text, io))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Returns [String] the text, converted to valid UTF-8 as the JSON formatters convert it (see
|
|
31
|
+
# Utils.encode_utf8_string), and then, for an IO with an external encoding other than UTF-8,
|
|
32
|
+
# converted to that encoding, with "?" (or U+FFFD for a Unicode encoding) in place of each
|
|
33
|
+
# character that it does not have.
|
|
34
|
+
def self.writable_text(text, io)
|
|
35
|
+
text = Utils.encode_utf8_string(text)
|
|
36
|
+
encoding = io.external_encoding if io.respond_to?(:external_encoding)
|
|
37
|
+
return text if encoding.nil? || encoding == Encoding::UTF_8 || encoding == Encoding::BINARY
|
|
38
|
+
|
|
39
|
+
text.encode(encoding, invalid: :replace, undef: :replace)
|
|
40
|
+
end
|
|
41
|
+
private_class_method :writable_text
|
|
42
|
+
|
|
43
|
+
# Whether the IO converts the whole text before it writes any of it: an ::IO, a Tempfile, which
|
|
44
|
+
# writes with File#write, or a StringIO.
|
|
45
|
+
def self.converts_whole_text?(io)
|
|
46
|
+
io.is_a?(::IO) || (defined?(::Tempfile) && io.is_a?(::Tempfile)) ||
|
|
47
|
+
(defined?(::StringIO) && io.is_a?(::StringIO))
|
|
48
|
+
end
|
|
49
|
+
private_class_method :converts_whole_text?
|
|
50
|
+
|
|
51
|
+
# Returns [String] the formatted text of a log entry as the line that .write writes: a new
|
|
52
|
+
# String of it followed by a line break. A new one, since the formatter's String can be one it
|
|
53
|
+
# does not own, such as a custom formatter's log.message, the application's own, or frozen. It
|
|
54
|
+
# keeps the text's encoding. An object that converts to a String implicitly (#to_str) is read
|
|
55
|
+
# as that String, and anything else, such as nil, a Hash, or a Pathname, raises TypeError.
|
|
56
|
+
# Interpolation would write any object, and tag US-ASCII text UTF-8.
|
|
57
|
+
def self.line(text)
|
|
58
|
+
string = String.try_convert(text)
|
|
59
|
+
raise(TypeError, "Formatter returned a #{text.class}, not a String") unless string
|
|
60
|
+
|
|
61
|
+
string + "\n" # rubocop:disable Style/StringConcatenation
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -10,15 +10,29 @@ module SemanticLogger
|
|
|
10
10
|
# SemanticLogger.add_appender(
|
|
11
11
|
# appender: :udp,
|
|
12
12
|
# server: 'server:3300',
|
|
13
|
+
# metrics: false
|
|
13
14
|
# )
|
|
14
15
|
class Udp < SemanticLogger::Subscriber
|
|
16
|
+
# The host and port of a server, with an IPv6 address in brackets, as a URL writes it.
|
|
17
|
+
SERVER = /\A(?:\[(?<ipv6>[^\[\]]+)\]|(?<host>[^:\[\]]+)):(?<port>\d+)\z/
|
|
18
|
+
private_constant :SERVER
|
|
19
|
+
|
|
20
|
+
# The codes of a lookup that found no address of the family it asked for, rather than failing, of those that
|
|
21
|
+
# the platform defines.
|
|
22
|
+
NO_ADDRESS = %i[EAI_NONAME EAI_NODATA EAI_ADDRFAMILY].filter_map do |name|
|
|
23
|
+
Socket.const_get(name) if Socket.const_defined?(name)
|
|
24
|
+
end.freeze
|
|
25
|
+
private_constant :NO_ADDRESS
|
|
26
|
+
|
|
15
27
|
attr_accessor :server, :udp_flags
|
|
16
28
|
attr_reader :socket
|
|
17
29
|
|
|
18
30
|
# Create UDP log appender.
|
|
19
31
|
#
|
|
20
32
|
# server: [String]
|
|
21
|
-
#
|
|
33
|
+
# The server to write UDP messages to, as "host:port", such as "localhost:8088", with an IPv6
|
|
34
|
+
# address in brackets, as a URL writes it, such as "[::1]:8088". A host name is sent to over
|
|
35
|
+
# IPv4 when it has an IPv4 address, and otherwise, or when it cannot connect over IPv4, over IPv6.
|
|
22
36
|
#
|
|
23
37
|
# udp_flags: [Integer]
|
|
24
38
|
# Should be a bitwise OR of Socket::MSG_* constants.
|
|
@@ -35,12 +49,13 @@ module SemanticLogger
|
|
|
35
49
|
#
|
|
36
50
|
# level: [:trace | :debug | :info | :warn | :error | :fatal]
|
|
37
51
|
# Override the log level for this appender.
|
|
38
|
-
# Default:
|
|
52
|
+
# Default: none (:trace), so it writes every entry that a logger emits; each logger
|
|
53
|
+
# filters by its own level.
|
|
39
54
|
#
|
|
40
|
-
# formatter: [Object|Proc]
|
|
55
|
+
# formatter: [Object|Proc|Symbol|Hash]
|
|
41
56
|
# An instance of a class that implements #call, or a Proc to be used to format
|
|
42
57
|
# the output from this appender
|
|
43
|
-
# Default:
|
|
58
|
+
# Default: :json
|
|
44
59
|
#
|
|
45
60
|
# filter: [Regexp|Proc]
|
|
46
61
|
# RegExp: Only include log messages where the class name matches the supplied.
|
|
@@ -50,7 +65,7 @@ module SemanticLogger
|
|
|
50
65
|
#
|
|
51
66
|
# metrics: [Boolean]
|
|
52
67
|
# Send metrics only events over udp.
|
|
53
|
-
# Default: true
|
|
68
|
+
# Default: true, with a deprecation warning when it is not supplied, since v6 changes it to false.
|
|
54
69
|
#
|
|
55
70
|
# Limitations:
|
|
56
71
|
# * UDP packet size is limited by the connected network and any routers etc
|
|
@@ -59,44 +74,151 @@ module SemanticLogger
|
|
|
59
74
|
# Example:
|
|
60
75
|
# SemanticLogger.add_appender(
|
|
61
76
|
# appender: :udp,
|
|
62
|
-
# server: 'server:3300'
|
|
77
|
+
# server: 'server:3300',
|
|
78
|
+
# metrics: false
|
|
63
79
|
# )
|
|
64
|
-
def initialize(server:, udp_flags: 0, metrics:
|
|
80
|
+
def initialize(server:, udp_flags: 0, metrics: METRICS_NOT_SUPPLIED, **args, &)
|
|
65
81
|
@server = server
|
|
66
82
|
@udp_flags = udp_flags
|
|
67
83
|
|
|
68
|
-
super(metrics: metrics, **args, &)
|
|
84
|
+
super(metrics: deprecated_metrics_default(metrics, :udp), **args, &)
|
|
69
85
|
reopen
|
|
70
86
|
end
|
|
71
87
|
|
|
72
88
|
# After forking an active process call #reopen to re-open
|
|
73
89
|
# open the handles to resources
|
|
90
|
+
#
|
|
91
|
+
# Connects a new socket to the server before closing the one it replaces. When it cannot, it
|
|
92
|
+
# raises, keeping the socket it has: ArgumentError for a server that is not "host:port", or
|
|
93
|
+
# the error from resolving or connecting, such as SocketError for a host name that does not resolve.
|
|
94
|
+
# An error closing the socket it replaced is reported to the internal logger as a warning, since the
|
|
95
|
+
# reopen itself worked.
|
|
74
96
|
def reopen
|
|
75
|
-
|
|
76
|
-
@socket
|
|
77
|
-
|
|
78
|
-
|
|
97
|
+
socket = connected_socket
|
|
98
|
+
old_socket = @socket
|
|
99
|
+
@socket = socket
|
|
100
|
+
close_socket(old_socket, replaced: true)
|
|
79
101
|
end
|
|
80
102
|
|
|
81
|
-
# Write the log using the specified protocol and server.
|
|
103
|
+
# Write the log using the specified protocol and server. Opens a new socket, connected to the server, first
|
|
104
|
+
# when the appender has none, after #close.
|
|
82
105
|
def log(log)
|
|
83
|
-
@socket.send(
|
|
106
|
+
(@socket ||= connected_socket).send(format_log(log), udp_flags)
|
|
84
107
|
true
|
|
85
108
|
end
|
|
86
109
|
|
|
87
|
-
# Flush is called by the semantic_logger during shutdown.
|
|
110
|
+
# Flush is called by the semantic_logger during shutdown. Does nothing after #close, which left no socket.
|
|
88
111
|
def flush
|
|
89
112
|
@socket&.flush
|
|
90
113
|
end
|
|
91
114
|
|
|
92
|
-
#
|
|
115
|
+
# Closes the socket, such as when the appender is removed, and forgets it, so that the next write opens a new
|
|
116
|
+
# one. An error closing it is reported to the internal logger as a warning, rather than raised.
|
|
117
|
+
#
|
|
118
|
+
# In a forked child, the socket is the child's own copy of the parent's, so closing it leaves the parent's open.
|
|
93
119
|
def close
|
|
94
|
-
@socket
|
|
120
|
+
socket = @socket
|
|
121
|
+
@socket = nil
|
|
122
|
+
close_socket(socket)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# A log message too large for a datagram (Errno::EMSGSIZE) cannot be sent by retrying.
|
|
126
|
+
def message_error?(exception)
|
|
127
|
+
exception.is_a?(Errno::EMSGSIZE) || super
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# See Subscriber#inspect.
|
|
131
|
+
def inspect_details
|
|
132
|
+
"server=#{server.inspect}"
|
|
95
133
|
end
|
|
96
134
|
|
|
97
135
|
private
|
|
98
136
|
|
|
99
|
-
#
|
|
137
|
+
# Closes the given socket, if any: the one that a reopen replaced, when replaced: is true. An error closing
|
|
138
|
+
# it is reported to the internal logger as a warning, rather than raised.
|
|
139
|
+
def close_socket(socket, replaced: false)
|
|
140
|
+
socket&.close
|
|
141
|
+
rescue StandardError => e
|
|
142
|
+
logger.warn("Could not close the socket to the UDP server#{' that a reopen replaced' if replaced}", e)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Returns [UDPSocket] a new socket, connected to the first of the server's addresses that it can connect to, as
|
|
146
|
+
# Ruby's own UDPSocket#connect tries the addresses of its socket's family in turn. A host name's IPv4 addresses
|
|
147
|
+
# come first, so that a name with both is still sent to over IPv4, as when the socket was IPv4 only, whichever
|
|
148
|
+
# address the resolver returns first. Its IPv6 addresses are looked up only when the resolver answers that it has
|
|
149
|
+
# no IPv4 address, or it has none that it can connect to, so that a resolver that does not answer for them does
|
|
150
|
+
# not hold up the others. Each family keeps the resolver's order. When looking up its IPv4 addresses fails for
|
|
151
|
+
# another reason, such as a resolver that does not answer in time, raises that SocketError at once, rather than
|
|
152
|
+
# sending over IPv6 to a name that has IPv4 addresses, or waiting for a resolver that is down a second time.
|
|
153
|
+
#
|
|
154
|
+
# When it cannot connect to any address, raises the error from the first one it tried, the one it prefers. When
|
|
155
|
+
# the host has no address, such as a name that does not resolve, raises the SocketError from looking up its IPv6
|
|
156
|
+
# addresses, with the one from looking up its IPv4 addresses as its cause (see #raise_lookup_error).
|
|
157
|
+
def connected_socket
|
|
158
|
+
host, port, families = server_endpoint
|
|
159
|
+
connect_error = nil
|
|
160
|
+
lookup_errors = []
|
|
161
|
+
families.each do |family|
|
|
162
|
+
Addrinfo.getaddrinfo(host, port, family, :DGRAM).each do |address|
|
|
163
|
+
return connect_to(address)
|
|
164
|
+
rescue SystemCallError => e
|
|
165
|
+
connect_error ||= e
|
|
166
|
+
end
|
|
167
|
+
rescue SocketError => e
|
|
168
|
+
lookup_errors << e
|
|
169
|
+
break unless no_address?(e)
|
|
170
|
+
end
|
|
171
|
+
raise(connect_error) if connect_error
|
|
172
|
+
|
|
173
|
+
raise_lookup_error(host, lookup_errors)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Returns [true|false] whether the SocketError from looking up a host's addresses of one family says that it has
|
|
177
|
+
# none, rather than that the lookup failed. It cannot tell without the error's code, which Socket::ResolutionError
|
|
178
|
+
# has from Ruby 3.3, so on Ruby 3.2, or on JRuby, it takes any SocketError to say so, as before.
|
|
179
|
+
def no_address?(error)
|
|
180
|
+
!error.respond_to?(:error_code) || NO_ADDRESS.include?(error.error_code)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# Raises the last of the SocketErrors from looking up the host's addresses, with the one before it, if any, as its
|
|
184
|
+
# cause, since it looked up the later family only because the earlier one had no address. Raises a SocketError
|
|
185
|
+
# of its own, naming the host, when no lookup raised, since JRuby returns no addresses for a family that the host
|
|
186
|
+
# has none in, rather than raising.
|
|
187
|
+
def raise_lookup_error(host, errors)
|
|
188
|
+
last, earlier = errors.reverse
|
|
189
|
+
raise(SocketError, "getaddrinfo: no address for #{host}") unless last
|
|
190
|
+
|
|
191
|
+
earlier ? raise(last, cause: earlier) : raise(last)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Returns [UDPSocket] a new socket of the address's family, connected to it. Closes the socket when it does not
|
|
195
|
+
# return it, such as when connecting raises, or the thread is interrupted.
|
|
196
|
+
def connect_to(address)
|
|
197
|
+
socket = UDPSocket.new(address.afamily)
|
|
198
|
+
socket.connect(address.ip_address, address.ip_port)
|
|
199
|
+
connected = true
|
|
200
|
+
socket
|
|
201
|
+
ensure
|
|
202
|
+
socket&.close unless connected
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Returns [Array(String, Integer, Array<Symbol>)] the host and port of the server, and the address families to
|
|
206
|
+
# look the host up in, in the order to try them. A host in brackets is an IPv6 address. Any other host is an IPv4
|
|
207
|
+
# address, or a name, which is looked up for its IPv4 addresses first, then for its IPv6 addresses. Whitespace
|
|
208
|
+
# around the server, such as the newline at the end of an environment variable, is ignored.
|
|
209
|
+
def server_endpoint
|
|
210
|
+
match = SERVER.match(server.to_s.strip)
|
|
211
|
+
unless match
|
|
212
|
+
raise(ArgumentError,
|
|
213
|
+
"Invalid :server #{server.inspect}, it must be \"host:port\", such as \"localhost:8088\", " \
|
|
214
|
+
"with an IPv6 address in brackets, such as \"[::1]:8088\".")
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
port = match[:port].to_i
|
|
218
|
+
match[:ipv6] ? [match[:ipv6], port, %i[INET6]] : [match[:host], port, %i[INET INET6]]
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Returns [SemanticLogger::Formatters::Json] formatter default for this Appender
|
|
100
222
|
def default_formatter
|
|
101
223
|
SemanticLogger::Formatters::Json.new
|
|
102
224
|
end
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
module SemanticLogger
|
|
5
5
|
module Appender
|
|
6
6
|
class Wrapper < SemanticLogger::Subscriber
|
|
7
|
-
|
|
7
|
+
# The logger this appender writes to. Not #logger, which is the internal logger that this
|
|
8
|
+
# appender reports its own problems to, as every appender does (see Subscriber#logger).
|
|
9
|
+
attr_reader :wrapped_logger
|
|
8
10
|
|
|
9
11
|
# Forward all logging calls to the supplied logging instance.
|
|
10
12
|
#
|
|
@@ -14,7 +16,8 @@ module SemanticLogger
|
|
|
14
16
|
#
|
|
15
17
|
# level: [:trace | :debug | :info | :warn | :error | :fatal]
|
|
16
18
|
# Override the log level for this appender.
|
|
17
|
-
# Default:
|
|
19
|
+
# Default: none (:trace), so it writes every entry that a logger emits; each logger
|
|
20
|
+
# filters by its own level.
|
|
18
21
|
#
|
|
19
22
|
# formatter: [Object|Proc]
|
|
20
23
|
# An instance of a class that implements #call, or a Proc to be used to format
|
|
@@ -39,10 +42,10 @@ module SemanticLogger
|
|
|
39
42
|
#
|
|
40
43
|
# Install the `rails_semantic_logger` gem to replace the Rails logger with Semantic Logger.
|
|
41
44
|
def initialize(logger:, **args, &)
|
|
42
|
-
@
|
|
45
|
+
@wrapped_logger = logger
|
|
43
46
|
|
|
44
47
|
# Check if the custom appender responds to all the log levels. For example Ruby ::Logger
|
|
45
|
-
does_not_implement = LEVELS[1..].find { |i| !@
|
|
48
|
+
does_not_implement = LEVELS[1..].find { |i| !@wrapped_logger.respond_to?(i) }
|
|
46
49
|
if does_not_implement
|
|
47
50
|
raise(ArgumentError,
|
|
48
51
|
"Supplied logger does not implement:#{does_not_implement}. " \
|
|
@@ -57,20 +60,20 @@ module SemanticLogger
|
|
|
57
60
|
# Ruby or Rails Loggers
|
|
58
61
|
def log(log)
|
|
59
62
|
level = log.level == :trace ? :debug : log.level
|
|
60
|
-
@
|
|
63
|
+
@wrapped_logger.send(level, format_log(log))
|
|
61
64
|
true
|
|
62
65
|
end
|
|
63
66
|
|
|
64
67
|
# Flush all pending logs to disk.
|
|
65
68
|
# Waits for all queued log messages to be written to disk.
|
|
66
69
|
def flush
|
|
67
|
-
@
|
|
70
|
+
@wrapped_logger.flush if @wrapped_logger.respond_to?(:flush)
|
|
68
71
|
end
|
|
69
72
|
|
|
70
73
|
# Close underlying log
|
|
71
74
|
# Waits for all queued log messages to be written to disk.
|
|
72
75
|
def close
|
|
73
|
-
@
|
|
76
|
+
@wrapped_logger.close if @wrapped_logger.respond_to?(:close)
|
|
74
77
|
end
|
|
75
78
|
end
|
|
76
79
|
end
|