logjam_agent 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ module LogjamAgent
2
+ module Rack
3
+ class Logger < ActiveSupport::LogSubscriber
4
+ def initialize(app)
5
+ @app = app
6
+ @hostname = Socket.gethostname
7
+ end
8
+
9
+ def call(env)
10
+ start_time = Time.now
11
+ before_dispatch(env, start_time)
12
+ result = @app.call(env)
13
+ ensure
14
+ run_time = Time.now - start_time
15
+ after_dispatch(env, result, run_time*1000)
16
+ end
17
+
18
+ protected
19
+
20
+ def before_dispatch(env, start_time)
21
+ TimeBandits.reset
22
+
23
+ Thread.current[:time_bandits_completed_info] = nil
24
+
25
+ request = ActionDispatch::Request.new(env)
26
+ path = request.filtered_path
27
+
28
+ Rails.logger.request.fields.merge!(:started_at => start_time, :ip => request.ip, :host => @hostname)
29
+
30
+ info "\n\nStarted #{request.request_method} \"#{path}\" for #{request.ip} at #{start_time.to_default_s}"
31
+ end
32
+
33
+ def after_dispatch(env, result, run_time_ms)
34
+ status = result ? result.first : 500
35
+ duration, additions, view_time, action = Thread.current[:time_bandits_completed_info]
36
+
37
+ basic_request_info = {:total_time => run_time_ms, :code => status, :action => action, :view_time => view_time || 0.0}
38
+
39
+ message = "Completed #{status} #{::Rack::Utils::HTTP_STATUS_CODES[status]} in %.1fms" % run_time_ms
40
+ message << " (#{additions.join(' | ')})" unless additions.blank?
41
+ info message
42
+
43
+ ActiveSupport::LogSubscriber.flush_all!
44
+
45
+ Rails.logger.request.fields.merge!(basic_request_info)
46
+
47
+ env["time_bandits.metrics"] = TimeBandits.metrics
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,36 @@
1
+ require 'logjam_agent'
2
+ module LogjamAgent
3
+ class Railtie < Rails::Railtie
4
+
5
+ initializer "initialize_logjam_agent_logger", :before => :initialize_logger do |app|
6
+ Rails.logger ||= app.config.logger ||
7
+ begin
8
+ path = app.config.paths.log.to_a.first
9
+ logger = LogjamAgent::BufferedLogger.new(path)
10
+ logger.level = ActiveSupport::BufferedLogger.const_get(app.config.log_level.to_s.upcase)
11
+ logger.auto_flushing = false if Rails.env.production?
12
+ logger
13
+ rescue StandardError => e
14
+ logger = LogjamAgent::BufferedLogger.new(STDERR)
15
+ logger.level = ActiveSupport::BufferedLogger::WARN
16
+ logger.warn(
17
+ "Logging Error: Unable to access log file. Please ensure that #{path} exists and is writable. " +
18
+ "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
19
+ )
20
+ logger
21
+ end
22
+ end
23
+
24
+ initializer "logjam_agent", :after => "time_bandits" do |app|
25
+ app.config.middleware.swap("TimeBandits::Rack::Logger", "LogjamAgent::Rack::Logger")
26
+ app.config.middleware.insert_before("LogjamAgent::Rack::Logger", "LogjamAgent::Middleware")
27
+
28
+ ActiveSupport.on_load(:action_controller) do
29
+ require 'logjam_agent/middleware.rb'
30
+ require 'logjam_agent/rack/logger.rb'
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+
@@ -15,7 +15,7 @@ module LogjamAgent
15
15
  end
16
16
 
17
17
  def add_line(severity, timestamp, message)
18
- @lines << [severity, format_time(timestamp), message]
18
+ @lines << [severity, format_time(timestamp), message.strip]
19
19
  end
20
20
 
21
21
  def forward
@@ -1,3 +1,3 @@
1
1
  module LogjamAgent
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
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: 23
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 4
10
- version: 0.0.4
10
+ version: 0.1.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: 2011-08-29 00:00:00 +02:00
18
+ date: 2011-09-04 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -68,6 +68,8 @@ files:
68
68
  - lib/logjam_agent/buffered_logger.rb
69
69
  - lib/logjam_agent/forwarders.rb
70
70
  - lib/logjam_agent/middleware.rb
71
+ - lib/logjam_agent/rack/logger.rb
72
+ - lib/logjam_agent/railtie.rb
71
73
  - lib/logjam_agent/request.rb
72
74
  - lib/logjam_agent/syslog_like_formatter.rb
73
75
  - lib/logjam_agent/version.rb