logglier 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,6 +35,8 @@ module Logglier
35
35
  @headers = {}
36
36
  if @format == :json
37
37
  @headers['Content-Type'] = 'application/json'
38
+ else
39
+ @headers['Content-Type'] = 'text/plain'
38
40
  end
39
41
 
40
42
  connect!
@@ -4,7 +4,34 @@ module Logglier
4
4
  module Client
5
5
  class HTTP
6
6
 
7
- # Used by the Threaded client to hold a queue, deliver messsages from it
7
+ # Used by the Threaded client to manage the delivery thread
8
+ # recreating it if is lost due to a fork.
9
+ #
10
+ class DeliveryThreadManager
11
+ def initialize(input_uri, opts={})
12
+ @input_uri, @opts = input_uri, opts
13
+ start_thread
14
+ end
15
+
16
+ # Pushes a message to the delivery thread, starting one if necessary
17
+ def deliver(message)
18
+ start_thread unless @thread.alive?
19
+ @thread.deliver(message)
20
+ #Race condition? Sometimes we need to rescue this and start a new thread
21
+ rescue NoMethodError
22
+ @thread.kill #Try not to leak threads, should already be dead anyway
23
+ start_thread
24
+ retry
25
+ end
26
+
27
+ private
28
+
29
+ def start_thread
30
+ @thread = DeliveryThread.new(@input_uri, @opts)
31
+ end
32
+ end
33
+
34
+ # Used by the DeliveryThreadManager to hold a queue, deliver messsages from it
8
35
  # and to ensure it's flushed on program exit.
9
36
  #
10
37
  # @note Uses NetHTTPProxy
@@ -19,7 +46,6 @@ module Logglier
19
46
  # @note See NetHTTPProxy for further option processing of opts
20
47
  # @note registers an at_exit handler that signals exit intent and joins the thread.
21
48
  def initialize(input_uri, opts={})
22
-
23
49
  @input_uri = input_uri
24
50
 
25
51
  opts[:read_timeout] = opts[:read_timeout] || 120
@@ -14,7 +14,7 @@ module Logglier
14
14
  setup_input_uri(opts)
15
15
  @format = opts[:format] ? opts[:format].to_sym : nil
16
16
  @deliverer = if opts[:threaded]
17
- Logglier::Client::HTTP::DeliveryThread.new(@input_uri, opts)
17
+ Logglier::Client::HTTP::DeliveryThreadManager.new(@input_uri, opts)
18
18
  else
19
19
  Logglier::Client::HTTP::NetHTTPProxy.new(@input_uri, opts)
20
20
  end
@@ -60,7 +60,7 @@ module Logglier
60
60
  # Syslog specific PRI calculation.
61
61
  # See RFC3164 4.1.1
62
62
  def pri(severity)
63
- @facility + case severity
63
+ severity_value = case severity
64
64
  when "FATAL"
65
65
  0
66
66
  when "ERROR"
@@ -72,6 +72,7 @@ module Logglier
72
72
  when "DEBUG"
73
73
  7
74
74
  end
75
+ (@facility << 3) + severity_value
75
76
  end
76
77
 
77
78
  # Generate a syslog compat message
@@ -14,7 +14,7 @@ module Logglier
14
14
  # @return [Logglier::Client::HTTP, Logglier::Client::Syslog] returns an instance of the Logglier Client class
15
15
  def self.new(input_url, opts={})
16
16
  unless input_url
17
- raise URLRequired.new
17
+ raise InputURLRequired.new
18
18
  end
19
19
 
20
20
  opts.merge!({ :input_url => input_url })
@@ -90,7 +90,7 @@ module Logglier
90
90
  begin
91
91
  @input_uri = URI.parse(@input_uri)
92
92
  rescue URI::InvalidURIError => e
93
- raise URLRequired.new("Invalid Input URL: #{@input_uri}")
93
+ raise InputURLRequired.new("Invalid Input URL: #{@input_uri}")
94
94
  end
95
95
  end
96
96
 
@@ -1,3 +1,3 @@
1
1
  module Logglier
2
- VERSION = '0.2.7'
2
+ VERSION = '0.2.8'
3
3
  end
data/logglier.gemspec CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.required_rubygems_version = '>= 1.3.6'
23
23
 
24
24
  s.add_development_dependency 'rake'
25
- s.add_development_dependency 'rspec', '~> 2.5.0'
25
+ s.add_development_dependency 'rspec', '~> 2.11.0'
26
26
  end
27
27
 
