brontes3d-production_log_analyzer 2009022403 → 2009072100

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ class RackLoggingPerProc
2
+
3
+ def self.logger_mutex
4
+ @@logger_mutex ||= Mutex.new
5
+ end
6
+
7
+ def self.logs
8
+ @@logs ||= []
9
+ end
10
+
11
+ def self.clear_logs
12
+ @@logs = []
13
+ end
14
+
15
+ def initialize app, log_path_prefix
16
+ @app = app
17
+ @log_path_prefix = log_path_prefix
18
+ @process_logger = false
19
+ end
20
+
21
+ def call env
22
+ RackLoggingPerProc.logger_mutex.synchronize do
23
+ unless @process_logger
24
+ @process_logger = ActiveSupport::BufferedLogger.new(@log_path_prefix + "_#{Process.pid}" + ".log")
25
+ @process_logger.debug("\nProcess logging started")
26
+ RAILS_DEFAULT_LOGGER.instance_eval do
27
+ class << self
28
+ def add(severity, message = nil, progname = nil, &block)
29
+ to_return = super
30
+ if to_return
31
+ RackLoggingPerProc.logger_mutex.synchronize do
32
+ RackLoggingPerProc.logs << [severity, to_return.strip]
33
+ end
34
+ end
35
+ to_return
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ start_time = Time.now
43
+ @app.call(env)
44
+ ensure
45
+ RackLoggingPerProc.logger_mutex.synchronize do
46
+ @process_logger.info("RECEIVE_REQUEST (#{Process.pid}) #{env['REQUEST_METHOD']} #{env['REQUEST_URI']} #{start_time}")
47
+ RackLoggingPerProc.logs.each do |to_log|
48
+ @process_logger.add(to_log[0], to_log[1])
49
+ end
50
+ @process_logger.info("#{Time.now} (#{Process.pid}) RESPONSE_SENT")
51
+ @process_logger.info("\n")
52
+ @process_logger.flush
53
+ RackLoggingPerProc.clear_logs
54
+ end
55
+ end
56
+
57
+ end
@@ -2,12 +2,11 @@ require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
3
  require 'rubygems'
4
4
  require 'active_support'
5
- require 'phusion_passenger/rack/request_handler'
6
5
 
7
- require 'passenger_log_per_proc'
6
+ require 'rack_logging_per_proc'
8
7
 
9
- SCRATCH_DIR = File.join(Dir.tmpdir, "passenger_log_proc_test")
10
- RAILS_LOG_LOCATION = File.join(SCRATCH_DIR, "test_passenger_rails.log")
8
+ SCRATCH_DIR = File.join(Dir.tmpdir, "rack_logging_proc_test")
9
+ RAILS_LOG_LOCATION = File.join(SCRATCH_DIR, "test_rack_rails.log")
11
10
  RAILS_DEFAULT_LOGGER = ActiveSupport::BufferedLogger.new(RAILS_LOG_LOCATION)
12
11
 
13
12
  class TestPassengerLogPerProc < Test::Unit::TestCase
@@ -21,32 +20,33 @@ class TestPassengerLogPerProc < Test::Unit::TestCase
21
20
  FileUtils.rm_rf(SCRATCH_DIR)
22
21
  end
23
22
 
24
- def run_a_request(count = 1)
23
+ def run_a_request(app, count = 1)
25
24
  Process.fork do
26
25
  count.times do |i|
27
- r, w = IO.pipe
28
- req_handler = PhusionPassenger::Rack::RequestHandler.new(w, Proc.new{
29
- RAILS_DEFAULT_LOGGER.debug("hi #{i} from #{Process.pid}")
30
- [200, {}, "hi"] })
31
26
  env = {}
32
27
  env["PATH_INFO"] = "/"
33
- input, output = IO.pipe
34
- req_handler.send(:process_request, env, input, output)
28
+ env["count"] = i
29
+ app.call(env)
35
30
  end
36
31
  end
37
32
  end
38
33
 
39
- def test_todo
40
- pid_before = run_a_request
34
+ def test_todo
35
+ app = lambda do |env|
36
+ RAILS_DEFAULT_LOGGER.debug("hi #{env['count']} from #{Process.pid}")
37
+ [200, {}, "hi"]
38
+ end
39
+
40
+ pid_before = run_a_request(app)
41
41
 
42
42
  Process.waitpid(pid_before)
43
43
 
44
- per_proc_logs_dir = File.join(SCRATCH_DIR, "passenger")
44
+ per_proc_logs_dir = File.join(SCRATCH_DIR, "per_proc")
45
45
  FileUtils.mkdir_p(per_proc_logs_dir)
46
- PassengerLogPerProc.enable("#{per_proc_logs_dir}/test")
47
-
48
- pid_after1 = run_a_request(2)
49
- pid_after2 = run_a_request(2)
46
+ middleware_instance = RackLoggingPerProc.new(app, "#{per_proc_logs_dir}/test")
47
+
48
+ pid_after1 = run_a_request(middleware_instance, 2)
49
+ pid_after2 = run_a_request(middleware_instance, 2)
50
50
 
