Class: SemanticLogger::Appender::Udp
- Inherits:
-
Subscriber
- Object
- Base
- Subscriber
- SemanticLogger::Appender::Udp
- 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',
)
Instance Attribute Summary collapse
-
#server ⇒ Object
Returns the value of attribute server.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#udp_flags ⇒ Object
Returns the value of attribute udp_flags.
Attributes inherited from Subscriber
#application, #formatter, #host
Attributes inherited from Base
Instance Method Summary collapse
-
#close ⇒ Object
Close is called during shutdown, or with reopen.
-
#flush ⇒ Object
Flush is called by the semantic_logger during shutdown.
-
#initialize(options = {}, &block) ⇒ Udp
constructor
Create UDP log appender.
-
#log(log) ⇒ Object
Write the log using the specified protocol and server.
-
#reopen ⇒ Object
After forking an active process call #reopen to re-open open the handles to resources.
Methods inherited from Subscriber
Methods inherited from Base
#fast_tag, #level, #level=, #measure, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload
Constructor Details
#initialize(options = {}, &block) ⇒ Udp
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.
Limitations:
-
UDP packet size is limited by the connected network and any routers etc that the message has to traverse. See en.wikipedia.org/wiki/Maximum_transmission_unit
Example:
SemanticLogger.add_appender(
appender: :udp,
server: 'server:3300'
)
60 61 62 63 64 65 66 67 68 |
# File 'lib/semantic_logger/appender/udp.rb', line 60 def initialize( = {}, &block) = .dup @server = .delete(:server) @udp_flags = .delete(:udp_flags) || 0 raise(ArgumentError, 'Missing mandatory argument: :server') unless @server super(, &block) reopen end |
Instance Attribute Details
#server ⇒ Object
Returns the value of attribute server.
15 16 17 |
# File 'lib/semantic_logger/appender/udp.rb', line 15 def server @server end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
16 17 18 |
# File 'lib/semantic_logger/appender/udp.rb', line 16 def socket @socket end |
#udp_flags ⇒ Object
Returns the value of attribute udp_flags.
15 16 17 |
# File 'lib/semantic_logger/appender/udp.rb', line 15 def udp_flags @udp_flags end |
Instance Method Details
#close ⇒ Object
Close is called during shutdown, or with reopen
93 94 95 |
# File 'lib/semantic_logger/appender/udp.rb', line 93 def close @socket.close if @socket end |
#flush ⇒ Object
Flush is called by the semantic_logger during shutdown.
88 89 90 |
# File 'lib/semantic_logger/appender/udp.rb', line 88 def flush @socket.flush if @socket end |
#log(log) ⇒ Object
Write the log using the specified protocol and server.
80 81 82 83 84 85 |
# File 'lib/semantic_logger/appender/udp.rb', line 80 def log(log) return false unless should_log?(log) @socket.send(formatter.call(log, self), udp_flags) true end |
#reopen ⇒ Object
After forking an active process call #reopen to re-open open the handles to resources
72 73 74 75 76 77 |
# File 'lib/semantic_logger/appender/udp.rb', line 72 def reopen close @socket = UDPSocket.new host, port = server.split(':') @socket.connect(host, port.to_i) end |