Class: SemanticLogger::Appender::Graylog
- Inherits:
-
Subscriber
- Object
- Base
- Subscriber
- SemanticLogger::Appender::Graylog
- Defined in:
- lib/semantic_logger/appender/graylog.rb
Defined Under Namespace
Classes: LevelMap
Instance Attribute Summary collapse
-
#gelf_options ⇒ Object
Returns the value of attribute gelf_options.
-
#level_map ⇒ Object
Returns the value of attribute level_map.
-
#max_size ⇒ Object
Returns the value of attribute max_size.
-
#notifier ⇒ Object
readonly
Returns the value of attribute notifier.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#url ⇒ Object
Returns the value of attribute url.
Attributes inherited from Subscriber
#application, #environment, #formatter, #host, #logger, #metrics
Attributes inherited from Base
Instance Method Summary collapse
-
#call(log, logger) ⇒ Object
Returns [Hash] of parameters to send.
-
#initialize(url: "udp://localhost:12201", max_size: "WAN", gelf_options: {}, level_map: LevelMap.new, **args, &block) ⇒ Graylog
constructor
Create Graylog log appender.
-
#log(log) ⇒ Object
Forward log messages.
-
#reopen ⇒ Object
Re-open after process fork.
Methods inherited from Subscriber
#close, #console_output?, #flush, #level, #should_log?
Methods inherited from Base
#backtrace, #fast_tag, #level, #level=, #measure, #named_tags, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags
Constructor Details
#initialize(url: "udp://localhost:12201", max_size: "WAN", gelf_options: {}, level_map: LevelMap.new, **args, &block) ⇒ Graylog
Create Graylog log appender.
Options:
url: [String]
Valid URL to post to.
Log to UDP Example:
'udp://localhost:12201'
Log to TCP Example:
'tcp://localhost:12201'
Default: 'udp://localhost:12201'
max_size: [String]
Max udp packet size. Ignored when protocol is :tcp
Default: "WAN"
gelf_options: [Hash]
Custom gelf . See Graylog documentation.
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 where the class name matches the supplied.
regular expression. All other will be ignored.
Proc: Only include log where the supplied Proc returns true
The Proc must return true or false.
host: [String]
Name of this host to appear in log .
Default: SemanticLogger.host
application: [String]
Name of this application to appear in log .
Default: SemanticLogger.application
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/semantic_logger/appender/graylog.rb', line 85 def initialize(url: "udp://localhost:12201", max_size: "WAN", gelf_options: {}, level_map: LevelMap.new, **args, &block) @url = url @max_size = max_size = @level_map = level_map.is_a?(LevelMap) ? level_map : LevelMap.new(level_map) super(**args, &block) reopen end |
Instance Attribute Details
#gelf_options ⇒ Object
Returns the value of attribute gelf_options.
42 43 44 |
# File 'lib/semantic_logger/appender/graylog.rb', line 42 def end |
#level_map ⇒ Object
Returns the value of attribute level_map.
42 43 44 |
# File 'lib/semantic_logger/appender/graylog.rb', line 42 def level_map @level_map end |
#max_size ⇒ Object
Returns the value of attribute max_size.
42 43 44 |
# File 'lib/semantic_logger/appender/graylog.rb', line 42 def max_size @max_size end |
#notifier ⇒ Object (readonly)
Returns the value of attribute notifier.
43 44 45 |
# File 'lib/semantic_logger/appender/graylog.rb', line 43 def notifier @notifier end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
43 44 45 |
# File 'lib/semantic_logger/appender/graylog.rb', line 43 def port @port end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
43 44 45 |
# File 'lib/semantic_logger/appender/graylog.rb', line 43 def protocol @protocol end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
43 44 45 |
# File 'lib/semantic_logger/appender/graylog.rb', line 43 def server @server end |
#url ⇒ Object
Returns the value of attribute url.
42 43 44 |
# File 'lib/semantic_logger/appender/graylog.rb', line 42 def url @url end |
Instance Method Details
#call(log, logger) ⇒ Object
Returns [Hash] of parameters to send
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/semantic_logger/appender/graylog.rb', line 120 def call(log, logger) h = default_formatter.call(log, logger) h[:short_message] = h.delete(:message) if h[:short_message].nil? h[:short_message] = log.exception.nil? ? "<no-exception-message>" : log.exception. end h[:level] = logger.level_map[log.level] h[:level_str] = log.level.to_s h[:duration_str] = h.delete(:duration) h end |
#log(log) ⇒ Object
Forward log messages
134 135 136 137 |
# File 'lib/semantic_logger/appender/graylog.rb', line 134 def log(log) notifier.notify!(formatter.call(log, self)) true end |
#reopen ⇒ Object
Re-open after process fork
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/semantic_logger/appender/graylog.rb', line 102 def reopen uri = URI.parse(@url) @server = uri.host @port = uri.port @protocol = uri.scheme.to_sym unless i[udp tcp].include?(@protocol) raise(ArgumentError, "Invalid protocol value: #{@protocol}. Must be :udp or :tcp") end [:protocol] ||= (@protocol == :tcp ? GELF::Protocol::TCP : GELF::Protocol::UDP) [:facility] ||= application @notifier = GELF::Notifier.new(server, port, max_size, ) @notifier.collect_file_and_line = false end |