mongo_request_logger 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 14cc51ac49b41aff00fa2706ef33b7d6746dc5c4
4
- data.tar.gz: c31e051b7f43597f14a6801b50bdfac0d588ad4a
3
+ metadata.gz: 686d9920c99400beb652b916232a1c96a993bd96
4
+ data.tar.gz: 9f60ec4554d2f7d30c21cd1d1da25cc8d6047727
5
5
  SHA512:
6
- metadata.gz: 03fc69114b36bd8655151147e521b7669648ea4ad495398bb97dff12d0d3d1e119fd254775f559745d220c59f489c6e09c1ce354729a46105911fb038bdddd1b
7
- data.tar.gz: 6f595c406735e69032845e2544eb0bfd0019cd5da6509bec8c20fcaa2553e72bfb66ab354229cd52e5d258bc922a2830ba4c366f89003b032f92aad4da227a52
6
+ metadata.gz: 04ca6c4bfc3152ac76195cb468ecac824518c456fb6ea4ccead67910acea8a017718e7e3258392a04b1826d9a9d25eeba209c1061cb433d0cdbbf12e50aead4c
7
+ data.tar.gz: 4abb554be869b9ba08977b0df18050d0ba6ef12c75278bf62688d3e4f9c8ccc524aafc9925d6699f4cb82e22ba40757b62048baea3adc3b56071d7cefc3c3e2c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.4
2
+
3
+ * Improved hanlding of logger when not configured for an environment.
4
+
1
5
  ## 0.2.3
2
6
 
3
7
  * Simpler configuration for Sinatra apps.
data/Gemfile CHANGED
@@ -4,7 +4,7 @@ gem 'mongo'
4
4
  gem 'bson_ext'
5
5
  gem 'moped'
6
6
  gem 'rspec'
7
- gem 'rails', '~> 3.2.10'
7
+ gem 'sinatra', '~> 1.4.0'
8
8
 
9
9
  # Specify your gem's dependencies in mongo_request_logger.gemspec
10
10
  gemspec
data/README.md CHANGED
@@ -50,7 +50,7 @@ In config.ru:
50
50
 
51
51
  In a Sinatra app:
52
52
 
53
- # Disable the default logger
53
+ # This prevents Sinatra from using the default logger, so it uses our logger instead.
54
54
  set :logging, nil
55
55
 
56
56
  get '/' do
@@ -31,33 +31,36 @@ module MongoRequestLogger
31
31
  global_config = MongoRequestLogger::Config.new
32
32
  global_config.load_file(config_path)
33
33
  log_config = global_config.namespaced(environment)
34
+ fallback_log = options[:fallback_log] || log_config['fallback_log'] || "log/#{environment}.log"
35
+
36
+
37
+ if log_config && log_config['database']
38
+ if defined? Moped
39
+ adapter = MongoRequestLogger::Adapters::Moped.new(log_config)
40
+ else
41
+ adapter = MongoRequestLogger::Adapters::Mongo.new(log_config)
42
+ end
34
43
 
35
- return false unless log_config && log_config['database']
44
+ MongoRequestLogger::Viewer.adapter = adapter
45
+
46
+ if defined?(PhusionPassenger)
47
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
48
+ if forked
49
+ adapter.reconnect
50
+ end
51
+ end
52
+ end
36
53
 
37
- if defined? Moped
38
- adapter = MongoRequestLogger::Adapters::Moped.new(log_config)
54
+ logger = MongoRequestLogger::Logger.new adapter, fallback_log
39
55
  else
40
- adapter = MongoRequestLogger::Adapters::Mongo.new(log_config)
56
+ logger = ::Logger.new(fallback_log)
41
57
  end
42
58
 
43
- fallback_log = options[:fallback_log] || log_config['fallback_log'] || "log/#{environment}.log"
44
-
45
- logger = MongoRequestLogger::Logger.new adapter, fallback_log
46
59
 
47
60
  MongoRequestLogger::Rack.logger = logger
48
61
  MongoRequestLogger::Rack.ignore_prefixes << '/assets'
49
62
  MongoRequestLogger::Rack.ignore_prefixes << '/favicon.ico'
50
63
 
51
- MongoRequestLogger::Viewer.adapter = adapter
52
-
53
- if defined?(PhusionPassenger)
54
- PhusionPassenger.on_event(:starting_worker_process) do |forked|
55
- if forked
56
- adapter.reconnect
57
- end
58
- end
59
- end
60
-
61
64
  logger
62
65
 
63
66
  end
@@ -25,42 +25,51 @@ module MongoRequestLogger
25
25
  end
26
26
 
27
27
  def call(env)