28
28
 
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Logglier::Client::Syslog do
4
+ describe "PRI calculation conforms to RFC3164 4.1.1" do
5
+ before do
6
+ UDPSocket.stub(:new).and_return(mock("socket").as_null_object)
7
+ TCPSocket.stub(:new).and_return(mock("Socket").as_null_object)
8
+ end
9
+ it "calculates a number with the lowest 3 bits representing the severity and the higher bits representing the facility" do
10
+ client = described_class.new(:input_url => "udp://127.0.0.1:514/17")
11
+ client.pri("WARN").should == (17 << 3) + 4
12
+ end
13
+ end
14
+ end
data/spec/http_spec.rb CHANGED
@@ -23,6 +23,11 @@ describe 'HTTP' do
23
23
  proxy.failsafe.should == $stderr
24
24
  end
25
25
 
26
+ it "defaults Content-Type header to text/plain" do
27
+ @http.should_receive(:request_post).with(anything(), anything(), hash_including('Content-Type' => 'text/plain'))
28
+ @proxy.deliver('message')
29
+ end
30
+
26
31
  describe "error handling" do
27
32
  Logglier::Client::HTTP::NetHTTPProxy::RETRY_EXCEPTIONS.each do |error|
28
33
  context "for #{error}" do
@@ -19,3 +19,43 @@ describe Logglier::Client::HTTP::DeliveryThread do
19
19
  subject.join
20
20
  end
21
21
  end
22
+
23
+ describe Logglier::Client::HTTP::DeliveryThreadManager do
24
+ before do
25
+ @mock_http = MockNetHTTPProxy.new
26
+ Logglier::Client::HTTP::NetHTTPProxy.stub(:new) { @mock_http }
27
+ end
28
+
29
+ subject { described_class.new(URI.parse('http://localhost')) }
30
+
31
+ it "should instantiate a delivery_thread" do
32
+ Logglier::Client::HTTP::DeliveryThread.should_receive(:new).once
33
+ subject
34
+ end
35
+
36
+ it "should deliver messages via delivery_thread" do
37
+ @mock_thread = Object.new
38
+ @mock_thread.stub(:alive?) { true }
39
+ @mock_thread.should_receive(:deliver).once.with('foo')
40
+ Logglier::Client::HTTP::DeliveryThread.should_receive(:new).once.and_return(@mock_thread)
41
+
42
+ subject.deliver('foo')
43
+ end
44
+
45
+ it "should respawn a dead delivery_thread" do
46
+ @first_thread = subject.instance_variable_get(:@thread)
47
+ @first_thread.should_receive(:deliver).once.with('first')
48
+
49
+ subject.deliver('first')
50
+
51
+ @first_thread.kill
52
+
53
+ subject.deliver('force respawn')
54
+
55
+ @second_thread = subject.instance_variable_get(:@thread)
56
+ @second_thread.should_not eql(@first_thread)
57
+ @second_thread.should_receive(:deliver).once.with('second')
58
+
59
+ subject.deliver('second')
60
+ end
61
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logglier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-26 00:00:00.000000000Z
12
+ date: 2012-08-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70301258989680 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,18 +21,28 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70301258989680
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec
27
- requirement: &70301258989120 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
31
36
  - !ruby/object:Gem::Version
32
- version: 2.5.0
37
+ version: 2.11.0
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70301258989120
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.11.0
36
46
  description: Logger => Loggly
37
47
  email: edwardam@interlix.com
38
48
  executables: []
@@ -51,6 +61,7 @@ files:
51
61
  - ./lib/logglier/client.rb
52
62
  - ./lib/logglier/version.rb
53
63
  - ./lib/logglier.rb
64
+ - ./spec/client/syslog_spec.rb
54
65
  - ./spec/client_spec.rb
55
66
  - ./spec/http_spec.rb
56
67
  - ./spec/logglier_spec.rb
@@ -76,13 +87,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
87
  version: 1.3.6
77
88
  requirements: []
78
89
  rubyforge_project: logglier
79
- rubygems_version: 1.8.6
90
+ rubygems_version: 1.8.23
80
91
  signing_key:
81
92
  specification_version: 3
82
93
  summary: Loggly "plugin" for Logger
83
94
  test_files:
95
+ - ./spec/client/syslog_spec.rb
84
96
  - ./spec/client_spec.rb
85
97
  - ./spec/http_spec.rb
86
98
  - ./spec/logglier_spec.rb
87
99
  - ./spec/spec_helper.rb
88
100
  - ./spec/threaded_spec.rb
101
+ has_rdoc: