logjam_agent 0.6.9 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/logjam_agent/buffered_logger.rb +38 -6
- data/lib/logjam_agent/middleware.rb +3 -1
- data/lib/logjam_agent/rack/logger.rb +37 -16
- data/lib/logjam_agent/railtie.rb +5 -3
- data/lib/logjam_agent/request.rb +5 -4
- data/lib/logjam_agent/syslog_like_formatter.rb +10 -1
- data/lib/logjam_agent/version.rb +1 -1
- data/logjam_agent.gemspec +1 -1
- metadata +8 -8
@@ -1,16 +1,44 @@
|
|
1
|
-
require 'active_support/buffered_logger'
|
2
|
-
require 'active_support/core_ext/logger'
|
3
1
|
require 'fileutils'
|
4
2
|
|
3
|
+
if ActiveSupport::VERSION::STRING < "4.0"
|
4
|
+
require 'active_support/buffered_logger'
|
5
|
+
require 'active_support/core_ext/logger'
|
6
|
+
else
|
7
|
+
require 'active_support/logger'
|
8
|
+
|
9
|
+
class LogjamAgent::ConsoleFormatter < Logger::Formatter
|
10
|
+
# This method is invoked when a log event occurs
|
11
|
+
def call(severity, timestamp, progname, msg)
|
12
|
+
"[#{format_time(timestamp)}] #{String === msg ? msg : msg.inspect}\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
def format_time(timestamp)
|
16
|
+
timestamp.strftime("%H:%M:%S.#{"%06d" % timestamp.usec}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class ActiveSupport::Logger
|
21
|
+
class << self
|
22
|
+
alias_method :original_broadcast, :broadcast
|
23
|
+
def broadcast(logger)
|
24
|
+
logger.formatter = LogjamAgent::ConsoleFormatter.new
|
25
|
+
logger.formatter.extend(ActiveSupport::TaggedLogging::Formatter)
|
26
|
+
original_broadcast(logger)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
5
32
|
module LogjamAgent
|
6
|
-
class BufferedLogger < ActiveSupport::
|
33
|
+
class BufferedLogger < ( ActiveSupport::VERSION::STRING < "4.0" ?
|
34
|
+
ActiveSupport::BufferedLogger : ActiveSupport::Logger )
|
7
35
|
|
8
36
|
attr_accessor :formatter
|
9
37
|
|
10
38
|
def initialize(*args)
|
11
39
|
super(*args)
|
12
40
|
# stupid bug in the buffered logger code (Rails::VERSION::STRING < "3.2")
|
13
|
-
@log.write "\n" if respond_to?(:buffer)
|
41
|
+
@log.write "\n" if @log && respond_to?(:buffer)
|
14
42
|
@formatter = lambda{|_, _, _, message| message}
|
15
43
|
end
|
16
44
|
|
@@ -36,6 +64,8 @@ module LogjamAgent
|
|
36
64
|
|
37
65
|
def add(severity, message = nil, progname = nil, &block)
|
38
66
|
return if level > severity
|
67
|
+
message = progname if message.nil?
|
68
|
+
progname = nil # rails 3.2 bug when using tagged logging
|
39
69
|
request = self.request || Thread.main.thread_variable_get(:logjam_request)
|
40
70
|
if message.is_a?(Exception)
|
41
71
|
request.add_exception(message.class.to_s) if request
|
@@ -51,8 +81,10 @@ module LogjamAgent
|
|
51
81
|
if respond_to?(:buffer)
|
52
82
|
buffer << formatted_message << "\n"
|
53
83
|
auto_flush
|
54
|
-
|
84
|
+
elsif @log # @log is a logger (or nil for rails 4)
|
55
85
|
@log << "#{formatted_message}\n"
|
86
|
+
elsif @logdev
|
87
|
+
@logdev.write(formatted_message)
|
56
88
|
end
|
57
89
|
request.add_line(severity, time, message) if request
|
58
90
|
message
|
@@ -63,7 +95,7 @@ module LogjamAgent
|
|
63
95
|
if respond_to?(:buffer)
|
64
96
|
@log = log_device
|
65
97
|
else
|
66
|
-
@log.instance_eval do
|
98
|
+
(@log||self).instance_eval do
|
67
99
|
raise "cannot set log device" unless defined?(@logdev)
|
68
100
|
@logdev = log_device
|
69
101
|
end
|
@@ -15,7 +15,9 @@ module LogjamAgent
|
|
15
15
|
ensure
|
16
16
|
headers = result[1]
|
17
17
|
headers["X-Logjam-Request-Id"] = request.id
|
18
|
-
|
18
|
+
unless (caller_id = request.fields[:caller_id]).blank?
|
19
|
+
headers["X-Logjam-Caller-Id"] = caller_id
|
20
|
+
end
|
19
21
|
finish_request(env)
|
20
22
|
end
|
21
23
|
|
@@ -4,36 +4,60 @@ module LogjamAgent
|
|
4
4
|
|
5
5
|
module Rack
|
6
6
|
class Logger < ActiveSupport::LogSubscriber
|
7
|
-
def initialize(app)
|
7
|
+
def initialize(app, taggers = nil)
|
8
8
|
@app = app
|
9
|
+
@taggers = taggers || Rails.application.config.log_tags || []
|
9
10
|
@hostname = LogjamAgent.hostname
|
10
11
|
end
|
11
12
|
|
12
13
|
def call(env)
|
14
|
+
request = ActionDispatch::Request.new(env)
|
15
|
+
|
16
|
+
if logger.respond_to?(:tagged) && !@taggers.empty?
|
17
|
+
logger.tagged(compute_tags(request)) { call_app(request, env) }
|
18
|
+
else
|
19
|
+
call_app(request, env)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def call_app(request, env)
|
13
26
|
start_time = Time.now
|
14
|
-
before_dispatch(env, start_time)
|
27
|
+
before_dispatch(request, env, start_time)
|
15
28
|
result = @app.call(env)
|
16
29
|
ensure
|
17
30
|
run_time = Time.now - start_time
|
18
31
|
after_dispatch(env, result, run_time*1000)
|
19
32
|
end
|
20
33
|
|
21
|
-
|
34
|
+
def compute_tags(request)
|
35
|
+
@taggers.collect do |tag|
|
36
|
+
case tag
|
37
|
+
when :uuid
|
38
|
+
Rails.logger.request.uuid
|
39
|
+
when Proc
|
40
|
+
tag.call(request)
|
41
|
+
when Symbol
|
42
|
+
request.send(tag)
|
43
|
+
else
|
44
|
+
tag
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
22
48
|
|
23
|
-
def before_dispatch(env, start_time)
|
49
|
+
def before_dispatch(request, env, start_time)
|
24
50
|
TimeBandits.reset
|
25
|
-
|
26
51
|
Thread.current.thread_variable_set(:time_bandits_completed_info, nil)
|
27
52
|
|
28
|
-
request = ActionDispatch::Request.new(env)
|
29
|
-
|
30
53
|
path = request.filtered_path
|
31
54
|
|
32
55
|
logjam_fields = Rails.logger.request.fields
|
33
56
|
logjam_fields.merge!(:started_at => start_time.iso8601, :ip => request.remote_ip, :host => @hostname)
|
34
57
|
logjam_fields.merge!(extract_request_info(request))
|
35
58
|
|
36
|
-
|
59
|
+
debug ""
|
60
|
+
info "Started #{request.request_method} \"#{path}\" for #{request.ip} at #{start_time.to_default_s}"
|
37
61
|
end
|
38
62
|
|
39
63
|
def after_dispatch(env, result, run_time_ms)
|
@@ -78,8 +102,8 @@ module LogjamAgent
|
|
78
102
|
end
|
79
103
|
|
80
104
|
result
|
81
|
-
rescue Exception
|
82
|
-
Rails.logger.error(
|
105
|
+
rescue Exception => e
|
106
|
+
Rails.logger.error(e)
|
83
107
|
result
|
84
108
|
end
|
85
109
|
|
@@ -128,7 +152,7 @@ require 'action_controller/log_subscriber'
|
|
128
152
|
module ActionController #:nodoc:
|
129
153
|
|
130
154
|
class LogSubscriber
|
131
|
-
if Rails::VERSION::STRING =~
|
155
|
+
if Rails::VERSION::STRING =~ /\A3\.0/
|
132
156
|
def start_processing(event)
|
133
157
|
payload = event.payload
|
134
158
|
params = payload[:params].except(*INTERNAL_PARAMS)
|
@@ -138,14 +162,13 @@ module ActionController #:nodoc:
|
|
138
162
|
full_name = "#{controller}##{action}"
|
139
163
|
action_name = LogjamAgent.action_name_proc.call(full_name)
|
140
164
|
|
141
|
-
# puts "setting logjam action to #{action_name}"
|
142
165
|
Rails.logger.request.fields[:action] = action_name
|
143
166
|
|
144
167
|
info " Processing by #{full_name} as #{payload[:formats].first.to_s.upcase}"
|
145
168
|
info " Parameters: #{params.inspect}" unless params.empty?
|
146
169
|
end
|
147
170
|
|
148
|
-
elsif Rails::VERSION::STRING =~
|
171
|
+
elsif Rails::VERSION::STRING =~ /\A3\.1/
|
149
172
|
|
150
173
|
def start_processing(event)
|
151
174
|
payload = event.payload
|
@@ -158,14 +181,13 @@ module ActionController #:nodoc:
|
|
158
181
|
full_name = "#{controller}##{action}"
|
159
182
|
action_name = LogjamAgent.action_name_proc.call(full_name)
|
160
183
|
|
161
|
-
# puts "setting logjam action to #{action_name}"
|
162
184
|
Rails.logger.request.fields[:action] = action_name
|
163
185
|
|
164
186
|
info " Processing by #{full_name} as #{format}"
|
165
187
|
info " Parameters: #{params.inspect}" unless params.empty?
|
166
188
|
end
|
167
189
|
|
168
|
-
elsif Rails::VERSION::STRING =~
|
190
|
+
elsif Rails::VERSION::STRING =~ /\A(3\.2|4\.0)/
|
169
191
|
|
170
192
|
def start_processing(event)
|
171
193
|
payload = event.payload
|
@@ -178,7 +200,6 @@ module ActionController #:nodoc:
|
|
178
200
|
full_name = "#{controller}##{action}"
|
179
201
|
action_name = LogjamAgent.action_name_proc.call(full_name)
|
180
202
|
|
181
|
-
# puts "setting logjam action to #{action_name}"
|
182
203
|
Rails.logger.request.fields[:action] = action_name
|
183
204
|
|
184
205
|
info "Processing by #{full_name} as #{format}"
|
data/lib/logjam_agent/railtie.rb
CHANGED
@@ -15,13 +15,15 @@ module LogjamAgent
|
|
15
15
|
paths = app.config.paths
|
16
16
|
path = (Rails::VERSION::STRING < "3.1" ? paths.log.to_a : paths['log']).first.to_s
|
17
17
|
logger = LogjamAgent::BufferedLogger.new(path)
|
18
|
-
logger.level =
|
18
|
+
logger.level = ::Logger.const_get(app.config.log_level.to_s.upcase)
|
19
19
|
logger.formatter = LogjamAgent::SyslogLikeFormatter.new
|
20
20
|
logger.auto_flushing = false if Rails.env.production? && Rails::VERSION::STRING < "3.2"
|
21
|
+
logger = ActiveSupport::TaggedLogging.new(logger) if Rails::VERSION::STRING >= "3.2"
|
21
22
|
logger
|
22
|
-
rescue StandardError
|
23
|
+
rescue StandardError
|
23
24
|
logger = LogjamAgent::BufferedLogger.new(STDERR)
|
24
|
-
logger
|
25
|
+
logger = ActiveSupport::TaggedLogging.new(logger) if Rails::VERSION::STRING >= "3.2"
|
26
|
+
logger.level = ::Logger::WARN
|
25
27
|
logger.warn(
|
26
28
|
"Logging Error: Unable to access log file. Please ensure that #{path} exists and is writable. " +
|
27
29
|
"The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
|
data/lib/logjam_agent/request.rb
CHANGED
@@ -6,7 +6,7 @@ end
|
|
6
6
|
|
7
7
|
module LogjamAgent
|
8
8
|
class Request
|
9
|
-
attr_reader :fields
|
9
|
+
attr_reader :fields, :uuid
|
10
10
|
|
11
11
|
def initialize(app, env, logger, initial_fields)
|
12
12
|
@logger = logger
|
@@ -14,13 +14,13 @@ module LogjamAgent
|
|
14
14
|
@env = env
|
15
15
|
@forwarder = Forwarders.get(app, env)
|
16
16
|
@lines = []
|
17
|
-
@
|
18
|
-
@fields = initial_fields.merge(:request_id => @
|
17
|
+
@uuid = UUID4R::uuid(1).gsub('-','')
|
18
|
+
@fields = initial_fields.merge(:request_id => @uuid, :host => LogjamAgent.hostname, :process_id => Process.pid, :lines => @lines)
|
19
19
|
@mutex = Mutex.new
|
20
20
|
end
|
21
21
|
|
22
22
|
def id
|
23
|
-
"#{@app}-#{@env}-#{@
|
23
|
+
"#{@app}-#{@env}-#{@uuid}"
|
24
24
|
end
|
25
25
|
|
26
26
|
def action
|
@@ -49,6 +49,7 @@ module LogjamAgent
|
|
49
49
|
|
50
50
|
def forward
|
51
51
|
engine = @fields.delete(:engine)
|
52
|
+
# puts @fields.inspect
|
52
53
|
@forwarder.forward(LogjamAgent.encode_payload(@fields), :engine => engine)
|
53
54
|
rescue Exception => e
|
54
55
|
handle_forwarding_error(e)
|
@@ -6,6 +6,7 @@ module LogjamAgent
|
|
6
6
|
@hostname = LogjamAgent.hostname
|
7
7
|
@app_name = "rails"
|
8
8
|
@attributes = []
|
9
|
+
@newline = ActiveSupport::VERSION::STRING < "4.0" ? "" : "\n"
|
9
10
|
end
|
10
11
|
|
11
12
|
attr_accessor :attributes
|
@@ -29,7 +30,15 @@ module LogjamAgent
|
|
29
30
|
end
|
30
31
|
|
31
32
|
def call(severity, timestamp, progname, msg)
|
32
|
-
"#{format_severity(severity)} #{format_time(timestamp)}
|
33
|
+
"#{format_severity(severity)} #{format_time(timestamp)}#{render_attributes}#{format_host_info(progname)}: #{format_message(msg)}#{@newline}"
|
34
|
+
end
|
35
|
+
|
36
|
+
if Rails.env.development?
|
37
|
+
def format_host_info(proganme); ""; end
|
38
|
+
else
|
39
|
+
def format_host_info(proganme)
|
40
|
+
" #{@hostname} #{progname||@app_name}[#{$$}]"
|
41
|
+
end
|
33
42
|
end
|
34
43
|
|
35
44
|
def render_attributes
|
data/lib/logjam_agent/version.rb
CHANGED
data/logjam_agent.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logjam_agent
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 7
|
9
|
+
- 0
|
10
|
+
version: 0.7.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Stefan Kaes
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-
|
18
|
+
date: 2013-07-20 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rake
|
@@ -81,11 +81,11 @@ dependencies:
|
|
81
81
|
requirements:
|
82
82
|
- - ">="
|
83
83
|
- !ruby/object:Gem::Version
|
84
|
-
hash:
|
84
|
+
hash: 3
|
85
85
|
segments:
|
86
86
|
- 0
|
87
|
-
-
|
88
|
-
version: "0.
|
87
|
+
- 4
|
88
|
+
version: "0.4"
|
89
89
|
type: :runtime
|
90
90
|
version_requirements: *id005
|
91
91
|
description: Logjam logger and request information forwarding
|