51
51
  Process.waitpid(pid_after1)
52
52
  Process.waitpid(pid_after2)
@@ -74,14 +74,14 @@ class TestPassengerLogPerProc < Test::Unit::TestCase
74
74
  # puts "proc1_file_contents log: \n" + proc1_file_contents
75
75
  proc1_log_lines = proc1_file_contents.split("\n").reject{|l| l.match(/RECEIVE_REQUEST/) || l.match(/RESPONSE_SENT/) }
76
76
 
77
- assert_equal(["Passenger logging started", "hi 0 from #{pid_after1}", "", "hi 1 from #{pid_after1}"],
77
+ assert_equal(["Process logging started", "hi 0 from #{pid_after1}", "", "hi 1 from #{pid_after1}"],
78
78
  proc1_log_lines[1,5])
79
79
 
80
80
 
81
81
  # puts "proc2_file_contents log: \n" + proc2_file_contents
82
82
  proc2_log_lines = proc2_file_contents.split("\n").reject{|l| l.match(/RECEIVE_REQUEST/) || l.match(/RESPONSE_SENT/) }
83
83
 
84
- assert_equal(["Passenger logging started", "hi 0 from #{pid_after2}", "", "hi 1 from #{pid_after2}"],
84
+ assert_equal(["Process logging started", "hi 0 from #{pid_after2}", "", "hi 1 from #{pid_after2}"],
85
85
  proc2_log_lines[1,5])
86
86
  end
87
87
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brontes3d-production_log_analyzer
3
3
  version: !ruby/object:Gem::Version
4
- version: "2009022403"
4
+ version: "2009072100"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Hodel
@@ -61,8 +61,8 @@ files:
61
61
  - test/test_analyzer.rb
62
62
  - test/test_parser.rb
63
63
  - test/test_helper.rb
64
- - lib/passenger_log_per_proc.rb
65
- - test/test_passenger_log_per_proc.rb
64
+ - lib/rack_logging_per_proc.rb
65
+ - test/test_rack_logging_per_proc.rb
66
66
  - test/test_syslogs/test.syslog.0.14.x.log
67
67
  - test/test_syslogs/test.syslog.1.2.shortname.log
68
68
  - test/test_syslogs/test.syslog.empty.log
@@ -1,55 +0,0 @@
1
- class PassengerLogPerProc
2
-
3
- cattr_accessor :log_path_prefix
4
-
5
- def self.logger_mutex
6
- @@logger_mutex ||= Mutex.new
7
- end
8
-
9
- # with log_path_with_prefix like {RAILS_ROOT}/log/passenger/{RAILS_ENV}
10
- # you will get logs like {RAILS_ROOT}/log/passenger/{RAILS_ENV}_{Pid}.log
11
- def self.enable(log_path_with_prefix)
12
- PassengerLogPerProc.log_path_prefix = log_path_with_prefix
13
- PhusionPassenger::Rack::RequestHandler.class_eval do
14
- cattr_accessor :process_logger
15
-
16
- def process_request_with_extra_logging(env, input, output)
17
- unless self.class.process_logger
18
- PhusionPassenger::Rack::RequestHandler.process_logger = ActiveSupport::BufferedLogger.new(PassengerLogPerProc.log_path_prefix + "_#{Process.pid}" + ".log")
19
-
20
- PhusionPassenger::Rack::RequestHandler.process_logger.debug("\nPassenger logging started")
21
-
22
- RAILS_DEFAULT_LOGGER.instance_eval do
23
- class << self
24
- def add(severity, message = nil, progname = nil, &block)
25
- to_return = super
26
- if to_return
27
- Thread.current[:passenger_logs] ||= []
28
- Thread.current[:passenger_logs] << [severity, to_return.strip]
29
- end
30
- to_return
31
- end
32
- end
33
- end
34
- end
35
-
36
- Thread.current[:passenger_logs] ||= []
37
- start_time = Time.now
38
- process_request_without_extra_logging(env, input, output)
39
- ensure
40
- PassengerLogPerProc.logger_mutex.synchronize do
41
- PhusionPassenger::Rack::RequestHandler.process_logger.info("RECEIVE_REQUEST (#{Process.pid}) #{env['REQUEST_METHOD']} #{env['REQUEST_URI']} #{start_time}")
42
- Thread.current[:passenger_logs].each do |to_log|
43
- PhusionPassenger::Rack::RequestHandler.process_logger.add(to_log[0], to_log[1])
44
- end
45
- PhusionPassenger::Rack::RequestHandler.process_logger.info("#{Time.now} (#{Process.pid}) RESPONSE_SENT")
46
- PhusionPassenger::Rack::RequestHandler.process_logger.info("\n")
47
- Thread.current[:passenger_logs] = []
48
- end
49
- end
50
-
51
- alias_method_chain :process_request, :extra_logging
52
- end
53
- end
54
-
55
- end