Permalink
Browse files

Allow to redefine level/severity mapping.

  • Loading branch information...
1 parent 5d9f70c commit bb1f4a95549f44981ff4a4caee0c43d75585e4ae @AlekSi AlekSi committed May 6, 2011
Showing with 35 additions and 10 deletions.
  1. +16 −2 lib/gelf/notifier.rb
  2. +13 −4 lib/gelf/severity.rb
  3. +4 −2 test/test_notifier.rb
  4. +2 −2 test/test_severity.rb
View
@@ -7,7 +7,7 @@ class << self
end
attr_accessor :host, :port, :enabled
- attr_reader :max_chunk_size, :level, :default_options
+ attr_reader :max_chunk_size, :level, :default_options, :level_mapping
# +host+ and +port+ are host/ip and port of graylog2-server.
# +max_size+ is passed to max_chunk_size=.
@@ -26,6 +26,7 @@ def initialize(host = 'localhost', port = 12201, max_size = 'WAN', default_optio
self.default_options['facility'] ||= 'gelf-rb'
@sender = RubyUdpSender.new(host, port)
+ self.level_mapping = :logger
end
# +size+ may be a number of bytes, 'WAN' (1420 bytes) or 'LAN' (8154).
@@ -53,6 +54,19 @@ def default_options=(options)
@default_options = self.class.stringify_keys(options)
end
+ # +mapping+ may be a hash, 'logger' (GELF::LOGGER_MAPPING) or 'direct' (GELF::DIRECT_MAPPING).
+ # Default (compatible) value is 'logger'.
+ def level_mapping=(mapping)
+ case mapping.to_s.downcase
+ when 'logger'
+ @level_mapping = GELF::LOGGER_MAPPING
+ when 'direct'
+ @level_mapping = GELF::DIRECT_MAPPING
+ else
+ @level_mapping = mapping
+ end
+ end
+
def disable
@enabled = false
end
@@ -186,7 +200,7 @@ def datagrams_from_hash
def serialize_hash
raise ArgumentError.new("Hash is empty.") if @hash.nil? || @hash.empty?
- @hash['level'] = GELF::LEVELS_MAPPING[@hash['level']]
+ @hash['level'] = @level_mapping[@hash['level']]
Zlib::Deflate.deflate(@hash.to_json).bytes
end
View
@@ -1,11 +1,11 @@
module GELF
- # There are two things you should know about log leves/severity:
+ # There are two things you should know about log levels/severity:
# - syslog defines levels from 0 (Emergency) to 7 (Debug).
# 0 (Emergency) and 1 (Alert) levels are reserved for OS kernel.
# - Ruby default Logger defines levels from 0 (DEBUG) to 4 (FATAL) and 5 (UNKNOWN).
# Note that order is inverted.
# For compatibility we define our constants as Ruby Logger, and convert values before
- # generating GELF message.
+ # generating GELF message, using defined mapping.
module Levels
DEBUG = 0
@@ -18,11 +18,20 @@ module Levels
include Levels
- # Maps Ruby Logger levels to syslog levels as SyslogLogger and syslogger gems.
- LEVELS_MAPPING = {DEBUG => 7, # Debug
+ # Maps Ruby Logger levels to syslog levels as SyslogLogger and syslogger gems. This one is default.
+ LOGGER_MAPPING = {DEBUG => 7, # Debug
INFO => 6, # Info
WARN => 5, # Notice
ERROR => 4, # Warning
FATAL => 3, # Error
UNKNOWN => 1} # Alert – shouldn't be used
+
+ # Maps Ruby Logger levels to syslog levels as is.
+ DIRECT_MAPPING = {DEBUG => 7, # Debug
+ INFO => 6, # Info
+ # skip 5 Notice
+ WARN => 4, # Warning
+ ERROR => 3, # Error
+ FATAL => 2, # Critical
+ UNKNOWN => 1} # Alert – shouldn't be used
end
View
@@ -121,16 +121,18 @@ class TestNotifier < Test::Unit::TestCase
context "serialize_hash" do
setup do
+ @notifier.level_mapping = :direct
@notifier.instance_variable_set('@hash', { 'level' => GELF::WARN, 'field' => 'value' })
@data = @notifier.__send__(:serialize_hash)
assert @data.respond_to?(:each)
@deserialized_hash = JSON.parse(Zlib::Inflate.inflate(@data.to_a.pack('C*')))
assert_instance_of Hash, @deserialized_hash
end
- should "map level" do
+ should "map level using mapping" do
assert_not_equal GELF::WARN, @deserialized_hash['level']
- assert_equal GELF::LEVELS_MAPPING[GELF::WARN], @deserialized_hash['level']
+ assert_not_equal GELF::LOGGER_MAPPING[GELF::WARN], @deserialized_hash['level']
+ assert_equal GELF::DIRECT_MAPPING[GELF::WARN], @deserialized_hash['level']
end
end
View
@@ -1,8 +1,8 @@
require 'helper'
class TestSeverity < Test::Unit::TestCase
- should "map Ruby Logger levels to syslog levels" do
- GELF::LEVELS_MAPPING.each do |ruby_level, syslog_level|
+ should "map Ruby Logger levels to syslog levels as SyslogLogger" do
+ GELF::LOGGER_MAPPING.each do |ruby_level, syslog_level|
assert_not_equal syslog_level, ruby_level
end
end

0 comments on commit bb1f4a9

Please sign in to comment.