Class: Rack::TrafficLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/traffic_logger.rb,
lib/rack/traffic_logger/echo.rb,
lib/rack/traffic_logger/reader.rb,
lib/rack/traffic_logger/request.rb,
lib/rack/traffic_logger/version.rb,
lib/rack/traffic_logger/formatter.rb,
lib/rack/traffic_logger/header_hash.rb,
lib/rack/traffic_logger/express_setup.rb,
lib/rack/traffic_logger/formatter/json.rb,
lib/rack/traffic_logger/faraday_adapter.rb,
lib/rack/traffic_logger/formatter/stream.rb,
lib/rack/traffic_logger/stream_simulator.rb,
lib/rack/traffic_logger/option_interpreter.rb,
lib/rack/traffic_logger/option_interpreter/shorthand.rb

Direct Known Subclasses

FaradayAdapter

Defined Under Namespace

Classes: Echo, FaradayAdapter, Formatter, HeaderHash, OptionInterpreter, Reader, Request, StreamSimulator

Constant Summary collapse

BASIC_ENV_PROPERTIES =

These environment properties will always be logged as part of request logs

%w[
  REQUEST_METHOD
  HTTPS
  SERVER_NAME
  SERVER_PORT
  PATH_INFO
  QUERY_STRING
  HTTP_VERSION
  REMOTE_HOST
  REMOTE_ADDR
]
VERSION =
'0.3.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, log_path, *options) ⇒ TrafficLogger

Returns a new instance of TrafficLogger.



29
30
31
32
33
34
# File 'lib/rack/traffic_logger.rb', line 29

def initialize(app, log_path, *options)
  @app = app
  @log_path = log_path
  @formatter = options.first.respond_to?(:format) ? options.shift : Formatter::Stream.new
  @options = OptionInterpreter.new(*options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



27
28
29
# File 'lib/rack/traffic_logger.rb', line 27

def options
  @options
end

Class Method Details

.use(on: nil, filter: true, log_path: '/dev/stdout', formatter: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rack/traffic_logger/express_setup.rb', line 4

def self.use(on: nil, filter: true, log_path: '/dev/stdout', formatter: nil)
  filter = (filter || '').to_s.downcase.strip
  unless ['0', 'no', 'false', 'nil', 'none', '', 'off'].include? filter
    args = [self, log_path]
    args << formatter if formatter
    begin
      raise if %w[1 yes true normal basic minimal on].include? filter
      on.use *args, filter
    rescue
      on.use *args
    end
  end
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/traffic_logger.rb', line 36

def call(env)
  request = Request.new(self)
  request.start env
  response = nil
  begin
    response = @app.call(env)
  ensure
    request.finish response
  end
  response
end

#log(hash) ⇒ Object



48
49
50
# File 'lib/rack/traffic_logger.rb', line 48

def log(hash)
  write @formatter.format hash
end

#write(data) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rack/traffic_logger.rb', line 52

def write(data)
  if @log_path.respond_to? :write
    @log_path.write data
  else
    ::File.write @log_path, data, mode: 'a', encoding: data.encoding
  end
end