gray_logger 0.11.0 → 0.11.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.
- data/lib/gray_logger.rb +19 -0
- data/lib/gray_logger/configuration.rb +26 -0
- data/lib/gray_logger/helper_methods.rb +12 -2
- data/lib/gray_logger/logger.rb +8 -18
- data/lib/gray_logger/proxy.rb +51 -0
- data/lib/gray_logger/rack.rb +6 -64
- data/lib/gray_logger/railtie.rb +2 -1
- metadata +6 -4
data/lib/gray_logger.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'gelf'
|
2
2
|
|
3
3
|
require 'gray_logger/support'
|
4
|
+
require 'gray_logger/proxy'
|
5
|
+
require 'gray_logger/configuration'
|
4
6
|
require 'gray_logger/bucket'
|
5
7
|
require 'gray_logger/message'
|
6
8
|
require 'gray_logger/logger'
|
@@ -11,3 +13,20 @@ if defined?(Rails)
|
|
11
13
|
require 'gray_logger/rails_modules'
|
12
14
|
require 'gray_logger/railtie' if defined?(Rails::Railtie)
|
13
15
|
end
|
16
|
+
|
17
|
+
module GrayLogger
|
18
|
+
|
19
|
+
class << self
|
20
|
+
attr_accessor :configuration
|
21
|
+
end
|
22
|
+
|
23
|
+
# GrayLogger.configure({:host => '127.0.0.1'}) do |config|
|
24
|
+
# config.port = "11200"
|
25
|
+
# end
|
26
|
+
def self.configure(config)
|
27
|
+
self.configuration = ::GrayLogger::Configuration.new(config)
|
28
|
+
yield(configuration) if block_given?
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module GrayLogger
|
2
|
+
class Configuration
|
3
|
+
include ::GrayLogger::Support
|
4
|
+
|
5
|
+
attr_accessor :host, :port, :size, :automatic_logging, :options
|
6
|
+
def initialize(configuration_hash)
|
7
|
+
defaults = {
|
8
|
+
:size => "WAN",
|
9
|
+
:facility => "facility-not-defined"
|
10
|
+
}
|
11
|
+
|
12
|
+
config = symbolize_keys(configuration_hash)
|
13
|
+
config = defaults.merge(config)
|
14
|
+
|
15
|
+
[:host, :port, :size, :automatic_logging].each do |method|
|
16
|
+
send("#{method}=", config.delete(method))
|
17
|
+
end
|
18
|
+
self.options = config
|
19
|
+
end
|
20
|
+
|
21
|
+
def automatic_logging?
|
22
|
+
@automatic_logging.nil? ? true : !!@automatic_logging
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -1,8 +1,18 @@
|
|
1
1
|
module GrayLogger
|
2
2
|
module HelperMethods
|
3
|
+
|
4
|
+
protected
|
5
|
+
def gray_logger_proxy
|
6
|
+
if Rails.version.to_i >= 3
|
7
|
+
env["rack.gray_logger.proxy"]
|
8
|
+
else
|
9
|
+
request.env["rack.gray_logger.proxy"]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
3
13
|
def gray_logger
|
4
|
-
|
14
|
+
gray_logger_proxy.gray_logger
|
5
15
|
end
|
6
|
-
|
16
|
+
|
7
17
|
end
|
8
18
|
end
|
data/lib/gray_logger/logger.rb
CHANGED
@@ -2,34 +2,24 @@ module GrayLogger
|
|
2
2
|
|
3
3
|
class Logger < GELF::Logger
|
4
4
|
include ::GrayLogger::Support
|
5
|
-
attr_reader :buckets, :
|
5
|
+
attr_reader :buckets, :configuration
|
6
6
|
|
7
|
-
def initialize(configuration
|
8
|
-
|
9
|
-
@
|
10
|
-
|
11
|
-
defaults = {
|
12
|
-
:size => "WAN",
|
13
|
-
:facility => "facility-not-defined"
|
14
|
-
}
|
15
|
-
|
16
|
-
config = symbolize_keys(configuration)
|
17
|
-
config = defaults.merge(config)
|
18
|
-
|
19
|
-
super(config.delete(:host), config.delete(:port), config.delete(:size), config)
|
7
|
+
def initialize(configuration)
|
8
|
+
super(configuration.host, configuration.port, configuration.size, configuration.options)
|
9
|
+
@configuration = configuration
|
20
10
|
|
21
11
|
@buckets = {}
|
22
12
|
end
|
23
13
|
|
24
|
-
def automatic_logging?
|
25
|
-
!!@automatic_logging
|
26
|
-
end
|
27
|
-
|
28
14
|
def reset!
|
29
15
|
@buckets = {}
|
30
16
|
self
|
31
17
|
end
|
32
18
|
|
19
|
+
def automatic_logging?
|
20
|
+
configuration.automatic_logging?
|
21
|
+
end
|
22
|
+
|
33
23
|
# logger.after_request_log << {:my_field => 'field content'}
|
34
24
|
# logger.after_request_log.my_field = 'field content'
|
35
25
|
def after_request_log
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module GrayLogger
|
2
|
+
class Proxy
|
3
|
+
attr_accessor :proxied_logger, :gray_logger
|
4
|
+
|
5
|
+
def initialize(attributes={})
|
6
|
+
self.proxied_logger = attributes[:proxied_logger]
|
7
|
+
self.gray_logger = attributes[:gray_logger]
|
8
|
+
end
|
9
|
+
|
10
|
+
# def debug(*args)
|
11
|
+
# if proxied_logger.nil?
|
12
|
+
# gray_logger.send(:debug, *args)
|
13
|
+
# else
|
14
|
+
# after_request_log.append_to(:log_file, "[debug] #{args[0]}") unless gray_logger.nil?
|
15
|
+
# proxied_logger.send(:debug, *args)
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
GELF::Levels.constants.each do |const|
|
19
|
+
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
20
|
+
def #{const.downcase}(*args)
|
21
|
+
if proxied_logger.nil?
|
22
|
+
gray_logger.send(:#{const.downcase}, *args)
|
23
|
+
else
|
24
|
+
gray_logger.after_request_log.append_to(:log_file, "[#{const.downcase}] \#{args[0]}") unless gray_logger.nil?
|
25
|
+
proxied_logger.send(:#{const.downcase}, *args)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
EOT
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
# delegate every method the proxy doesn't know to gray_logger and proxied_logger. let them handle this.
|
33
|
+
def method_missing(method_name, *args, &block)
|
34
|
+
unless proxied_logger.nil?
|
35
|
+
begin
|
36
|
+
proxied_logger.send(method_name, *args, &block)
|
37
|
+
rescue => e
|
38
|
+
proxied_logger.error(e.backtrace.join("\n"))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.proxy= proxy
|
45
|
+
@proxy = proxy
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.proxy
|
49
|
+
@proxy ||= ::GrayLogger::Proxy.new
|
50
|
+
end
|
51
|
+
end
|
data/lib/gray_logger/rack.rb
CHANGED
@@ -1,21 +1,19 @@
|
|
1
1
|
module Rack
|
2
2
|
module GrayLogger
|
3
|
+
|
3
4
|
class Middleware
|
4
5
|
include ::GrayLogger::Support
|
5
6
|
|
6
7
|
attr_accessor :gray_logger
|
7
8
|
|
8
|
-
def initialize(app
|
9
|
+
def initialize(app)
|
9
10
|
@app = app
|
10
|
-
|
11
|
-
configuration = symbolize_keys(options.delete(:configuration))
|
12
|
-
self.gray_logger = ::GrayLogger::Logger.new(configuration)
|
13
|
-
|
14
|
-
::Rack::GrayLogger.proxy.gray_logger = gray_logger
|
11
|
+
::GrayLogger.proxy.gray_logger = ::GrayLogger::Logger.new(::GrayLogger.configuration.dup)
|
15
12
|
end
|
16
13
|
|
17
14
|
def call(env)
|
18
|
-
|
15
|
+
env["rack.gray_logger.proxy"] = ::GrayLogger.proxy
|
16
|
+
gray_logger = ::GrayLogger.proxy.gray_logger
|
19
17
|
begin
|
20
18
|
status, headers, body = @app.call(env)
|
21
19
|
rescue => e
|
@@ -23,7 +21,7 @@ module Rack
|
|
23
21
|
raise
|
24
22
|
ensure
|
25
23
|
req = Rack::Request.new(env)
|
26
|
-
if
|
24
|
+
if ::GrayLogger.configuration.automatic_logging?
|
27
25
|
gray_logger.log_exception(env['rack.exception'])
|
28
26
|
gray_logger.after_request_log.status_code = status.to_i
|
29
27
|
gray_logger.after_request_log.short_message = "Request: #{req.path} (#{status.to_i})" if gray_logger.after_request_log[:short_message].nil?
|
@@ -38,61 +36,5 @@ module Rack
|
|
38
36
|
|
39
37
|
end
|
40
38
|
|
41
|
-
class Proxy
|
42
|
-
attr_accessor :proxied_logger, :gray_logger
|
43
|
-
|
44
|
-
def initialize(attributes={})
|
45
|
-
self.proxied_logger = attributes[:proxied_logger]
|
46
|
-
self.gray_logger = attributes[:gray_logger]
|
47
|
-
end
|
48
|
-
|
49
|
-
# def debug(*args)
|
50
|
-
# if proxied_logger.nil?
|
51
|
-
# super(*args)
|
52
|
-
# else
|
53
|
-
# after_request_log.append_to(:log_file, "[debug] #{args[0]}") unless gray_logger.nil?
|
54
|
-
# proxied_logger.send(:debug, *args)
|
55
|
-
# end
|
56
|
-
# end
|
57
|
-
GELF::Levels.constants.each do |const|
|
58
|
-
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
59
|
-
def #{const.downcase}(*args)
|
60
|
-
if proxied_logger.nil?
|
61
|
-
gray_logger.send(:#{const.downcase}, *args)
|
62
|
-
else
|
63
|
-
gray_logger.after_request_log.append_to(:log_file, "[#{const.downcase}] \#{args[0]}") unless gray_logger.nil?
|
64
|
-
proxied_logger.send(:#{const.downcase}, *args)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
EOT
|
68
|
-
end
|
69
|
-
|
70
|
-
private
|
71
|
-
def method_missing(method_name, *args, &block)
|
72
|
-
unless gray_logger.nil?
|
73
|
-
begin
|
74
|
-
gray_logger.send(method_name, *args, &block)
|
75
|
-
rescue => e
|
76
|
-
gray_logger.handle_exception(e)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
unless proxied_logger.nil?
|
80
|
-
begin
|
81
|
-
proxied_logger.send(method_name, *args, &block)
|
82
|
-
rescue => e
|
83
|
-
proxied_logger.error(e.backtrace.join("\n"))
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def self.proxy= proxy
|
90
|
-
@proxy = proxy
|
91
|
-
end
|
92
|
-
|
93
|
-
def self.proxy
|
94
|
-
@proxy ||= ::Rack::GrayLogger::Proxy.new
|
95
|
-
end
|
96
|
-
|
97
39
|
end
|
98
40
|
end
|
data/lib/gray_logger/railtie.rb
CHANGED
@@ -5,7 +5,8 @@ module GrayLogger
|
|
5
5
|
begin
|
6
6
|
initializer "gray_logger.configure_rails_initialization" do |app|
|
7
7
|
configuration = YAML.load(File.read(Rails.root.join('config/gray_logger.yml')))[Rails.env]
|
8
|
-
|
8
|
+
::GrayLogger.configure(configuration)
|
9
|
+
app.middleware.insert_after "ActionDispatch::ShowExceptions", "Rack::GrayLogger::Middleware"
|
9
10
|
end
|
10
11
|
rescue => e
|
11
12
|
$stderr.puts("GrayLogger not configured. Please add config/gray_logger.yml")
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gray_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 49
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 11
|
9
|
-
-
|
10
|
-
version: 0.11.
|
9
|
+
- 1
|
10
|
+
version: 0.11.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Benjamin Behr
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-11-
|
18
|
+
date: 2012-11-19 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: gelf
|
@@ -41,9 +41,11 @@ extra_rdoc_files: []
|
|
41
41
|
|
42
42
|
files:
|
43
43
|
- lib/gray_logger/bucket.rb
|
44
|
+
- lib/gray_logger/configuration.rb
|
44
45
|
- lib/gray_logger/helper_methods.rb
|
45
46
|
- lib/gray_logger/logger.rb
|
46
47
|
- lib/gray_logger/message.rb
|
48
|
+
- lib/gray_logger/proxy.rb
|
47
49
|
- lib/gray_logger/rack.rb
|
48
50
|
- lib/gray_logger/rails_modules.rb
|
49
51
|
- lib/gray_logger/railtie.rb
|