logstash-file 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Y2MwYzc5MzMxYjE2YjA2M2EwODRmYWQ2MDY4ZDE3MDU5MjQzN2RhYg==
5
+ data.tar.gz: !binary |-
6
+ MGU2N2UwOGEwOWZiNjE2NzI0MmNlOTA0YmQ5MTk4MjJlMGE3NzU5Yw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YWRkMzc4YWEzZGQ3YzUwOTBmZjcyMzc4ZTdjMWU2MDNiMTIyMmE4YzVmZWMy
10
+ MDQxMDQ4NWIyM2RiMmZiZWJkNDBmNzc4YmIxZWFmNzFhM2U5MGRiMzA1ODdk
11
+ MjMxZDFkYzcwNjI0YTQxYTJhOTE5M2NhYjA1OWU5YmQ5ODQ2M2Q=
12
+ data.tar.gz: !binary |-
13
+ M2UxNGU2YzY0ZTRmNTJmYjUzYzEzMjA1OGYzMTlhNjE5ZmJiNGMzOGM4YzJh
14
+ OGVmZjA5OWJiYTZmY2M4MTY5NzJlOGIxYzQwN2Q1NTIyMDJjMDIwYmMyNDk1
15
+ M2M1Njg5M2NjNjg0ZTgwODUxNDE0NTZiYTE5MDIxZjk3OTQxODc=
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ coverage
2
+ yardoc
3
+ pkg
4
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,21 @@
1
+ ---
2
+ script: rake
3
+
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - rbx-head
8
+ - jruby-19mode
9
+ - jruby-head
10
+
11
+ notifications:
12
+ email:
13
+ recipients:
14
+ - development@olery.com
15
+ email:
16
+ on_success: change
17
+ on_failure: always
18
+
19
+ branches:
20
+ only:
21
+ - master
data/.yardopts ADDED
@@ -0,0 +1,12 @@
1
+ ./lib/logstash-file/**/*.rb ./lib/logstash-file.rb
2
+ -m markdown
3
+ -M kramdown
4
+ -o yardoc
5
+ -r ./README.md
6
+ --private
7
+ --protected
8
+ --asset ./doc/css/common.css:css/common.css
9
+ --verbose
10
+ -
11
+ ./doc/*.md
12
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org/'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013, Olery <http://olery.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Logstash File Logger
2
+
3
+ <a href="http://logstash.net/" title="Logstash">
4
+ <img src="logstash.png" alt="Logstash" align="right">
5
+ </a>
6
+
7
+ The logstash-file Gem is yet another Logstash logger but instead of using a
8
+ network transporation layer such as TCP or UDP it writes JSON data to a file.
9
+
10
+ The LogstashFile::Logger class provides a Logger-like interface by supplying
11
+ methods such as `info`, `error`, etc. Unlike the Logger API they take a 2nd
12
+ optional argument that can be used to specify custom fields to set in the
13
+ logging message. See below for more information.
14
+
15
+ ## Requirements
16
+
17
+ * Ruby 1.9.3 or newer
18
+
19
+ ## Usage
20
+
21
+ First install the Gem:
22
+
23
+ gem install logstash-file
24
+
25
+ Basic logging is as following:
26
+
27
+ require 'logstash-file'
28
+
29
+ logger = LogstashFile::Logger.new(STDOUT)
30
+
31
+ logger.info("Hello!") # => {"@fields": {"level": "info"}, "@message": "Hello!", "@timestamp": "..."}
32
+
33
+ # Logging using custom fields
34
+ logger.info("Hello!", :user => "Alice") # => {"@fields": {"user": "Alice", ...}, ...}
35
+
36
+ ## License
37
+
38
+ The source code of this repository and logstash-file itself are licensed under
39
+ the MIT license unless specified otherwise. A copy of this license can be found
40
+ in the file "LICENSE" in the root directory of this repository.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require_relative 'lib/logstash-file/version'
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'ci/reporter/rake/rspec'
5
+
6
+ Dir['./task/*.rake'].each do |task|
7
+ import(task)
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,69 @@
1
+
2
+ body
3
+ {
4
+ font-size: 14px;
5
+ line-height: 1.6;
6
+ margin: 0 auto;
7
+ max-width: 960px;
8
+ }
9
+
10
+ p code
11
+ {
12
+ background: #f2f2f2;
13
+ padding-left: 3px;
14
+ padding-right: 3px;
15
+ }
16
+
17
+ pre.code
18
+ {
19
+ font-size: 13px;
20
+ line-height: 1.4;
21
+ }
22
+
23
+ /**
24
+ * YARD uses generic table styles, using a special class means those tables
25
+ * don't get messed up.
26
+ */
27
+ .table
28
+ {
29
+ border: 1px solid #ccc;
30
+ border-right: none;
31
+ border-collapse: separate;
32
+ border-spacing: 0;
33
+ text-align: left;
34
+ }
35
+
36
+ .table.full
37
+ {
38
+ width: 100%;
39
+ }
40
+
41
+ .table .field_name
42
+ {
43
+ min-width: 160px;
44
+ }
45
+
46
+ .table thead tr th.no_sort:first-child
47
+ {
48
+ width: 25px;
49
+ }
50
+
51
+ .table thead tr th, .table tbody tr td
52
+ {
53
+ border-bottom: 1px solid #ccc;
54
+ border-right: 1px solid #ccc;
55
+ min-width: 20px;
56
+ padding: 8px 5px;
57
+ text-align: left;
58
+ vertical-align: top;
59
+ }
60
+
61
+ .table tbody tr:last-child td
62
+ {
63
+ border-bottom: none;
64
+ }
65
+
66
+ .table tr:nth-child(odd) td
67
+ {
68
+ background: #f9f9f9;
69
+ }
data/jenkins.sh ADDED
@@ -0,0 +1,16 @@
1
+ # This configuration file is used to test/build the project on Olery's private
2
+ # Jenkins instance. Patches containing changes to this file made by people
3
+ # outside of Olery will most likely be rejected.
4
+
5
+ # The name of the project, used for other settings such as the MySQL database
6
+ # and the package name.
7
+ PROJECT_NAME="logstash_file"
8
+
9
+ # Whether or not to use a MySQL database, set to a non empty value to enable
10
+ # this. Enabling this will tell Jenkins to create/drop the database and to
11
+ # import any migrations if needed.
12
+ unset USE_MYSQL
13
+
14
+ # The command to run for the test suite. Junction itself doesn't have a test
15
+ # suite so we'll use a noop.
16
+ TEST_COMMAND="rake jenkins --trace"
@@ -0,0 +1,64 @@
1
+ module LogstashFile
2
+ ##
3
+ # The {LogstashFile::Logger} class is a simple Logstash JSON logger with an
4
+ # API similar to the Logger class provided by the Ruby standard library.
5
+ #
6
+ # This logger class internally uses a mutex to synchronize write operations,
7
+ # thus it should be fine to share an instance of this class between multiple
8
+ # threads. Having said that, you might experience a performance bottleneck if
9
+ # you're logging a lot across different threads.
10
+ #
11
+ class Logger
12
+ ##
13
+ # The format used for formatting the times of each log entry.
14
+ #
15
+ # @return [String]
16
+ #
17
+ TIME_FORMAT = '%Y-%m-%dT%H:%M:%S%z'
18
+
19
+ ##
20
+ # @param [String|IO] path Path to the file to write logging data to. You
21
+ # can also supply an IO object to use.
22
+ #
23
+ # @todo Investigate to see if a mutex is really required for write-only
24
+ # operations.
25
+ #
26
+ def initialize(path)
27
+ @handle = path.respond_to?(:write) ? path : File.open(path, 'a+')
28
+ @mutex = Mutex.new
29
+ end
30
+
31
+ ##
32
+ # Closes the associated stream.
33
+ #
34
+ def close
35
+ @handle.close
36
+ end
37
+
38
+ [:info, :error, :warning, :debug].each do |level|
39
+ define_method(level) do |message, fields = {}|
40
+ log(message, fields.merge(:level => level))
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ ##
47
+ # @param [String] message The message to log.
48
+ # @param [Hash] fields Extra fields to log.
49
+ #
50
+ def log(message, fields = {})
51
+ time = Time.now.utc.strftime(TIME_FORMAT)
52
+ entry = {:@timestamp => time, :@fields => fields, :@message => message}
53
+
54
+ write(entry)
55
+ end
56
+
57
+ ##
58
+ # @param [Hash] hash
59
+ #
60
+ def write(hash)
61
+ @mutex.synchronize { @handle.puts(JSON(hash)) }
62
+ end
63
+ end # Logger
64
+ end # LogstashFile
@@ -0,0 +1,3 @@
1
+ module LogstashFile
2
+ VERSION = '0.0.1'
3
+ end # LogstashFile
@@ -0,0 +1,5 @@
1
+ require 'json'
2
+ require 'thread'
3
+
4
+ require_relative 'logstash-file/version'
5
+ require_relative 'logstash-file/logger'
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../lib/logstash-file/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'logstash-file'
5
+ gem.version = LogstashFile::VERSION
6
+ gem.authors = ['Yorick Peterse']
7
+ gem.summary = 'Simple file based logging for Logstash.'
8
+ gem.description = gem.summary
9
+ gem.has_rdoc = 'yard'
10
+
11
+ gem.required_ruby_version = '>= 1.9.3'
12
+
13
+ gem.files = `git ls-files`.split("\n").sort
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+
17
+ gem.add_dependency 'json'
18
+
19
+ gem.add_development_dependency 'rake'
20
+ gem.add_development_dependency 'bundler'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'yard'
23
+ gem.add_development_dependency 'simplecov'
24
+ gem.add_development_dependency 'kramdown'
25
+ gem.add_development_dependency 'ci_reporter'
26
+ end
data/logstash.png ADDED
Binary file
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe LogstashFile::Logger do
4
+ let :buffer do
5
+ StringIO.new
6
+ end
7
+
8
+ let :logger do
9
+ LogstashFile::Logger.new(buffer)
10
+ end
11
+
12
+ def read_output(buffer)
13
+ buffer.rewind
14
+
15
+ return JSON(buffer.read.strip)
16
+ end
17
+
18
+ example 'log an info message' do
19
+ logger.info('info message')
20
+
21
+ output = read_output(buffer)
22
+
23
+ output['@message'].should == 'info message'
24
+ output['@fields'].should == {'level' => 'info'}
25
+
26
+ output.key?('@timestamp').should == true
27
+ end
28
+
29
+ example 'add custom fields to a log entry' do
30
+ logger.info('info message', :number => 10)
31
+
32
+ output = read_output(buffer)
33
+
34
+ output['@fields']['number'].should == 10
35
+ end
36
+
37
+ # Half-assed test to ensure that multiple entries are actually written on
38
+ # separate lines. It doesn't *actually* test thread-safety though, that's a
39
+ # pretty difficult thing to test reliably.
40
+ example 'synchronize multi-threaded operations' do
41
+ amount = 50
42
+ threads = []
43
+
44
+ amount.times do |n|
45
+ threads << Thread.new { logger.info("thread #{n}") }
46
+ end
47
+
48
+ threads.each(&:join)
49
+
50
+ buffer.rewind
51
+
52
+ lines = buffer.read.split("\n")
53
+
54
+ lines.length.should == amount
55
+ end
56
+
57
+ example 'create a logger using a filepath' do
58
+ file = Tempfile.new('logstash-file')
59
+ logger = LogstashFile::Logger.new(file.path)
60
+
61
+ logger.info('testing')
62
+ logger.close
63
+
64
+ output = read_output(file)
65
+
66
+ output['@message'].should == 'testing'
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ require 'rspec'
2
+ require 'stringio'
3
+ require 'tempfile'
4
+
5
+ require_relative 'support/simplecov' if ENV['COVERAGE']
6
+ require_relative '../lib/logstash-file'
7
+
8
+ RSpec.configure do |config|
9
+ config.color = true
10
+ end
@@ -0,0 +1,12 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.configure do
4
+ root File.expand_path('../../../', __FILE__)
5
+ command_name 'rspec'
6
+ project_name 'logstash-file'
7
+
8
+ add_filter 'spec'
9
+ add_filter 'lib/logstash-file/version'
10
+ end
11
+
12
+ SimpleCov.start
@@ -0,0 +1,6 @@
1
+ desc 'Generates code coverage'
2
+ task :coverage do
3
+ ENV['COVERAGE'] = '1'
4
+
5
+ Rake::Task['test'].invoke
6
+ end
data/task/doc.rake ADDED
@@ -0,0 +1,4 @@
1
+ desc 'Builds the documentation'
2
+ task :doc do
3
+ sh('yard doc')
4
+ end
data/task/jenkins.rake ADDED
@@ -0,0 +1,2 @@
1
+ desc 'Runs all the tests for Jenkins'
2
+ task :jenkins => ['ci:setup:rspec', 'test']
data/task/tag.rake ADDED
@@ -0,0 +1,6 @@
1
+ desc 'Creates a Git tag for the current version'
2
+ task :tag do
3
+ version = Oni::VERSION
4
+
5
+ sh %Q{git tag -a -m "Version #{version}" -s #{version}}
6
+ end
data/task/test.rake ADDED
@@ -0,0 +1,4 @@
1
+ desc 'Runs the tests'
2
+ task :test do
3
+ sh 'rspec spec'
4
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yorick Peterse
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: kramdown
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: ci_reporter
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Simple file based logging for Logstash.
126
+ email:
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - .gitignore
132
+ - .travis.yml
133
+ - .yardopts
134
+ - Gemfile
135
+ - LICENSE
136
+ - README.md
137
+ - Rakefile
138
+ - doc/css/common.css
139
+ - jenkins.sh
140
+ - lib/logstash-file.rb
141
+ - lib/logstash-file/logger.rb
142
+ - lib/logstash-file/version.rb
143
+ - logstash-file.gemspec
144
+ - logstash.png
145
+ - spec/logstash-file/logger_spec.rb
146
+ - spec/spec_helper.rb
147
+ - spec/support/simplecov.rb
148
+ - task/coverage.rake
149
+ - task/doc.rake
150
+ - task/jenkins.rake
151
+ - task/tag.rake
152
+ - task/test.rake
153
+ homepage:
154
+ licenses: []
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: 1.9.3
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ! '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.1.2
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Simple file based logging for Logstash.
176
+ test_files:
177
+ - spec/logstash-file/logger_spec.rb
178
+ - spec/spec_helper.rb
179
+ - spec/support/simplecov.rb
180
+ has_rdoc: yard