Class: Grape::Middleware::Logger
- Inherits:
-
Globals
- Object
- Globals
- Grape::Middleware::Logger
- Defined in:
- lib/grape/middleware/logger.rb
Constant Summary collapse
- BACKSLASH =
'/'.freeze
Class Attribute Summary collapse
-
.condensed ⇒ Object
Returns the value of attribute condensed.
-
.filter ⇒ Object
Returns the value of attribute filter.
-
.headers ⇒ Object
Returns the value of attribute headers.
-
.logger ⇒ Object
Returns the value of attribute logger.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
- #after(status) ⇒ Object
-
#after_exception(e) ⇒ Object
Helpers.
- #after_failure(error) ⇒ Object
- #before ⇒ Object
- #call!(env) ⇒ Object
- #headers ⇒ Object
-
#initialize(_, options = {}) ⇒ Logger
constructor
A new instance of Logger.
- #log_info(log_statements = []) ⇒ Object
- #parameters ⇒ Object
- #processed_by ⇒ Object
- #start_time ⇒ Object
Constructor Details
#initialize(_, options = {}) ⇒ Logger
Returns a new instance of Logger.
19 20 21 22 23 24 25 |
# File 'lib/grape/middleware/logger.rb', line 19 def initialize(_, = {}) super @options[:filter] ||= self.class.filter @options[:headers] ||= self.class.headers @options[:condensed] ||= false @logger = [:logger] || self.class.logger || self.class.default_logger end |
Class Attribute Details
.condensed ⇒ Object
Returns the value of attribute condensed.
10 11 12 |
# File 'lib/grape/middleware/logger.rb', line 10 def condensed @condensed end |
.filter ⇒ Object
Returns the value of attribute filter.
10 11 12 |
# File 'lib/grape/middleware/logger.rb', line 10 def filter @filter end |
.headers ⇒ Object
Returns the value of attribute headers.
10 11 12 |
# File 'lib/grape/middleware/logger.rb', line 10 def headers @headers end |
.logger ⇒ Object
Returns the value of attribute logger.
10 11 12 |
# File 'lib/grape/middleware/logger.rb', line 10 def logger @logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/grape/middleware/logger.rb', line 7 def logger @logger end |
Class Method Details
Instance Method Details
#after(status) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/grape/middleware/logger.rb', line 71 def after(status) log_info( [ "Completed #{status} in #{((Time.now - start_time) * 1000).round(2)}ms", '' ] ) end |
#after_exception(e) ⇒ Object
Helpers
84 85 86 87 |
# File 'lib/grape/middleware/logger.rb', line 84 def after_exception(e) logger.info %Q( #{e.class.name}: #{e.}) after(500) end |
#after_failure(error) ⇒ Object
89 90 91 92 |
# File 'lib/grape/middleware/logger.rb', line 89 def after_failure(error) logger.info %Q( Error: #{error[:message]}) if error[:message] after(error[:status]) end |
#before ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/grape/middleware/logger.rb', line 27 def before start_time # sets env['grape.*'] super log_statements = [ '', %Q(Started %s "%s" at %s) % [ env[Grape::Env::GRAPE_REQUEST].request_method, env[Grape::Env::GRAPE_REQUEST].path, start_time.to_s ], %Q(Processing by #{processed_by}), %Q( Parameters: #{parameters})] log_statements.append(%Q( Headers: #{headers})) if @options[:headers] log_info(log_statements) end |
#call!(env) ⇒ Object
Note:
Error and exception handling are required for the after
hooks Exceptions are logged as a 500 status and re-raised Other “errors” are caught, logged and re-thrown
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/grape/middleware/logger.rb', line 49 def call!(env) @env = env before error = catch(:error) do begin @app_response = @app.call(@env) rescue => e after_exception(e) raise e end nil end if error after_failure(error) throw(:error, error) else status, _, _ = *@app_response after(status) end @app_response end |
#headers ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/grape/middleware/logger.rb', line 105 def headers request_headers = env[Grape::Env::GRAPE_REQUEST_HEADERS].to_hash return Hash[request_headers.sort] if @options[:headers] == :all headers_needed = Array(@options[:headers]) result = {} headers_needed.each do |need| result.merge!(request_headers.select { |key, value| need.to_s.casecmp(key).zero? }) end Hash[result.sort] end |
#log_info(log_statements = []) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/grape/middleware/logger.rb', line 133 def log_info(log_statements=[]) if @options[:condensed] logger.info log_statements.compact.delete_if(&:empty?).each(&:strip!).join(" - ") else log_statements.each { |log_statement| logger.info log_statement } end end |
#parameters ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/grape/middleware/logger.rb', line 94 def parameters request_params = env[Grape::Env::GRAPE_REQUEST_PARAMS].to_hash request_params.merge! env[Grape::Env::RACK_REQUEST_FORM_HASH] if env[Grape::Env::RACK_REQUEST_FORM_HASH] request_params.merge! env['action_dispatch.request.request_parameters'] if env['action_dispatch.request.request_parameters'] if @options[:filter] @options[:filter].filter(request_params) else request_params end end |
#processed_by ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/grape/middleware/logger.rb', line 121 def processed_by endpoint = env[Grape::Env::API_ENDPOINT] result = [] if endpoint.namespace == BACKSLASH result << '' else result << endpoint.namespace end result.concat endpoint.[:path].map { |path| path.to_s.sub(BACKSLASH, '') } endpoint.[:for].to_s << result.join(BACKSLASH) end |
#start_time ⇒ Object
117 118 119 |
# File 'lib/grape/middleware/logger.rb', line 117 def start_time @start_time ||= Time.now end |