yell-adapters-gelf 0.4.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+
3
+ script: "rake"
4
+
5
+ rvm:
6
+ - 1.8.7
7
+ - 1.9.2
8
+ - 1.9.3
9
+ - jruby-18mode
10
+ - jruby-19mode
11
+ - rbx-18mode
12
+ # - rbx-19mode
13
+ - ree
14
+
15
+ notifications:
16
+ email:
17
+ - me@rudionrails.com
18
+
data/README.md CHANGED
@@ -7,6 +7,11 @@ directly into the Yell wiki at https://github.com/rudionrails/yell/wiki.
7
7
  Just in case you wonder: GELF means Graylog Extended Log Format. Read all
8
8
  it at http://www.graylog2.org/about/gelf.
9
9
 
10
+ [![Build Status](https://secure.travis-ci.org/rudionrails/yell-adapters-gelf.png?branch=master)](http://travis-ci.org/rudionrails/yell-adapters-gelf)
11
+
12
+ The Graylog adapter for Yell works and is tested with ruby 1.8.7, 1.9.x, jruby 1.8 and 1.9 mode, rubinius 1.8 and 1.9 as well as ree.
13
+
14
+
10
15
  ## Installation
11
16
 
12
17
  System wide:
@@ -36,8 +41,8 @@ logger.info "Hello World"
36
41
  Or alternatively with the block syntax:
37
42
 
38
43
  ```ruby
39
- logger = Yell.new do
40
- adapter :gelf
44
+ logger = Yell.new do |l|
45
+ l.adapter :gelf
41
46
  end
42
47
 
43
48
  logger.info 'Hello World!'
@@ -50,13 +55,12 @@ options to the adapter:
50
55
  logger = Yell.new :gelf, :host => 'hostname', :port => 1234
51
56
 
52
57
  # Or with the block syntax
53
- logger = Yell.new do
54
- adapter :gelf, :host => 'hostname', :port => 1234
58
+ logger = Yell.new do |l|
59
+ l.adapter :gelf, :host => 'hostname', :port => 1234
55
60
  end
56
61
 
57
62
  logger.info 'Hello World!'
58
63
  ```
59
64
 
60
-
61
- Copyright © 2011-2012 Rudolf Schmidt, released under the MIT license
65
+ Copyright © 2012 Rudolf Schmidt, released under the MIT license
62
66
 
data/Rakefile CHANGED
@@ -1 +1,22 @@
1
- require "bundler/gem_tasks"
1
+ $:.unshift( 'lib' )
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ # === RSpec
7
+ begin
8
+ require 'rspec/core/rake_task'
9
+
10
+ desc "Run specs"
11
+ RSpec::Core::RakeTask.new do |t|
12
+ t.rspec_opts = %w(--color --format progress --order random)
13
+ t.ruby_opts = %w(-w)
14
+ end
15
+ rescue LoadError
16
+ task :spec do
17
+ abort "`gem install rspec` in order to run tests"
18
+ end
19
+ end
20
+
21
+ task :default => :spec
22
+
@@ -4,7 +4,7 @@ module Yell #:nodoc:
4
4
  module Adapters #:nodoc:
5
5
 
6
6
  class Gelf
7
- VERSION = "0.4.1"
7
+ VERSION = "0.6.0"
8
8
 
9
9
  end
10
10
 
@@ -1,20 +1,15 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'socket'
4
- require 'json'
5
- require 'zlib'
6
- require 'digest/md5'
7
-
8
3
  module Yell #:nodoc:
9
4
  module Adapters #:nodoc:
10
5
 
11
6
  # GELF for Graylog2.
12
7
  class Gelf < Yell::Adapters::Base
13
8
 
14
- # Syslog severities
9
+ # Graylog severities
15
10
  Severities = [7, 6, 4, 3, 2, 1]
16
11
 
17
- # Combines syslog severities with internal representation:
12
+ # Combines Graylog severities with internal representation:
18
13
  # 'DEBUG' => 7
19
14
  # 'INFO' => 6
20
15
  # 'WARN' => 4
@@ -48,71 +43,75 @@ module Yell #:nodoc:
48
43
  # Cycle host and port
49
44
  host = @hosts.shift
50
45
  @hosts << host
51
-
52
46
  host
53
47
  end
54
48
  end
55
49
 
56
- def initialize( options = {}, &block )
50
+
51
+ setup do |options|
57
52
  @uid = 0
58
53
 
59
- # initialize the UDP Sender
60
- @host = options.fetch(:host, 'localhost')
61
- @port = options.fetch(:port, 12201)
54
+ self.facility = options.fetch(:facility, 'yell')
62
55
 
63
- max_chunk_size options.fetch(:max_chunk_size, :wan)
56
+ # initialize the UDP Sender
57
+ self.host = options.fetch(:host, 'localhost')
58
+ self.port = options.fetch(:port, 12201)
64
59
 
65
- super( options, &block )
60
+ self.max_chunk_size = options.fetch(:max_chunk_size, :wan)
66
61
  end
67
62
 
63
+ write do |event|
64
+ # https://github.com/Graylog2/graylog2-docs/wiki/GELF
65
+ _datagrams = datagrams(
66
+ 'version' => '1.0',
68
67
 
69
- # The sender (UDP Socket)
70
- def sender
71
- @sender ||= Yell::Adapters::Gelf::Sender.new( [@host, @port] )
68
+ 'facility' => facility,
69
+ 'level' => SeverityMap[event.level],
70
+ 'short_message' => event.message,
71
+ 'timestamp' => event.time.to_f,
72
+ 'host' => event.hostname,
73
+
74
+ 'file' => event.file,
75
+ 'line' => event.line,
76
+ '_method' => event.method,
77
+ '_pid' => event.pid
78
+ )
79
+
80
+ sender.send( *_datagrams )
72
81
  end
73
82
 
74
- # Close the UDP sender
75
- def close
83
+ close do
76
84
  @sender.close if @sender.respond_to? :close
77
-
78
85
  @sender = nil
79
86
  end
80
87
 
81
- def max_chunk_size( val )
88
+
89
+ # Accessor to the Graylog host
90
+ attr_accessor :host
91
+
92
+ # Accessor to the Graylog port
93
+ attr_accessor :port
94
+
95
+ # Accessor to the Graylog facility
96
+ attr_accessor :facility
97
+
98
+ # Accessor to the Graylog chunk size
99
+ attr_reader :max_chunk_size
100
+
101
+ def max_chunk_size=( val )
82
102
  @max_chunk_size = case val
83
103
  when :wan then 1420
84
104
  when :lan then 8154
85
- else val
105
+ else val.to_i
86
106
  end
87
107
  end
88
108
 
89
109
 
90
110
  private
91
111
 
92
- def write!( event )
93
- # See https://github.com/Graylog2/graylog2-docs/wiki/GELF
94
- # for formatting options.
95
- _datagrams = datagrams(
96
- 'version' => '1.0',
97
- 'facility' => 'yell',
98
-
99
- 'level' => SeverityMap[event.level],
100
- 'short_message' => event.message,
101
- 'timestamp' => event.time.to_f,
102
- 'host' => Socket.gethostname,
103
-
104
- 'file' => event.file,
105
- 'line' => event.line,
106
- '_method' => event.method,
107
- '_pid' => Process.pid
108
- )
109
-
110
- sender.send( *_datagrams )
111
- rescue Exception => e
112
- close
113
-
114
- # re-raise the exception
115
- raise( e, caller )
112
+ # The sender (UDP Socket)
113
+ def sender
114
+ @sender ||= Yell::Adapters::Gelf::Sender.new( [@host, @port] )
116
115
  end
117
116
 
118
117
  def datagrams( data )
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- # Copyright (c) 2011-2012 Rudolf Schmidt
3
+ # Copyright (c) 2012 Rudolf Schmidt
4
4
  #
5
5
  # Permission is hereby granted, free of charge, to any person obtaining
6
6
  # a copy of this software and associated documentation files (the
@@ -21,15 +21,15 @@
21
21
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
 
24
- require 'yell'
25
-
26
- begin
27
- require 'yell/adapters/gelf'
28
- rescue LoadError
29
- $: << File.dirname(__FILE__)
30
- require 'yell/adapters/gelf'
31
- end
32
-
33
24
  module Yell #:nodoc:
34
25
  end
35
26
 
27
+ require 'socket'
28
+ require 'zlib'
29
+ require 'digest/md5'
30
+
31
+ require 'json'
32
+ require 'yell'
33
+
34
+ require File.dirname(__FILE__) + '/yell/adapters/gelf'
35
+
@@ -0,0 +1,13 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'yell-adapters-gelf'
5
+
6
+ require 'rspec'
7
+ require 'rr'
8
+
9
+ RSpec.configure do |config|
10
+ config.mock_framework = :rr
11
+
12
+ end
13
+
@@ -0,0 +1,169 @@
1
+ require 'spec_helper'
2
+
3
+ describe Yell::Adapters::Gelf do
4
+
5
+ module SenderStub
6
+ extend self
7
+
8
+ def datagrams; @datagrams; end
9
+
10
+ def send( *datagrams )
11
+ @datagrams = datagrams
12
+ end
13
+ end
14
+
15
+ before do
16
+ stub( Yell::Adapters::Gelf::Sender ).new( anything ) { SenderStub }
17
+ end
18
+
19
+ context "a new Yell::Adapters::Gelf instance" do
20
+ subject { Yell::Adapters::Gelf.new }
21
+
22
+ it { subject.host.should == 'localhost' }
23
+ it { subject.port.should == 12201 }
24
+ it { subject.facility.should == 'yell' }
25
+ it { subject.max_chunk_size.should == 1420 }
26
+ end
27
+
28
+ context :host do
29
+ let( :adapter ) { Yell::Adapters::Gelf.new }
30
+ subject { adapter.host }
31
+
32
+ before { adapter.host = 'hostname' }
33
+
34
+ it { should == 'hostname' }
35
+ end
36
+
37
+ context :port do
38
+ let( :adapter ) { Yell::Adapters::Gelf.new }
39
+ subject { adapter.port }
40
+
41
+ before { adapter.port = 1234 }
42
+
43
+ it { should == 1234 }
44
+ end
45
+
46
+ context :max_chunk_size do
47
+ let( :adapter ) { Yell::Adapters::Gelf.new }
48
+ subject { adapter.max_chunk_size }
49
+
50
+ context :wan do
51
+ before { adapter.max_chunk_size = :wan }
52
+ it { should == 1420 }
53
+ end
54
+
55
+ context :lan do
56
+ before { adapter.max_chunk_size = :lan }
57
+ it { should == 8154 }
58
+ end
59
+
60
+ context :other do
61
+ before { adapter.max_chunk_size = "1234" }
62
+ it { should == 1234 }
63
+ end
64
+ end
65
+
66
+ context :write do
67
+ let( :event ) { Yell::Event.new( 'INFO', 'Hello World' ) }
68
+ let( :adapter ) { Yell::Adapters::Gelf.new }
69
+
70
+ context "single" do
71
+ let( :datagrams ) { SenderStub.datagrams }
72
+
73
+ before do
74
+ deflated = Zlib::Deflate.deflate( "*" * 1441000 ) # compresses to 1420 bytes
75
+ mock( Zlib::Deflate ).deflate( anything ) { deflated }
76
+
77
+ adapter.write event
78
+ end
79
+
80
+ it "should be one part" do
81
+ datagrams.size.should == 1
82
+ end
83
+
84
+ it "should be a string" do
85
+ datagrams[0].should be_kind_of String
86
+ end
87
+
88
+ it "should be zipped" do
89
+ datagrams[0][0..1].should == "\x78\x9c" # zlib header
90
+ end
91
+ end
92
+
93
+ context "chunked" do
94
+ let( :datagrams ) { SenderStub.datagrams }
95
+
96
+ before do
97
+ deflated = Zlib::Deflate.deflate( "*" * 1442000 ) # compresses to 1421 bytes
98
+ mock( Zlib::Deflate ).deflate( anything ) { deflated }
99
+
100
+ adapter.write event
101
+ end
102
+
103
+ it "should be multiple parts" do
104
+ datagrams.size.should == 2
105
+ end
106
+
107
+ it "should be multiple strings" do
108
+ datagrams.each { |datagram| datagram.should be_kind_of String }
109
+ end
110
+
111
+ it "should be multiple GELF chunks" do
112
+ # datagram assertions mostly taken from original gelf-rb gem
113
+ datagrams.each_with_index do |datagram, index|
114
+ datagram[0..1].should == "\x1e\x0f" # GELF header
115
+
116
+ # datagram[2..9] is unique the message id
117
+ datagram[10].ord.should == index # chunk number
118
+ datagram[11].ord.should == datagrams.size # total chunk number
119
+ end
120
+ end
121
+ end
122
+
123
+ context :datagrams do
124
+ after { adapter.write event }
125
+
126
+ it "should receive :version" do
127
+ mock.proxy( adapter ).datagrams( hash_including('version' => '1.0') )
128
+ end
129
+
130
+ it "should receive :facility" do
131
+ mock.proxy( adapter ).datagrams( hash_including('facility' => adapter.facility) )
132
+ end
133
+
134
+ it "should receive :level" do
135
+ mock.proxy( adapter ).datagrams( hash_including('level' => Yell::Adapters::Gelf::SeverityMap[event.level]) )
136
+ end
137
+
138
+ it "should receive :short_message" do
139
+ mock.proxy( adapter ).datagrams( hash_including('short_message' => event.message) )
140
+ end
141
+
142
+ it "should receive :timestamp" do
143
+ mock.proxy( adapter ).datagrams( hash_including('timestamp' => event.time.to_f) )
144
+ end
145
+
146
+ it "should receive :host" do
147
+ mock.proxy( adapter ).datagrams( hash_including('host' => event.hostname) )
148
+ end
149
+
150
+ it "should receive :file" do
151
+ mock.proxy( adapter ).datagrams( hash_including('file' => event.file) )
152
+ end
153
+
154
+ it "should receive :line" do
155
+ mock.proxy( adapter ).datagrams( hash_including('line' => event.line) )
156
+ end
157
+
158
+ it "should receive :method" do
159
+ mock.proxy( adapter ).datagrams( hash_including('_method' => event.method) )
160
+ end
161
+
162
+ it "should receive :pid" do
163
+ mock.proxy( adapter ).datagrams( hash_including('_pid' => event.pid) )
164
+ end
165
+ end
166
+ end
167
+
168
+ end
169
+
@@ -20,7 +20,9 @@ Gem::Specification.new do |s|
20
20
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
21
  s.require_paths = ["lib"]
22
22
 
23
- # specify any dependencies here; for example:
24
- # s.add_development_dependency "rspec"
25
- s.add_runtime_dependency "yell", "~> 0.4"
23
+ s.add_runtime_dependency "yell", "~> 0.6"
24
+ s.add_runtime_dependency "json"
25
+
26
+ s.add_development_dependency "rspec"
27
+ s.add_development_dependency "rr"
26
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yell-adapters-gelf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,52 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-30 00:00:00.000000000 Z
12
+ date: 2012-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yell
16
- requirement: &70330052059680 !ruby/object:Gem::Requirement
16
+ requirement: &70278652183180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0.4'
21
+ version: '0.6'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70330052059680
24
+ version_requirements: *70278652183180
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &70278652182780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70278652182780
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70278652182320 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70278652182320
47
+ - !ruby/object:Gem::Dependency
48
+ name: rr
49
+ requirement: &70278652181900 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70278652181900
25
58
  description: Graylog2 adapter for Yell
26
59
  email:
27
60
  executables: []
@@ -29,6 +62,7 @@ extensions: []
29
62
  extra_rdoc_files: []
30
63
  files:
31
64
  - .gitignore
65
+ - .travis.yml
32
66
  - Gemfile
33
67
  - LICENSE.txt
34
68
  - README.md
@@ -36,6 +70,8 @@ files:
36
70
  - lib/yell-adapters-gelf.rb
37
71
  - lib/yell/adapters/gelf.rb
38
72
  - lib/yell/adapters/gelf/version.rb
73
+ - spec/spec_helper.rb
74
+ - spec/yell/adapters/gelf_spec.rb
39
75
  - yell-adapters-gelf.gemspec
40
76
  homepage: http://rubygems.org/gems/yell
41
77
  licenses: []
@@ -61,5 +97,7 @@ rubygems_version: 1.8.17
61
97
  signing_key:
62
98
  specification_version: 3
63
99
  summary: Yell - Your Extensible Logging Library
64
- test_files: []
100
+ test_files:
101
+ - spec/spec_helper.rb
102
+ - spec/yell/adapters/gelf_spec.rb
65
103
  has_rdoc: