logstash-logger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ localhost
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ # - ree
4
+ # - 1.8.7
5
+ - 1.9.3
6
+ # - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ # - rbx-18mode
9
+ # - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in logstash-logger.gemspec
4
+ gemspec
5
+
6
+ gem 'json', :platform => :ruby_18
7
+
8
+ group :development do
9
+ gem 'ruby-debug', :platform => :ruby_18
10
+ gem 'debugger', :platform => :ruby_19
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David Butler
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ # LogStashLogger [![Build Status](https://travis-ci.org/dwbutler/logstash-logger.png?branch=master)](https://travis-ci.org/dwbutler/logstash-logger)
2
+
3
+ This gem implements a subclass of Ruby's Logger class that logs directly to [logstash](http://logstash.net).
4
+ It writes to a logstash listener over a TCP connection, in logstash JSON format. This is an improvement over
5
+ writing to a file or syslog since logstash can receive the structured data directly.
6
+
7
+ ## Features
8
+
9
+ * Writes directly to logstash over a TCP connection.
10
+ * Always writes in logstash JSON format.
11
+ * Logger can take a string message, a hash, a LogStash::Event, or a logstash-formatted json string as input.
12
+ * Events are automatically populated with message, timestamp, host, and severity.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'logstash-logger'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install logstash-logger
27
+
28
+ ## Usage
29
+
30
+ First set up a logstash agent to receive input over a TCP port.
31
+
32
+ ```ruby
33
+ logger = LogStashLogger.new('localhost', 5228)
34
+ logger.info 'test'
35
+ # Logs {"@source":"server-host-name","@tags":[],"@fields":{"severity":"INFO"},"@message":"test","@timestamp":"2012-12-15T00:48:29+00:00"}
36
+ ```
37
+
38
+ ## Rails integration
39
+
40
+ Add the following to your config/environments/production.rb:
41
+
42
+ ```ruby
43
+ config.logger = ActiveSupport::TaggedLogging.new(LogStashLogger.new('localhost', 5228))
44
+ ```
45
+
46
+ To get Rails to nicely output its logs in structured logstash format, try one of the following gems:
47
+
48
+ * [lograge](https://github.com/roidrage/lograge)
49
+ * [yarder](https://github.com/rurounijones/yarder)
50
+
51
+ Currently these gems output a JSON string, which LogStashLogger then parses.
52
+ Future versions of these gems could potentially have deeper integration with LogStashLogger.
53
+
54
+ ## Ruby compatibility
55
+
56
+ Verified to work with:
57
+
58
+ * Ruby 1.9.3
59
+ * JRuby 1.7.1 (1.9 mode)
60
+
61
+ Ruby 1.8.7 is not supported because LogStash::Event is not compatible with Ruby 1.8.7. This might change in the future.
62
+
63
+ Rubinius might work, but I haven't been able to test it.
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Run all specs"
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = %w[--color]
8
+ t.verbose = false
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,10 @@
1
+ require 'logstash-logger/version'
2
+
3
+ require 'thread'
4
+ require 'socket'
5
+ require 'logstash-event'
6
+
7
+ require 'logstash-logger/tcp_client'
8
+ require 'logstash-logger/logger'
9
+ require 'logstash-logger/version'
10
+
@@ -0,0 +1,66 @@
1
+ class LogStashLogger < ::Logger
2
+
3
+ attr_reader :client
4
+
5
+ LOGSTASH_EVENT_FIELDS = %w(@timestamp @tags @type @source @fields @message).freeze
6
+ HOST = Socket.gethostname
7
+
8
+ def initialize(host, port)
9
+ @client = ::LogStashLogger::TCPClient.new(host, port)
10
+ super
11
+ end
12
+
13
+ def add(severity, message = nil, progname = nil, &block)
14
+ severity ||= UNKNOWN
15
+ if severity < @level
16
+ return true
17
+ end
18
+ progname ||= @progname
19
+ if message.nil?
20
+ if block_given?
21
+ message = yield
22
+ else
23
+ message = progname
24
+ progname = @progname
25
+ end
26
+ end
27
+ @client.write(
28
+ format_message(format_severity(severity), Time.now, progname, message))
29
+ true
30
+ end
31
+
32
+ def format_message(severity, time, progname, message)
33
+ data = message
34
+ if data.is_a?(String) && data[0] == '{'
35
+ data = (JSON.parse(message) rescue nil) || message
36
+ end
37
+
38
+ event = case data
39
+ when LogStash::Event
40
+ data.clone
41
+ when Hash
42
+ event_data = {
43
+ "@tags" => [],
44
+ "@fields" => {},
45
+ "@timestamp" => time
46
+ }
47
+ LOGSTASH_EVENT_FIELDS.each do |field_name|
48
+ if field_data = data.delete(field_name)
49
+ event_data[field_name] = field_data
50
+ end
51
+ end
52
+ event_data["@fields"].merge!(data)
53
+ LogStash::Event.new(event_data)
54
+ when String
55
+ LogStash::Event.new("@message" => data, "@timestamp" => time)
56
+ end
57
+
58
+ event['severity'] ||= severity
59
+ #event.type = progname
60
+ if event.source == 'unknown'
61
+ event["@source"] = HOST
62
+ end
63
+
64
+ event
65
+ end
66
+ end
@@ -0,0 +1,23 @@
1
+ class LogStashLogger::TCPClient
2
+ def initialize(host, port)
3
+ @host = host
4
+ @port = port
5
+ end
6
+
7
+ def write(event)
8
+ begin
9
+ connect unless @socket
10
+
11
+ @socket.write("#{event.to_hash.to_json}\n")
12
+ rescue => e
13
+ warn "LogStashLogger::TCPClient - #{e.class} - #{e.message}"
14
+ @socket && @socket.close rescue nil
15
+ @socket = nil
16
+ end
17
+ end
18
+
19
+ private
20
+ def connect
21
+ @socket = TCPSocket.new(@host, @port)
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ require 'logger'
2
+
3
+ class LogStashLogger < ::Logger
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'logstash-logger/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "logstash-logger"
8
+ gem.version = LogStashLogger::VERSION
9
+ gem.authors = ["David Butler"]
10
+ gem.email = ["dwbutler@ucla.edu"]
11
+ gem.description = %q{Ruby logger that writes directly to LogStash}
12
+ gem.summary = %q{LogStash Logger for ruby}
13
+ gem.homepage = "http://github.com/dwbutler/logstash-logger"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency 'logstash-event'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'rake'
23
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe LogStashLogger do
4
+ subject { LogStashLogger.new('localhost', port)}
5
+ let(:port) { 5228 }
6
+ let(:server) { TCPServer.new(port) }
7
+ let(:host) { Socket.gethostname }
8
+
9
+ before(:all) do
10
+ server # Initialize TCP server
11
+ end
12
+
13
+ let(:logstash_event) do
14
+ event = LogStash::Event.new
15
+ event.message = 'test'
16
+ event['severity'] = 'INFO'
17
+ event
18
+ end
19
+
20
+ it 'should take a string message and write a logstash event' do
21
+ message = 'test'
22
+
23
+ subject.client.should_receive(:write) do |event|
24
+ event.should be_a LogStash::Event
25
+ event.source.should eql(host)
26
+ event.message.should eql(message)
27
+ event['severity'].should eql('INFO')
28
+ end
29
+
30
+ subject.info(message)
31
+ end
32
+
33
+ it 'should take a logstash-formatted json string and write out a logstash event' do
34
+ subject.client.should_receive(:write) do |event|
35
+ event.should be_a LogStash::Event
36
+ event.message.should eql(logstash_event.message)
37
+ event.source.should eql(host)
38
+ end
39
+
40
+ subject.info(logstash_event.to_json)
41
+ end
42
+
43
+ it 'should take a LogStash::Event and write it out' do
44
+ subject.client.should_receive(:write) do |event|
45
+ event.should be_a LogStash::Event
46
+ event.message.should eql(logstash_event.message)
47
+ event['severity'].should eql(logstash_event['severity'])
48
+ event.timestamp.should eql(logstash_event.timestamp)
49
+ event.source.should eql(host)
50
+ end
51
+
52
+ subject.warn(logstash_event)
53
+ end
54
+
55
+ it 'should take a hash and write out a logstash event' do
56
+ data = {
57
+ "@message" => 'test',
58
+ 'severity' => 'INFO'
59
+ }
60
+
61
+ subject.client.should_receive(:write) do |event|
62
+ event.should be_a LogStash::Event
63
+ event.message.should eql('test')
64
+ event['severity'].should eql('INFO')
65
+ event.source.should eql(host)
66
+ end
67
+
68
+ subject.info(data)
69
+ end
70
+
71
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'logstash-logger'
5
+
6
+ RSpec.configure do |config|
7
+ config.order = "random"
8
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-logger
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - David Butler
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-12-15 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: logstash-event
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rspec
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ description: Ruby logger that writes directly to LogStash
63
+ email:
64
+ - dwbutler@ucla.edu
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - .gitignore
73
+ - .travis.yml
74
+ - Gemfile
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - lib/logstash-logger.rb
79
+ - lib/logstash-logger/logger.rb
80
+ - lib/logstash-logger/tcp_client.rb
81
+ - lib/logstash-logger/version.rb
82
+ - logstash-logger.gemspec
83
+ - spec/logger_spec.rb
84
+ - spec/spec_helper.rb
85
+ homepage: http://github.com/dwbutler/logstash-logger
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.24
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: LogStash Logger for ruby
118
+ test_files:
119
+ - spec/logger_spec.rb
120
+ - spec/spec_helper.rb