logging-logstash 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 47cda14544b36f3fc8530e81e4bf310ebc6f6905
4
+ data.tar.gz: 73ed0b9d9541f09ae2a08fb29a8cb04b1839308f
5
+ SHA512:
6
+ metadata.gz: d4fecaa072fc33f6f020b8a2057364f50a19a12dd19547c26fcbb8844d5b13cd435e0717fbf1e7ab8013103ba43b1bd323a7190124f54b8a94827f526c8e4ce9
7
+ data.tar.gz: cd7f1e7930649ef8d2da7a84f1d9d176db947db11a89c4a030be5bd889cad37ced37360df97e733f00d9623385f3a2ca12c4feffb47fac21a91794cf4f4b5112
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea/
16
+ *~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+
4
+ group :development do
5
+ gem "rspec", "3.1.0"
6
+ gem "logging", "1.8.1"
7
+ gem "timecop", "~> 0.7.1"
8
+ end
9
+ # Specify your gem's dependencies in logging-logstash.gemspec
10
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Peter Schrammel
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,51 @@
1
+ # Logging::Logstash
2
+
3
+ This is a Logstash appender for the Logging framework.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'logging-logstash'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install logging-logstash
20
+
21
+ ## Usage
22
+
23
+ Configure it as an appender:
24
+
25
+ logger=Logging.logger["test"]
26
+ logger.add_appenders(Logging::Appenders.logstash('mylog',{:uri => 'tcp://localhost:5229'}))
27
+
28
+ then log hashes or strings:
29
+
30
+ Logging.root.info("string")
31
+
32
+ String messages will be logged like a {:message => message} hash.
33
+
34
+ Logging.root.info(:my => "string", :app => "myappname")
35
+
36
+ The given hash will be enhanced by the following keys:
37
+ * @timestamp
38
+ * @version
39
+ * @severity (uppercase name of log level)
40
+ * @host (host of the logger)
41
+ * @log_name (the name you have given the appender)
42
+ * everything from the mdc (Mapped Diagnostic Context) hash
43
+ * everything from the ndc (Nested Diagnostic Context) array
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/[my-github-username]/logging-logstash/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,96 @@
1
+ module Logging
2
+ module Appenders
3
+ def self.logstash(*args)
4
+ return Logging::Appenders::Logstash if args.empty?
5
+ Logging::Appenders::Logstash.new(*args)
6
+ end
7
+
8
+ # This class provides an Appender that can write to remote syslog.
9
+ #
10
+ class Logstash < Logging::Appender
11
+
12
+ def self.num_to_level(level_num)
13
+ @level_nums ||= Loggin::LEVELS..inject({}) do |hash, kv|
14
+ hash[kv[1]] = kv[0]; hash
15
+ end
16
+ @level_nums[level_num]
17
+ end
18
+
19
+ # call:
20
+ # Logstash.new( name, opts = {} )
21
+ #
22
+ # Create an appender that will log messages to logstash. The
23
+ # options that can be used to configure the appender are as follows:
24
+ #
25
+ # it accepts all options of logstash logger: https://github.com/dwbutler/logstash-logger
26
+ #
27
+ def initialize(name, opts = {})
28
+ @host=opts.delete(:host) || Socket.gethostname
29
+ @logstash_device=LogStashLogger::Device.new(opts)
30
+ super
31
+ end
32
+
33
+
34
+ private
35
+
36
+ # to have a good entry point for testing
37
+ attr_reader :logstash_device
38
+
39
+ # call:
40
+ # write( event )
41
+ #
42
+ # The trick is, that logging accepts all kind of objects to be logged. They are in
43
+ # the event.data and are raw (not turned into strings).
44
+ #
45
+ def write(event)
46
+ ls_event= data2logstash_event(event)
47
+
48
+ logstash_device.write(ls_event.to_json+"\n")
49
+ logstash_device.flush
50
+ self
51
+ end
52
+
53
+ def data2logstash_event(logging_event)
54
+ stash_event=if logging_event.data.kind_of?(Hash)
55
+ LogStash::Event.new(logging_event.data.dup)
56
+ else
57
+ LogStash::Event.new("message" => msg2str(logging_event.data))
58
+ end
59
+
60
+ stash_event['@severity'] ||= ::Logging::LNAMES[logging_event.level]
61
+ stash_event['@host'] ||= @host
62
+ stash_event['@log_name'] ||= @name
63
+
64
+ Logging.mdc.context.each do |key, value|
65
+ stash_event[key] ||= value #we don't overrdide given things
66
+ end
67
+
68
+ Logging.ndc.context.each do |ctx|
69
+ ctx.each do |key, value|
70
+ stash_event[key] ||= value #we don't overrdide given things
71
+ end
72
+ end
73
+
74
+ # In case Time#to_json has been overridden
75
+ if stash_event.timestamp.is_a?(Time)
76
+ stash_event.timestamp = stash_event.timestamp.iso8601(3)
77
+ end
78
+ stash_event
79
+ end
80
+
81
+ def msg2str(msg)
82
+ case msg
83
+ when ::String
84
+ msg
85
+ when ::Exception
86
+ "#{ msg.message } (#{ msg.class })\n" <<
87
+ (msg.backtrace || []).join("\n")
88
+ else
89
+ msg.inspect
90
+ end
91
+ end
92
+
93
+
94
+ end # Logstash
95
+ end # Appenders
96
+ end #Logging
@@ -0,0 +1,10 @@
1
+ # in case someone did a :require => false
2
+ require "logging"
3
+ require "logstash-logger"
4
+ require "uri"
5
+ require "socket"
6
+
7
+
8
+ require 'logging/appender'
9
+ require "logging/logstash/version"
10
+ require 'logging/appenders/logstash'
@@ -0,0 +1,5 @@
1
+ module Logging
2
+ module Logstash
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'logging/logstash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "logging-logstash"
8
+ spec.version = Logging::Logstash::VERSION
9
+ spec.authors = ["Peter Schrammel"]
10
+ spec.email = ["peter.schrammel@gmx.de"]
11
+ spec.summary = %q{A loggstash appender for the logging framework.}
12
+ spec.description = %q{Allows you to log your mdc and hashes to logstash and keep the key-values for later analysis}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_dependency "logging", "~> 1.8.1"
25
+ spec.add_dependency "logstash-logger", "~> 0.7.0"
26
+ end
@@ -0,0 +1,86 @@
1
+ RSpec.describe 'Logging::Appenders::Logstash' do
2
+ before do
3
+ FileUtils.rm_rf("/tmp/logstash.output")
4
+ Timecop.freeze(Time.local(2014, 12, 10, 10, 20, 19))
5
+ Logging.mdc.clear
6
+ Logging.ndc.clear
7
+ end
8
+
9
+ let(:appender) {
10
+ Logging::Appenders.logstash('logstash', :uri => "file:///tmp/logstash.output")
11
+ }
12
+
13
+ let(:logger) {
14
+ log = Logging.logger['example_logger']
15
+ log.add_appenders(appender)
16
+ log.level = :info
17
+ log
18
+ }
19
+
20
+ it "initializes via constructor Logging::Appenders.logstash" do
21
+ expect(appender).to be_instance_of(Logging::Appenders::Logstash)
22
+ end
23
+
24
+ it "writes a log with a string" do
25
+ logger.info("test")
26
+ expect(log_content).to eq({"message" => "test",
27
+ "@timestamp" => "2014-12-10T10:20:19.000Z",
28
+ "@version" => "1",
29
+ "@severity" => "INFO",
30
+ "@log_name" => "logstash",
31
+ "@host" => "pa-dev"})
32
+ end
33
+
34
+ it "writes a log with a hash" do
35
+ logger.info("test" => 1, "test2" => "ok")
36
+ expect(log_content).to eq({"@timestamp" => "2014-12-10T10:20:19.000Z",
37
+ "@version" => "1",
38
+ "@severity" => "INFO",
39
+ "@host" => "pa-dev",
40
+ "@log_name" => "logstash",
41
+ "test" => 1,
42
+ "test2" => "ok"
43
+ })
44
+ end
45
+
46
+ it "enhances the information with the mdc context" do
47
+ Logging.mdc["app"] = "core_api"
48
+ logger.happy("test" => 1, "test2" => "ok")
49
+ expect(log_content).to eq({"@timestamp" => "2014-12-10T10:20:19.000Z",
50
+ "@version" => "1",
51
+ "@severity" => "HAPPY",
52
+ "@host" => "pa-dev",
53
+ "@log_name" => "logstash",
54
+ "test" => 1,
55
+ "test2" => "ok",
56
+ "app" => "core_api"
57
+ })
58
+ end
59
+
60
+ it "enhances the information with the ndc context" do
61
+ Logging.ndc.push(:app => "myapp")
62
+ Logging.ndc.push(:subsystem => "sub1")
63
+
64
+ logger.happy("test" => 1, "test2" => "ok")
65
+ expect(log_content).to eq({"@timestamp" => "2014-12-10T10:20:19.000Z",
66
+ "@version" => "1",
67
+ "@severity" => "HAPPY",
68
+ "@host" => "pa-dev",
69
+ "@log_name" => "logstash",
70
+ "test" => 1,
71
+ "test2" => "ok",
72
+ "app" => "myapp",
73
+ "subsystem" => "sub1"
74
+ })
75
+ end
76
+
77
+
78
+ it "escapes multilines correctly" do
79
+ logger.happy("test\ntest2")
80
+ expect(File.readlines("/tmp/logstash.output").size).to eq(1)
81
+ end
82
+
83
+ def log_content
84
+ JSON.parse(File.read("/tmp/logstash.output"))
85
+ end
86
+ end
@@ -0,0 +1,98 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ require 'timecop'
18
+ require 'logging'
19
+ require 'logging/logstash'
20
+
21
+ #Have a undefined init error? run your specs without bundler (got a solution? -> Pull request)
22
+ Logging.init(:DEBUG,:INFO,:HAPPY,:WARN,:FATAL)
23
+
24
+
25
+ RSpec.configure do |config|
26
+ # rspec-expectations config goes here. You can use an alternate
27
+ # assertion/expectation library such as wrong or the stdlib/minitest
28
+ # assertions if you prefer.
29
+ config.expect_with :rspec do |expectations|
30
+ # This option will default to `true` in RSpec 4. It makes the `description`
31
+ # and `failure_message` of custom matchers include text for helper methods
32
+ # defined using `chain`, e.g.:
33
+ # be_bigger_than(2).and_smaller_than(4).description
34
+ # # => "be bigger than 2 and smaller than 4"
35
+ # ...rather than:
36
+ # # => "be bigger than 2"
37
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
38
+ end
39
+
40
+ # rspec-mocks config goes here. You can use an alternate test double
41
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
42
+ config.mock_with :rspec do |mocks|
43
+ # Prevents you from mocking or stubbing a method that does not exist on
44
+ # a real object. This is generally recommended, and will default to
45
+ # `true` in RSpec 4.
46
+ mocks.verify_partial_doubles = true
47
+ end
48
+
49
+ # The settings below are suggested to provide a good initial experience
50
+ # with RSpec, but feel free to customize to your heart's content.
51
+
52
+ # These two settings work together to allow you to limit a spec run
53
+ # to individual examples or groups you care about by tagging them with
54
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
55
+ # get run.
56
+ config.filter_run :focus
57
+ config.run_all_when_everything_filtered = true
58
+
59
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
60
+ # For more details, see:
61
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
62
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
63
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
64
+ config.disable_monkey_patching!
65
+
66
+ # This setting enables warnings. It's recommended, but in some cases may
67
+ # be too noisy due to issues in dependencies.
68
+ config.warnings = true
69
+
70
+ # Many RSpec users commonly either run the entire suite or an individual
71
+ # file, and it's useful to allow more verbose output when running an
72
+ # individual spec file.
73
+ if config.files_to_run.one?
74
+ # Use the documentation formatter for detailed output,
75
+ # unless a formatter has already been configured
76
+ # (e.g. via a command-line flag).
77
+ config.default_formatter = 'doc'
78
+ end
79
+
80
+ # Print the 10 slowest examples and example groups at the
81
+ # end of the spec run, to help surface which specs are running
82
+ # particularly slow.
83
+ config.profile_examples = 10
84
+
85
+ # Run specs in random order to surface order dependencies. If you find an
86
+ # order dependency and want to debug it, you can fix the order by providing
87
+ # the seed, which is printed after each run.
88
+ # --seed 1234
89
+ config.order = :random
90
+
91
+ # Seed global randomization in this process using the `--seed` CLI option.
92
+ # Setting this allows you to use `--seed` to deterministically reproduce
93
+ # test failures related to randomization by passing the same `--seed` value
94
+ # as the one that triggered the failure.
95
+ Kernel.srand config.seed
96
+
97
+ end
98
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logging-logstash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Schrammel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: logging
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.8.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: logstash-logger
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.0
69
+ description: Allows you to log your mdc and hashes to logstash and keep the key-values
70
+ for later analysis
71
+ email:
72
+ - peter.schrammel@gmx.de
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/logging/appenders/logstash.rb
84
+ - lib/logging/logstash.rb
85
+ - lib/logging/logstash/version.rb
86
+ - logging-logstash.gemspec
87
+ - spec/lib/logging/appenders/logstash_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A loggstash appender for the logging framework.
113
+ test_files:
114
+ - spec/lib/logging/appenders/logstash_spec.rb
115
+ - spec/spec_helper.rb