Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Allow to redefine level/severity mapping.

  • Loading branch information...
commit bb1f4a95549f44981ff4a4caee0c43d75585e4ae 1 parent 5d9f70c
Alexey Palazhchenko authored May 06, 2011
18  lib/gelf/notifier.rb
@@ -7,7 +7,7 @@ class << self
7 7
     end
8 8
 
9 9
     attr_accessor :host, :port, :enabled
10  
-    attr_reader :max_chunk_size, :level, :default_options
  10
+    attr_reader :max_chunk_size, :level, :default_options, :level_mapping
11 11
 
12 12
     # +host+ and +port+ are host/ip and port of graylog2-server.
13 13
     # +max_size+ is passed to max_chunk_size=.
@@ -26,6 +26,7 @@ def initialize(host = 'localhost', port = 12201, max_size = 'WAN', default_optio
26 26
       self.default_options['facility'] ||= 'gelf-rb'
27 27
 
28 28
       @sender = RubyUdpSender.new(host, port)
  29
+      self.level_mapping = :logger
29 30
     end
30 31
 
31 32
     # +size+ may be a number of bytes, 'WAN' (1420 bytes) or 'LAN' (8154).
@@ -53,6 +54,19 @@ def default_options=(options)
53 54
       @default_options = self.class.stringify_keys(options)
54 55
     end
55 56
 
  57
+    # +mapping+ may be a hash, 'logger' (GELF::LOGGER_MAPPING) or 'direct' (GELF::DIRECT_MAPPING).
  58
+    # Default (compatible) value is 'logger'.
  59
+    def level_mapping=(mapping)
  60
+      case mapping.to_s.downcase
  61
+        when 'logger'
  62
+          @level_mapping = GELF::LOGGER_MAPPING
  63
+        when 'direct'
  64
+          @level_mapping = GELF::DIRECT_MAPPING
  65
+        else
  66
+          @level_mapping = mapping
  67
+      end
  68
+    end
  69
+
56 70
     def disable
57 71
       @enabled = false
58 72
     end
@@ -186,7 +200,7 @@ def datagrams_from_hash
186 200
     def serialize_hash
187 201
       raise ArgumentError.new("Hash is empty.") if @hash.nil? || @hash.empty?
188 202
 
189  
-      @hash['level'] = GELF::LEVELS_MAPPING[@hash['level']]
  203
+      @hash['level'] = @level_mapping[@hash['level']]
190 204
 
191 205
       Zlib::Deflate.deflate(@hash.to_json).bytes
192 206
     end
17  lib/gelf/severity.rb
... ...
@@ -1,11 +1,11 @@
1 1
 module GELF
2  
-  # There are two things you should know about log leves/severity:
  2
+  # There are two things you should know about log levels/severity:
3 3
   #  - syslog defines levels from 0 (Emergency) to 7 (Debug).
4 4
   #    0 (Emergency) and 1 (Alert) levels are reserved for OS kernel.
5 5
   #  - Ruby default Logger defines levels from 0 (DEBUG) to 4 (FATAL) and 5 (UNKNOWN).
6 6
   #    Note that order is inverted.
7 7
   # For compatibility we define our constants as Ruby Logger, and convert values before
8  
-  # generating GELF message.
  8
+  # generating GELF message, using defined mapping.
9 9
 
10 10
   module Levels
11 11
     DEBUG   = 0
@@ -18,11 +18,20 @@ module Levels
18 18
 
19 19
   include Levels
20 20
 
21  
-  # Maps Ruby Logger levels to syslog levels as SyslogLogger and syslogger gems.
22  
-  LEVELS_MAPPING = {DEBUG   => 7, # Debug
  21
+  # Maps Ruby Logger levels to syslog levels as SyslogLogger and syslogger gems. This one is default.
  22
+  LOGGER_MAPPING = {DEBUG   => 7, # Debug
23 23
                     INFO    => 6, # Info
24 24
                     WARN    => 5, # Notice
25 25
                     ERROR   => 4, # Warning
26 26
                     FATAL   => 3, # Error
27 27
                     UNKNOWN => 1} # Alert – shouldn't be used
  28
+
  29
+  # Maps Ruby Logger levels to syslog levels as is.
  30
+  DIRECT_MAPPING = {DEBUG   => 7, # Debug
  31
+                    INFO    => 6, # Info
  32
+                    # skip 5 Notice
  33
+                    WARN    => 4, # Warning
  34
+                    ERROR   => 3, # Error
  35
+                    FATAL   => 2, # Critical
  36
+                    UNKNOWN => 1} # Alert – shouldn't be used
28 37
 end
6  test/test_notifier.rb
@@ -121,6 +121,7 @@ class TestNotifier < Test::Unit::TestCase
121 121
 
122 122
     context "serialize_hash" do
123 123
       setup do
  124
+        @notifier.level_mapping = :direct
124 125
         @notifier.instance_variable_set('@hash', { 'level' => GELF::WARN, 'field' => 'value' })
125 126
         @data = @notifier.__send__(:serialize_hash)
126 127
         assert @data.respond_to?(:each)
@@ -128,9 +129,10 @@ class TestNotifier < Test::Unit::TestCase
128 129
         assert_instance_of Hash, @deserialized_hash
129 130
       end
130 131
 
131  
-      should "map level" do
  132
+      should "map level using mapping" do
132 133
         assert_not_equal GELF::WARN, @deserialized_hash['level']
133  
-        assert_equal GELF::LEVELS_MAPPING[GELF::WARN], @deserialized_hash['level']
  134
+        assert_not_equal GELF::LOGGER_MAPPING[GELF::WARN], @deserialized_hash['level']
  135
+        assert_equal GELF::DIRECT_MAPPING[GELF::WARN], @deserialized_hash['level']
134 136
       end
135 137
     end
136 138
 
4  test/test_severity.rb
... ...
@@ -1,8 +1,8 @@
1 1
 require 'helper'
2 2
 
3 3
 class TestSeverity < Test::Unit::TestCase
4  
-  should "map Ruby Logger levels to syslog levels" do
5  
-    GELF::LEVELS_MAPPING.each do |ruby_level, syslog_level|
  4
+  should "map Ruby Logger levels to syslog levels as SyslogLogger" do
  5
+    GELF::LOGGER_MAPPING.each do |ruby_level, syslog_level|
6 6
       assert_not_equal syslog_level, ruby_level
7 7
     end
8 8
   end

0 notes on commit bb1f4a9

Please sign in to comment.
Something went wrong with that request. Please try again.