logglier 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  Overview
2
2
  --------
3
3
 
4
- Posts Logger messages to Loggly using the HTTP API.
4
+ Send logged messages to Loggly using either the HTTP API or Syslog/UDP.
5
5
 
6
+ Can be used in place of Ruby's Logger (<http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/>)
7
+
8
+ In fact, it (currently) returns an instance of Logger.
6
9
 
7
10
  Usage
8
11
  -----
@@ -36,7 +39,7 @@ The id is provided by loggly, look at the input's details page
36
39
  Logglier.new('udp://<hostname>:<port>/<facility>')
37
40
 
38
41
  The facility is optional and defaults to 16 (local0) if none is
39
- specified.
42
+ specified. Facilities are just integers from 0 to 23, see <http://www.faqs.org/rfcs/rfc3164.html>
40
43
 
41
44
 
42
45
  TODO
@@ -26,12 +26,19 @@ module Logglier
26
26
  # Required by Logger::LogDevice
27
27
  def write(message)
28
28
  begin
29
- @http.start { @http.request_post(@input_uri.path, message) }
29
+ @http.request_post(@input_uri.path, message)
30
30
  rescue TimeoutError => e
31
31
  $stderr.puts "WARNING: TimeoutError posting message: #{message}"
32
32
  end
33
33
  end
34
34
 
35
+ def formatter
36
+ proc do |severity, datetime, progname, msg|
37
+ message = "#{datetime} "
38
+ message << massage_message(msg, severity)
39
+ end
40
+ end
41
+
35
42
  # Required by Logger::LogDevice
36
43
  def close
37
44
  nil
@@ -9,12 +9,20 @@ module Logglier
9
9
  class Syslog
10
10
  include Logglier::Client::InstanceMethods
11
11
 
12
- attr_reader :input_uri, :facility
12
+ attr_reader :input_uri, :facility, :syslog
13
13
 
14
14
  def initialize(opts={})
15
15
  setup_input_uri(opts)
