Class: Grape::Middleware::Logger

Inherits:
Globals
  • Object
show all
Defined in:
lib/grape/middleware/logger.rb

Constant Summary collapse

BACKSLASH =
'/'.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(_, options = {})
  super
  @options[:filter] ||= self.class.filter
  @options[:headers] ||= self.class.headers
  @options[:condensed] ||= false
  @logger = options[:logger] || self.class.logger || self.class.default_logger
end

Class Attribute Details

.condensedObject

Returns the value of attribute condensed.



10
11
12
# File 'lib/grape/middleware/logger.rb', line 10

def condensed
  @condensed
end

.filterObject

Returns the value of attribute filter.



10
11
12
# File 'lib/grape/middleware/logger.rb', line 10

def filter
  @filter
end

.headersObject

Returns the value of attribute headers.



10
11
12
# File 'lib/grape/middleware/logger.rb', line 10

def headers
  @headers
end

.loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/grape/middleware/logger.rb', line 10

def logger
  @logger
end

Instance Attribute Details

#loggerObject (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

.default_loggerObject



12
13
14
15
16
# File 'lib/grape/middleware/logger.rb', line 12

def default_logger
  default = Logger.new(STDOUT)
  default.formatter = ->(*args) { args.last.to_s << "\n".freeze }
  default
end

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.message})
  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

#beforeObject



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

#headersObject



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

#parametersObject



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_byObject



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.options[:path].map { |path| path.to_s.sub(BACKSLASH, '') }
  endpoint.options[:for].to_s << result.join(BACKSLASH)
end

#start_timeObject



117
118
119
# File 'lib/grape/middleware/logger.rb', line 117

def start_time
  @start_time ||= Time.now
end