Class: PierLogging::RequestLogger
- Inherits:
-
Object
- Object
- PierLogging::RequestLogger
- Defined in:
- lib/pier_logging/request_logger.rb
Constant Summary collapse
- REDACT_REPLACE_KEYS =
[ /passw(or)?d/i, /^pw$/, /^pass$/i, /secret/i, /token/i, /api[-._]?key/i, /session[-._]?id/i, /^connect\.sid$/ ].freeze
- REDACT_REPLACE_BY =
'*'.freeze
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, logger = PierLogging::Logger.new(STDOUT)) ⇒ RequestLogger
constructor
A new instance of RequestLogger.
- #log(args) ⇒ Object
Constructor Details
#initialize(app, logger = PierLogging::Logger.new(STDOUT)) ⇒ RequestLogger
Returns a new instance of RequestLogger.
19 20 21 22 |
# File 'lib/pier_logging/request_logger.rb', line 19 def initialize(app, logger = PierLogging::Logger.new(STDOUT)) @app = app @logger = logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
17 18 19 |
# File 'lib/pier_logging/request_logger.rb', line 17 def logger @logger end |
Instance Method Details
#call(env) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pier_logging/request_logger.rb', line 24 def call(env) starts_at = Time.now.utc begin status, type, body = response = @app.call(env) log([env, status, type, body, starts_at, Time.now.utc, nil]) rescue Exception => exception status = determine_status_code_from_exception(exception) type = determine_type_from_exception(exception) body = determine_body_from_exception(exception) log([env, status, type, body, starts_at, Time.now.utc, exception]) raise exception end response end |
#log(args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pier_logging/request_logger.rb', line 41 def log(args) return unless PierLogging.request_logger_configuration.enabled env, status, type, body, starts_at, ends_at, _ = args request = Rack::Request.new(env) request_headers = get_request_headers_from_env(env) logger.info redact_object({ message: (request), type: 'http', duration: ((ends_at - starts_at)*1000).to_i, context: { user: get_user_info_from_headers(request_headers), request_id: env['action_dispatch.request_id'], correlation_id: get_correlation_id(env, request_headers) }, request: { headers: request_headers, href: request.url, query_string: request.query_string, body: parse_body(request.body) }, response: { status: status, body: response_body(request.path, body), type: type['Content-Type'], } }) rescue StandardError => error # We should never fall in this part as the only errors that could result in this are errors # in our logger (inside this same method) @logger.error(error.) end |