16
- @syslog = UDPSocket.new()
17
- @syslog.connect(@input_uri.host, @input_uri.port)
16
+
17
+ case @input_uri.scheme
18
+ when 'udp'
19
+ @syslog = UDPSocket.new()
20
+ @syslog.connect(@input_uri.host, @input_uri.port)
21
+ when 'tcp'
22
+ @syslog = TCPSocket.new(@input_uri.host, @input_uri.port)
23
+ @syslog.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1 )
24
+ @syslog.setsockopt( Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
25
+ end
18
26
 
19
27
  unless @input_uri.path.empty?
20
28
  @facility = @input_uri.path.split('/')[1]
@@ -33,7 +41,6 @@ module Logglier
33
41
  # Required by Logger::LogDevice
34
42
  def write(message)
35
43
  begin
36
- pp message
37
44
  @syslog.send(message,0)
38
45
  rescue TimeoutError => e
39
46
  $stderr.puts "WARNING: TimeoutError posting message: #{message}"
@@ -77,15 +84,11 @@ module Logglier
77
84
  else
78
85
  message << "#{$0}: "
79
86
  end
80
- message << "severity=#{severity},"
81
- case msg
82
- when Hash
83
- msg.inject(message) {|m,v| m << "#{v[0]}=#{v[1]}" }
84
- when String
85
- message << msg
86
- else
87
- message << msg.inspect
87
+ message << massage_message(msg,severity)
88
+ if @input_uri.scheme == 'tcp'
89
+ message << "\r\n"
88
90
  end
91
+ message
89
92
  end
90
93
  end
91
94
 
@@ -27,6 +27,20 @@ module Logglier
27
27
 
28
28
  module InstanceMethods
29
29
 
30
+ def massage_message(incoming_message, severity)
31
+ outgoing_message = ""
32
+ outgoing_message << "severity=#{severity}, "
33
+ case incoming_message
34
+ when Hash
35
+ outgoing_message << incoming_message.map { |v| "#{v[0]}=#{v[1]}" }.join(", ")
36
+ when String
37
+ outgoing_message << incoming_message
38
+ else
39
+ outgoing_message << incoming_message.inspect
40
+ end
41
+ outgoing_message
42
+ end
43
+
30
44
  def setup_input_uri(opts)
31
45
  @input_uri = opts[:input_url]
32
46
 
@@ -1,3 +1,3 @@
1
1
  module Logglier
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
data/lib/logglier.rb CHANGED
@@ -14,9 +14,7 @@ module Logglier
14
14
 
15
15
  if client.respond_to?(:formatter)
16
16
  logger.formatter = client.formatter
17
- end
18
-
19
- if client.respond_to?(:datetime_format)
17
+ elsif client.respond_to?(:datetime_format)
20
18
  logger.datetime_format = client.datetime_format
21
19
  end
22
20
 
data/spec/client_spec.rb CHANGED
@@ -12,7 +12,7 @@ describe Logglier::Client do
12
12
 
13
13
  end
14
14
 
15
- context "a single string param" do
15
+ context "with a single string param" do
16
16
 
17
17
  context "that is a valid http uri" do
18
18
 
@@ -33,6 +33,10 @@ describe Logglier::Client do
33
33
 
34
34
  context "that is a valid tcp uri" do
35
35
 
36
+ before do
37
+ TCPSocket.stub(:new) { MockTCPSocket.new }
38
+ end
39
+
36
40
  it "should return an instance of the proper client" do
37
41
  log = Logglier::Client.new('tcp://logs.loggly.com:42538')
38
42
  log.should be_an_instance_of Logglier::Client::Syslog
@@ -58,19 +62,40 @@ describe Logglier::Client do
58
62
 
59
63
  end
60
64
 
61
- context "#write" do
62
- before do
63
- @log = Logglier::Client.new('https://localhost')
64
- @log.http.stub(:start)
65
+ context "HTTPS" do
66
+ context "#write" do
67
+ it "should post a message" do
68
+ log = Logglier::Client.new('https://localhost')
69
+ log.http.stub(:request_post)
70
+ log.http.should_receive(:request_post).with('','msg')
71
+ log.write('msg')
72
+ end
65
73
  end
74
+ end
66
75
 
67
- context "with a message" do
68
-
69
- it "should start a http call" do
70
- @log.http.should_receive(:start)
71
- @log.write('msg')
76
+ context "Syslog" do
77
+ context "udp" do
78
+ context "#write" do
79
+ it "should send a message" do
80
+ log = Logglier::Client.new('udp://localhost:12345')
81
+ log.syslog.stub(:send)
82
+ log.syslog.should_receive(:send).with('msg',0)
83
+ log.write('msg')
84
+ end
72
85
  end
86
+ end
73
87
 
88
+ context "tcp" do
89
+ context "#write" do
90
+ before { TCPSocket.stub(:new) { MockTCPSocket.new } }
91
+ it "should send a message" do
92
+ log = Logglier::Client.new('tcp://localhost:12345')
93
+ log.syslog.stub(:send)
94
+ log.syslog.should_receive(:send).with('msg',0)
95
+ log.write('msg')
96
+ end
97
+ end
74
98
  end
75
99
  end
100
+
76
101
  end
@@ -1,6 +1,34 @@
1
1
  describe Logglier do
2
2
 
3
- subject { Logglier.new('https://localhost') }
3
+ context "HTTPS" do
4
+ subject { new_logglier('https://localhost') }
4
5
 
5
- it { should be_an_instance_of Logger }
6
+ it { should be_an_instance_of Logger }
7
+ its('logdev.dev') { should be_an_instance_of Logglier::Client::HTTP }
8
+
9
+ it_should_behave_like "a logglier enhanced Logger instance"
10
+
11
+ end
12
+
13
+ context "Syslog TCP" do
14
+ before { TCPSocket.stub(:new) { MockTCPSocket.new } }
15
+
16
+ subject { new_logglier('tcp://localhost:12345') }
17
+
18
+ it { should be_an_instance_of Logger }
19
+ its('logdev.dev') { should be_an_instance_of Logglier::Client::Syslog }
20
+
21
+ it_should_behave_like "a logglier enhanced Logger instance"
22
+
23
+ end
24
+
25
+ context "Syslog UDP" do
26
+ subject { new_logglier('udp://localhost:12345') }
27
+
28
+ it { should be_an_instance_of Logger }
29
+ its('logdev.dev') { should be_an_instance_of Logglier::Client::Syslog }
30
+
31
+ it_should_behave_like "a logglier enhanced Logger instance"
32
+
33
+ end
6
34
  end
data/spec/spec_helper.rb CHANGED
@@ -2,11 +2,48 @@ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
2
 
3
3
  require 'lib/logglier'
4
4
 
5
+ module LoggerHacks
6
+ def logdev
7
+ @logdev
8
+ end
9
+ end
10
+
5
11
  RSpec.configure do |config|
6
12
  config.color_enabled = config.tty = true
13
+ config.add_formatter('documentation')
7
14
 
8
15
  config.before(:each) do
9
16
  end
10
17
 
18
+ class MockTCPSocket
19
+ def initialize(*args); end
20
+ def setsockopt(*args); end
21
+ def send(*args); end
22
+ end
23
+
24
+ def new_logglier(url)
25
+ log = Logglier.new(url)
26
+ log.extend(LoggerHacks)
27
+ end
28
+
11
29
  end
12
30
 
31
+ shared_examples_for "a logglier enhanced Logger instance" do
32
+ context "#add" do
33
+ context "with a string" do
34
+ it "should send a message via the logdev" do
35
+ subject.logdev.dev.should_receive(:write).with(/severity=WARN, foo/)
36
+ subject.add(Logger::WARN) { 'foo' }
37
+ end
38
+ end
39
+
40
+ context "with a hash" do
41
+ it "should send a message via the logdev" do
42
+ subject.logdev.dev.should_receive(:write).with(/severity=WARN, foo=bar, man=pants/)
43
+ # The following is equiv to:
44
+ # subject.warn :foo => :bar, :man => :pants
45
+ subject.add(Logger::WARN) { {:foo => :bar, :man => :pants} }
46
+ end
47
+ end
48
+ end
49
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logglier
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Edward Muller (aka freeformz)