class SemanticLogger::Appender::Udp

UDP log appender.

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

Example:

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

Attributes

server[RW]
socket[R]
udp_flags[RW]

Public Class Methods

new(server:, udp_flags: 0, metrics: true, **args, &block) click to toggle source

Create UDP log appender.

server: [String]
  URL of the server to write UDP messages to.

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: SemanticLogger.default_level

formatter: [Object|Proc]
  An instance of a class that implements #call, or a Proc to be used to format
  the output from this appender
  Default: Use the built-in formatter (See: #call)

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

Limitations:

Example:

SemanticLogger.add_appender(
  appender: :udp,
  server:   'server:3300'
)
Calls superclass method SemanticLogger::Subscriber::new
# File lib/semantic_logger/appender/udp.rb, line 64
def initialize(server:, udp_flags: 0, metrics: true, **args, &block)
  @server    = server
  @udp_flags = udp_flags

  super(metrics: metrics, **args, &block)
  reopen
end

Public Instance Methods

close() click to toggle source

Close is called during shutdown, or with reopen

# File lib/semantic_logger/appender/udp.rb, line 93
def close
  @socket&.close
end
flush() click to toggle source

Flush is called by the semantic_logger during shutdown.

# File lib/semantic_logger/appender/udp.rb, line 88
def flush
  @socket&.flush
end
log(log) click to toggle source

Write the log using the specified protocol and server.

# File lib/semantic_logger/appender/udp.rb, line 82
def log(log)
  @socket.send(formatter.call(log, self), udp_flags)
  true
end
reopen() click to toggle source

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

# File lib/semantic_logger/appender/udp.rb, line 74
def reopen
  close
  @socket    = UDPSocket.new
  host, port = server.split(":")
  @socket.connect(host, port.to_i)
end

Private Instance Methods

default_formatter() click to toggle source

Returns [SemanticLogger::Formatters::Default] formatter default for this Appender

# File lib/semantic_logger/appender/udp.rb, line 100
def default_formatter
  SemanticLogger::Formatters::Json.new
end