graylog2-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in graylog2-rails.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Artem Melnikov
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,29 @@
1
+ # Graylog2::Rails
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'graylog2-rails'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install graylog2-rails
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ default:
2
+ hostname: 127.0.0.1
3
+ port: 12201
4
+ local_app_name: rails_application
5
+ facility: rails_application
6
+ max_chunk_size: LAN
7
+ level: 3
8
+
9
+ production:
10
+ <<: *defaults
11
+
12
+ development:
13
+ <<: *defaults
14
+
15
+ test:
16
+ <<: *defaults
17
+
18
+ stage:
19
+ <<: *defaults
20
+
21
+ demo:
22
+ <<: *defaults
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/graylog2-rails/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Artem Melnikov"]
6
+ gem.email = ["artem.melnikov@ignar.name"]
7
+ gem.description = %q{Wrapper for Graylog2 logging system}
8
+ gem.summary = %q{Wrapper for Graylog2 logging system}
9
+ gem.homepage = "https://github.com/ignar/graylog2-rails"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "graylog2-rails"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Graylog2Rails::VERSION
17
+
18
+ gem.add_dependency("gelf", "~> 1.3.2")
19
+ gem.add_dependency("awesome_print")
20
+ gem.add_dependency("activesupport")
21
+ gem.add_dependency("rake")
22
+
23
+ gem.add_development_dependency("rspec", "~> 2.0")
24
+ end
@@ -0,0 +1,17 @@
1
+ require "rails/generators"
2
+ require "rake"
3
+
4
+ module Graylog2Rails
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ desc "Graylog2Rails install generator"
9
+ def install
10
+ say "Install Graylog2Rails", :green
11
+ @app_name = Rails.application.class.name.sub(/::Application$/, "").underscore
12
+ say "Copying config file"
13
+ template "config.erb", "config/graylog2_rails.yml"
14
+ say "Done", :green
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ default: &defaults
2
+ hostname: 127.0.0.1
3
+ port: 12201
4
+ max_chunk_size: LAN
5
+ level: 3
6
+
7
+ production:
8
+ <<: *defaults
9
+ local_app_name: <%= @app_name %>_production
10
+ facility: <%= @app_name %>_production
11
+
12
+ development:
13
+ <<: *defaults
14
+ local_app_name: <%= @app_name %>_development
15
+ facility: <%= @app_name %>_development
16
+
17
+ test:
18
+ <<: *defaults
19
+ local_app_name: <%= @app_name %>_test
20
+ facility: <%= @app_name %>_test
21
+
22
+ stage:
23
+ <<: *defaults
24
+ local_app_name: <%= @app_name %>_stage
25
+ facility: <%= @app_name %>_stage
26
+
27
+ demo:
28
+ <<: *defaults
29
+ local_app_name: <%= @app_name %>_demo
30
+ facility: <%= @app_name %>_demo
@@ -0,0 +1,10 @@
1
+ require "graylog2-rails/initializers"
2
+ require "graylog2-rails/message"
3
+ require "graylog2-rails/middleware"
4
+ require "graylog2-rails/engine"
5
+ require "graylog2-rails/notifier"
6
+ require "graylog2-rails/version"
7
+
8
+ module Graylog2Rails
9
+ mattr_accessor :gelf_config_options
10
+ end
@@ -0,0 +1,14 @@
1
+ if defined?(Rails) && Rails.version.to_f > 3.1
2
+ class Engine < Rails::Engine
3
+ isolate_namespace Graylog2Rails
4
+ include Graylog2Rails::Initializers
5
+
6
+ initializer "graylog2_rails.establish_connection" do |app|
7
+ read_config!
8
+ end
9
+
10
+ initializer "graylog2_rails.add_middleware", :after => "graylog2_rails.establish_connection" do |app|
11
+ app.middleware.insert_after ActionDispatch::Head, Graylog2Rails::Middleware, Graylog2Rails.gelf_config_options
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Graylog2Rails
2
+ module Initializers
3
+ def read_config!
4
+ custom_config_file = Rails.root.join("config", "graylog2_rails.yml")
5
+ file_content = if custom_config_file.exist?
6
+ File.open(custom_config_file).read
7
+ else
8
+ puts "WARNING: Graylog2 config not found. Using default configuration options. Please run `rake graylog2_rails:install'"
9
+ File.open(File.expand_path(File.join("..", "..", "..", "config", "graylog2_rails.yml"), __FILE__)).read
10
+ end
11
+ Graylog2Rails.gelf_config_options = YAML.load(ERB.new(file_content).result)[Rails.env]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,140 @@
1
+ module Graylog2Rails
2
+ class Message
3
+ attr_accessor :args
4
+ # The exception that caused this notice, if any
5
+ attr_reader :exception
6
+
7
+ # The backtrace from the given exception or hash.
8
+ attr_reader :backtrace
9
+
10
+ # The name of the class of error (such as RuntimeError)
11
+ attr_reader :error_class
12
+
13
+ # The name of the server environment (such as "production")
14
+ attr_reader :environment_name
15
+
16
+ # CGI variables such as HTTP_METHOD
17
+ attr_reader :cgi_data
18
+
19
+ # The message from the exception, or a general description of the error
20
+ attr_reader :error_message
21
+
22
+ # See Configuration#backtrace_filters
23
+ attr_reader :backtrace_filters
24
+
25
+ # See Configuration#params_filters
26
+ attr_reader :params_filters
27
+
28
+ # A hash of parameters from the query string or post body.
29
+ attr_reader :parameters
30
+ alias_method :params, :parameters
31
+
32
+ # The component (if any) which was used in this request (usually the controller)
33
+ attr_reader :component
34
+ alias_method :controller, :component
35
+
36
+ # The action (if any) that was called in this request
37
+ attr_reader :action
38
+
39
+ # A hash of session data from the request
40
+ attr_reader :session_data
41
+
42
+ # The path to the project that caused the error (usually Rails.root)
43
+ attr_reader :project_root
44
+
45
+ # The URL at which the error occurred (if any)
46
+ attr_reader :url
47
+
48
+ # The host name where this error occurred (if any)
49
+ attr_reader :hostname
50
+
51
+ attr_writer :exception, :backtrace, :error_class, :error_message,
52
+ :backtrace_filters, :parameters,
53
+ :environment_filters, :session_data, :url,
54
+ :component, :action, :cgi_data, :environment_name, :hostname
55
+
56
+ def initialize(args)
57
+ self.args = args
58
+ self.exception = args[:exception]
59
+ self.url = args[:url] || rack_env(:url)
60
+
61
+ self.parameters = args[:parameters] || action_dispatch_params || rack_env(:params) || {}
62
+ self.component = args[:component] || args[:controller] || parameters['controller']
63
+ self.action = args[:action] || parameters['action']
64
+
65
+ self.environment_name = args[:environment_name]
66
+ self.cgi_data = args[:cgi_data] || args[:rack_env]
67
+ self.backtrace = self.exception.backtrace.join("\n")
68
+ self.error_class = exception_attribute(:error_class) {|exception| exception.class.name }
69
+ self.error_message = exception_attribute(:error_message, 'Notification') do |exception|
70
+ "#{exception.class.name}: #{exception.message}"
71
+ end
72
+ end
73
+
74
+ def message
75
+ self.error_message
76
+ end
77
+
78
+ def rack_env(method)
79
+ rack_request.send(method) if rack_request
80
+ end
81
+
82
+ def action_dispatch_params
83
+ args[:rack_env]['action_dispatch.request.parameters'] if args[:rack_env]
84
+ end
85
+
86
+ def rack_request
87
+ @rack_request ||= if args[:rack_env]
88
+ ::Rack::Request.new(args[:rack_env])
89
+ end
90
+ end
91
+
92
+ def exception_attribute(attribute, default = nil, &block)
93
+ (exception && from_exception(attribute, &block)) || args[attribute] || default
94
+ end
95
+
96
+ def from_exception(attribute)
97
+ if block_given?
98
+ yield(exception)
99
+ else
100
+ exception.send(attribute)
101
+ end
102
+ end
103
+
104
+ def find_session_data
105
+ self.session_data = args[:session_data] || args[:session] || rack_session || {}
106
+ self.session_data = session_data[:data] if session_data[:data]
107
+ end
108
+
109
+ def to_s
110
+ message = []
111
+ message << "=================== Git revision"
112
+ message << `git log --pretty=format:'%H' -n 1`
113
+ message << "=================== Exception Class"
114
+ message << self.error_class
115
+ message << "=================== Exception Message"
116
+ message << self.error_message
117
+ message << "=================== URL"
118
+ message << self.url
119
+ message << "=================== Parameters"
120
+ message << args[:rack_env]['REQUEST_METHOD']
121
+ message << self.parameters
122
+ message << "=================== Component"
123
+ message << self.component
124
+ message << "=================== Action"
125
+ message << self.action
126
+ message << "=================== Ruby version"
127
+ message << RUBY_VERSION
128
+ message << "=================== Rails environment"
129
+ message << Rails.env
130
+ message << "=================== User information"
131
+ message << args[:rack_env]['HTTP_USER_AGENT']
132
+ message << args[:rack_env]['HTTP_ACCEPT_CHARSET']
133
+ message << "=================== Backtrace"
134
+ message << self.backtrace
135
+ message << "=================== ARGS"
136
+ message << self.args
137
+ message.join("\n\n")
138
+ end
139
+ end # Notice
140
+ end
@@ -0,0 +1,48 @@
1
+ module Graylog2Rails
2
+ class Middleware
3
+ attr_reader :args
4
+
5
+ def initialize(app, args = {})
6
+ @app, @args = app, args
7
+ end
8
+
9
+ def call(env)
10
+ dup._call(env)
11
+ end
12
+
13
+ def _call(env)
14
+ begin
15
+ @app.call(env)
16
+ rescue Exception => err
17
+ send_to_graylog2(err, env)
18
+ raise
19
+ end
20
+ end
21
+
22
+ def send_to_graylog2 err, env
23
+ opts = {:rack_env => env}
24
+ begin
25
+ opts = opts.merge(:exception => err) if err.is_a?(Exception)
26
+ opts = opts.merge(err.to_hash) if err.respond_to?(:to_hash)
27
+ notice = Graylog2Rails::Message.new(opts)
28
+ args = {
29
+ :short_message => notice.message,
30
+ :full_message => notice.to_s,
31
+ :facility => @args.delete("facility") + "_exception",
32
+ :level => @args.delete("level"),
33
+ :host => @args.delete("local_app_name"),
34
+ :file => err.backtrace[0].split(":")[0],
35
+ :line => err.backtrace[0].split(":")[1],
36
+ }
37
+ Rails.logger.tagged("GRAYLOG") do
38
+ Rails.logger.info args.inspect
39
+ end
40
+ notifier = GELF::Notifier.new(@args.delete("hostname"), @args.delete("port"), @args.delete("max_chunk_size"))
41
+ notifier.notify!(args)
42
+ rescue => i_err
43
+ puts "Graylog2 Exception logger. Could not send message: " + i_err.message
44
+ puts i_err.backtrace.join("\n")
45
+ end
46
+ end
47
+ end # class Middleware
48
+ end
@@ -0,0 +1,26 @@
1
+ module Graylog2Rails
2
+ class Notifier
3
+ attr_reader :gelf
4
+
5
+ def initialize(args = {})
6
+ @gelf = GELF::Notifier.new(Graylog2Rails.gelf_config_options["host"],
7
+ Graylog2Rails.gelf_config_options["port"],
8
+ Graylog2Rails.gelf_config_options["max_chunk_size"], {
9
+ facility: Graylog2Rails.gelf_config_options["facility"],
10
+ level: args.has_key?("level") ? args["level"] : Graylog2Rails.gelf_config_options["level"],
11
+ host: Graylog2Rails.gelf_config_options["local_app_name"]
12
+ })
13
+ end
14
+
15
+ def self.notify! args
16
+ @notifier ||= new
17
+ timestamp = Time.now.utc
18
+ Rails.logger.tagged("GRAYLOG") do
19
+ Rails.logger.info "[#{timestamp.to_datetime}] #{args.inspect}"
20
+ end
21
+ args.merge!({timestamp: timestamp.to_f})
22
+ @notifier.gelf.notify! args unless Rails.env.development?
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Graylog2Rails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ namespace :graylog2_rails do
2
+ desc "Install Graylog2"
3
+ task :install do
4
+ system "rails g graylog2_rails:install"
5
+ end
6
+ end
@@ -0,0 +1,11 @@
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
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graylog2-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Artem Melnikov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gelf
16
+ requirement: &70237560199040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70237560199040
25
+ - !ruby/object:Gem::Dependency
26
+ name: awesome_print
27
+ requirement: &70237560198620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70237560198620
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
38
+ requirement: &70237560198080 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70237560198080
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &70237560197640 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70237560197640
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &70237560197140 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '2.0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70237560197140
69
+ description: Wrapper for Graylog2 logging system
70
+ email:
71
+ - artem.melnikov@ignar.name
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - config/graylog2_rails.yml
83
+ - graylog2-rails.gemspec
84
+ - lib/generators/graylog2_rails/install_generator.rb
85
+ - lib/generators/graylog2_rails/templates/config.erb
86
+ - lib/graylog2-rails.rb
87
+ - lib/graylog2-rails/engine.rb
88
+ - lib/graylog2-rails/initializers.rb
89
+ - lib/graylog2-rails/message.rb
90
+ - lib/graylog2-rails/middleware.rb
91
+ - lib/graylog2-rails/notifier.rb
92
+ - lib/graylog2-rails/version.rb
93
+ - lib/tasks/graylog2_rails.rake
94
+ - spec/spec_helper.rb
95
+ homepage: https://github.com/ignar/graylog2-rails
96
+ licenses: []
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ segments:
108
+ - 0
109
+ hash: 2160470504534337998
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
117
+ - 0
118
+ hash: 2160470504534337998
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.15
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Wrapper for Graylog2 logging system
125
+ test_files:
126
+ - spec/spec_helper.rb