Class: ErpIntegration::Middleware::Logger
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- ErpIntegration::Middleware::Logger
- Defined in:
- lib/erp_integration/middleware/logger.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
Notice that logging headers will expose sensitive information like api-key.
{ headers: false, bodies: true, errors: true }.freeze
Instance Method Summary collapse
- #api_key_tag(api_key_fragment) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, logger = nil, options = {}) {|@formatter| ... } ⇒ Logger
constructor
A new instance of Logger.
- #on_complete(env) ⇒ Object
- #on_error(exc) ⇒ Object
-
#sanitize_api_key(api_key) ⇒ String
The last 4 characters of the API key.
Constructor Details
#initialize(app, logger = nil, options = {}) {|@formatter| ... } ⇒ Logger
Returns a new instance of Logger.
12 13 14 15 16 17 18 19 |
# File 'lib/erp_integration/middleware/logger.rb', line 12 def initialize(app, logger = nil, = {}) super(app) @logger = logger formatter_klass = .delete(:formatter) || Faraday::Logging::Formatter @formatter = formatter_klass.new(logger: logger, options: DEFAULT_OPTIONS) yield @formatter if block_given? end |
Instance Method Details
#api_key_tag(api_key_fragment) ⇒ Object
46 47 48 |
# File 'lib/erp_integration/middleware/logger.rb', line 46 def api_key_tag(api_key_fragment) "API key *#{api_key_fragment}" end |
#call(env) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/erp_integration/middleware/logger.rb', line 21 def call(env) api_key_fragment = sanitize_api_key(env.request_headers['X-API-KEY']) || 'none' request_uuid = SecureRandom.uuid @logger.(api_key_tag(api_key_fragment), request_uuid) do @formatter.request(env) @app.call(env).on_complete { |response| on_complete(response) } end end |
#on_complete(env) ⇒ Object
32 33 34 |
# File 'lib/erp_integration/middleware/logger.rb', line 32 def on_complete(env) @formatter.response(env) end |
#on_error(exc) ⇒ Object
36 37 38 |
# File 'lib/erp_integration/middleware/logger.rb', line 36 def on_error(exc) @formatter.exception(exc) if @formatter.respond_to?(:exception) end |
#sanitize_api_key(api_key) ⇒ String
Returns The last 4 characters of the API key.
42 43 44 |
# File 'lib/erp_integration/middleware/logger.rb', line 42 def sanitize_api_key(api_key) api_key[-4..-1] if api_key end |