lib/semlogr/rack/request_logger.rb in semlogr-rack-0.1.0 vs lib/semlogr/rack/request_logger.rb in semlogr-rack-0.1.1
- old
+ new
@@ -1,22 +1,26 @@
module Semlogr
module Rack
class RequestLogger
- def initialize(app, logger = nil)
+ def initialize(app, logger: nil, path_filters: [])
@app = app
@logger = logger
+ @path_filters = path_filters
end
def call(env)
+ path = env['REQUEST_URI']
+ return @app.call(env) if filtered?(path)
+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
status, headers, body = @app.call(env)
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
logger.info(
'HTTP {method} {path} - {status} ({duration}s)',
method: env['REQUEST_METHOD'],
- path: env['REQUEST_URI'],
+ path: path,
status: status,
duration: (finish - start).round(4)
)
[status, headers, body]
@@ -24,9 +28,13 @@
private
def logger
@logger || Semlogr.logger
+ end
+
+ def filtered?(path)
+ @path_filters.any? { |filter| filter =~ path }
end
end
end
end