28
- env['rack.logger'] = logger
29
- env['rack.errors'] = logger
28
+ if logger
29
+ env['rack.logger'] = logger
30
+ env['rack.errors'] = logger
31
+ end
32
+
33
+ if logger && logger.is_a?(MongoRequestLogger::Logger)
30
34
 
31
- #HACK: Don't log the log viewer itself
32
- self.class.ignore_prefixes.each do |prefix|
33
- if env["PATH_INFO"].to_s.start_with? prefix
34
- return @app.call(env)
35
+ #HACK: Don't log the log viewer itself
36
+ self.class.ignore_prefixes.each do |prefix|
37
+ if env["PATH_INFO"].to_s.start_with? prefix
38
+ return @app.call(env)
39
+ end
35
40
  end
36
- end
37
- # TODO: filter parameters
38
- # KLUDGE: this will cause parameter parsing to happen twice: once here, once later on in Rails.
39
- # Or maybe it's automatically cached in env?
40
- logger.log_request do
41
- begin
42
- request = ::Rack::Request.new(env)
43
- options = {
44
- path: request.path_info,
45
- host: request.host_with_port,
46
- user_agent: request.user_agent,
47
- ip: request.ip,
48
- referer: request.referrer,
49
- query_string: request.query_string,
50
- request_method: request.request_method,
51
- content_type: request.content_type,
52
- params: request.params
53
- }
54
- logger.add_metadata options
55
- logger.add_metadata_set :tags, "rack"
56
- status, headers, body = @app.call(env)
57
- logger.add_metadata response: status.to_i
58
- [status, headers, body]
59
- rescue Exception => e
60
- logger.add_metadata response: 500
61
- raise
41
+ # TODO: filter parameters
42
+ # KLUDGE: this will cause parameter parsing to happen twice: once here, once later on in Rails.
43
+ # Or maybe it's automatically cached in env?
44
+ logger.log_request do
45
+ begin
46
+ request = ::Rack::Request.new(env)
47
+ options = {
48
+ path: request.path_info,
49
+ host: request.host_with_port,
50
+ user_agent: request.user_agent,
51
+ ip: request.ip,
52
+ referer: request.referrer,
53
+ query_string: request.query_string,
54
+ request_method: request.request_method,
55
+ content_type: request.content_type,
56
+ params: request.params
57
+ }
58
+ logger.add_metadata options
59
+ logger.add_metadata_set :tags, "rack"
60
+ status, headers, body = @app.call(env)
61
+ logger.add_metadata response: status.to_i
62
+ [status, headers, body]
63
+ rescue Exception => e
64
+ logger.add_metadata response: 500
65
+ raise
66
+ end
62
67
  end
68
+ else
69
+ # No logger configured - just pass the request straight through
70
+ @app.call(env)
63
71
  end
72
+
64
73
  end
65
74
  end
66
75
  end
@@ -1,3 +1,3 @@
1
1
  module MongoRequestLogger
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
@@ -58,7 +58,9 @@ module MongoRequestLogger
58
58
  #end
59
59
 
60
60
  data = []
61
- if errors.empty?
61
+ if adapter.nil?
62
+ errors << "Log database is not configured"
63
+ elsif errors.empty?
62
64
  begin
63
65
  cursor = adapter.query(query, :limit => SERVER_LOGS_PER_PAGE)
64
66
  data = cursor.to_a
data/spec/testapp/app.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  require 'sinatra/base'
2
2
 
3
3
  class SinatraApp < Sinatra::Base
4
- # This prevents Sinatra from using the default logger
4
+ # This prevents Sinatra from using the default logger.
5
5
  set :logging, nil
6
6
 
7
7
  get '/' do
8
8
  logger.info 'Logging from Sinatra!'
9
9
 
10
- 'Log test'
10
+ "Log test on Sinatra #{Sinatra::VERSION}"
11
11
  end
12
12
  end
@@ -0,0 +1 @@
1
+ *.log
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_request_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Embark Mobile
@@ -399,6 +399,7 @@ files:
399
399
  - spec/testapp/app.rb
400
400
  - spec/testapp/config.ru
401
401
  - spec/testapp/config/logger.yml
402
+ - spec/testapp/log/.gitignore
402
403
  - spec/testapp/log/.gitkeep
403
404
  - views/index.erb
404
405
  homepage: ''
@@ -434,4 +435,5 @@ test_files:
434
435
  - spec/testapp/app.rb
435
436
  - spec/testapp/config.ru
436
437
  - spec/testapp/config/logger.yml
438
+ - spec/testapp/log/.gitignore
437
439
  - spec/testapp/log/.gitkeep