Class: SemanticLogger::Appender::Udp

Inherits:
Subscriber
  • Object
show all
Defined in:
lib/semantic_logger/appender/udp.rb

Overview

UDP log appender.

Write log messages to UDP. By default messages are in JSON format.

Example:

SemanticLogger.add_appender(
appender: :udp,
server:   'server:3300',
metrics:  false
)

Constant Summary

Constants inherited from Subscriber

Subscriber::MESSAGE_ERRORS

Instance Attribute Summary collapse

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #console_output?, #console_stream, #fails_any_batch?, #inspect, #not_sent?, #rejected_entry, #should_log?, #wrote_nothing?

Constructor Details

#initialize(server:, udp_flags: 0, metrics: METRICS_NOT_SUPPLIED, **args) ⇒ Udp

Create UDP log appender.

server: [String]
The server to write UDP messages to, as "host:port", such as "localhost:8088", with an IPv6
address in brackets, as a URL writes it, such as "[::1]:8088". A host name is sent to over
IPv4 when it has an IPv4 address, and otherwise, or when it cannot connect over IPv4, over IPv6.

udp_flags: [Integer]
Should be a bitwise OR of Socket::MSG_* constants.
Default: 0

Common Appender Parameters:

application: [String]
Name of this application to appear in log messages.
Default: SemanticLogger.application

host: [String]
Name of this host to appear in log messages.
Default: SemanticLogger.host

level: [:trace | :debug | :info | :warn | :error | :fatal]
Override the log level for this appender.
Default: none (:trace), so it writes every entry that a logger emits; each logger
filters by its own level.

formatter: [Object|Proc|Symbol|Hash]
An instance of a class that implements #call, or a Proc to be used to format
the output from this appender
Default: :json

filter: [Regexp|Proc]
RegExp: Only include log messages where the class name matches the supplied.
regular expression. All other messages will be ignored.
Proc: Only include log messages where the supplied Proc returns true
      The Proc must return true or false.

metrics: [Boolean]
Send metrics only events over udp.
Default: true, with a deprecation warning when it is not supplied, since v6 changes it to false.

Limitations:

Example:

SemanticLogger.add_appender(
appender: :udp,
server:   'server:3300',
metrics:  false
)


80
81
82
83
84
85
86
# File 'lib/semantic_logger/appender/udp.rb', line 80

def initialize(server:, udp_flags: 0, metrics: METRICS_NOT_SUPPLIED, **args, &)
  @server    = server
  @udp_flags = udp_flags

  super(metrics: deprecated_metrics_default(metrics, :udp), **args, &)
  reopen
end

Instance Attribute Details

#server ⇒ Object

Returns the value of attribute server.



27
28
29
# File 'lib/semantic_logger/appender/udp.rb', line 27

def server
  @server
end

#socket ⇒ Object (readonly)

Returns the value of attribute socket.



28
29
30
# File 'lib/semantic_logger/appender/udp.rb', line 28

def socket
  @socket
end

#udp_flags ⇒ Object

Returns the value of attribute udp_flags.



27
28
29
# File 'lib/semantic_logger/appender/udp.rb', line 27

def udp_flags
  @udp_flags
end

Instance Method Details

#close ⇒ Object

Closes the socket, such as when the appender is removed, and forgets it, so that the next write opens a new one. An error closing it is reported to the internal logger as a warning, rather than raised.

In a forked child, the socket is the child's own copy of the parent's, so closing it leaves the parent's open.



119
120
121
122
123
# File 'lib/semantic_logger/appender/udp.rb', line 119

def close
  socket  = @socket
  @socket = nil
  close_socket(socket)
end

#flush ⇒ Object

Flush is called by the semantic_logger during shutdown. Does nothing after #close, which left no socket.



111
112
113
# File 'lib/semantic_logger/appender/udp.rb', line 111

def flush
  @socket&.flush
end

#inspect_details ⇒ Object

See Subscriber#inspect.



131
132
133
# File 'lib/semantic_logger/appender/udp.rb', line 131

def inspect_details
  "server=#{server.inspect}"
end

#log(log) ⇒ Object

Write the log using the specified protocol and server. Opens a new socket, connected to the server, first when the appender has none, after #close.



105
106
107
108
# File 'lib/semantic_logger/appender/udp.rb', line 105

def log(log)
  (@socket ||= connected_socket).send(format_log(log), udp_flags)
  true
end

#message_error?(exception) ⇒ Boolean

A log message too large for a datagram (Errno::EMSGSIZE) cannot be sent by retrying.

Returns:

  • (Boolean)


126
127
128
# File 'lib/semantic_logger/appender/udp.rb', line 126

def message_error?(exception)
  exception.is_a?(Errno::EMSGSIZE) || super
end

#reopen ⇒ Object

After forking an active process call #reopen to re-open open the handles to resources

Connects a new socket to the server before closing the one it replaces. When it cannot, it raises, keeping the socket it has: ArgumentError for a server that is not "host:port", or the error from resolving or connecting, such as SocketError for a host name that does not resolve. An error closing the socket it replaced is reported to the internal logger as a warning, since the reopen itself worked.



96
97
98
99
100
101
# File 'lib/semantic_logger/appender/udp.rb', line 96

def reopen
  socket     = connected_socket
  old_socket = @socket
  @socket    = socket
  close_socket(old_socket, replaced: true)
end