Class: SemanticLogger::Appender::Graylog

Inherits:
Subscriber show all
Defined in:
lib/semantic_logger/appender/graylog.rb

Defined Under Namespace

Classes: LevelMap

Instance Attribute Summary collapse

Attributes inherited from Subscriber

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

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #flush, #level, #should_log?

Methods inherited from Base

#backtrace, #fast_tag, #level, #level=, #measure, #payload, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags, #with_payload

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 options. 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 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.

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

application: [String]
  Name of this application to appear in log messages.
  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
  @gelf_options = gelf_options
  @level_map    = level_map.is_a?(LevelMap) ? level_map : LevelMap.new(level_map)

  super(**args, &block)
  reopen
end

Instance Attribute Details

#gelf_optionsObject

Returns the value of attribute gelf_options.



42
43
44
# File 'lib/semantic_logger/appender/graylog.rb', line 42

def gelf_options
  @gelf_options
end

#level_mapObject

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_sizeObject

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

#notifierObject (readonly)

Returns the value of attribute notifier.



43
44
45
# File 'lib/semantic_logger/appender/graylog.rb', line 43

def notifier
  @notifier
end

#portObject (readonly)

Returns the value of attribute port.



43
44
45
# File 'lib/semantic_logger/appender/graylog.rb', line 43

def port
  @port
end

#protocolObject (readonly)

Returns the value of attribute protocol.



43
44
45
# File 'lib/semantic_logger/appender/graylog.rb', line 43

def protocol
  @protocol
end

#serverObject (readonly)

Returns the value of attribute server.



43
44
45
# File 'lib/semantic_logger/appender/graylog.rb', line 43

def server
  @server
end

#urlObject

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



118
119
120
121
122
123
124
125
126
# File 'lib/semantic_logger/appender/graylog.rb', line 118

def call(log, logger)
  h = default_formatter.call(log, logger)

  h[:short_message] = h.delete(:message) || log.exception.message
  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



129
130
131
132
# File 'lib/semantic_logger/appender/graylog.rb', line 129

def log(log)
  notifier.notify!(formatter.call(log, self))
  true
end

#reopenObject

Re-open after process fork

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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

  raise(ArgumentError, "Invalid protocol value: #{@protocol}. Must be :udp or :tcp") unless %i[udp tcp].include?(@protocol)

  gelf_options[:protocol] ||= (@protocol == :tcp ? GELF::Protocol::TCP : GELF::Protocol::UDP)
  gelf_options[:facility] ||= application

  @notifier                       = GELF::Notifier.new(server, port, max_size, gelf_options)
  @notifier.collect_file_and_line = false
end