Class: Rack::FluentdLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/fluentd_logger.rb,
lib/rack/fluentd_logger_version.rb

Constant Summary collapse

VERSION =
'0.1.4'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ FluentdLogger

Returns a new instance of FluentdLogger.



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

def initialize(app)
  @app = app
  @executer = Concurrent::SingleThreadExecutor.new

  self.class.configure if self.class.logger.nil?
end

Class Attribute Details

.json_parserObject (readonly)

Returns the value of attribute json_parser.



11
12
13
# File 'lib/rack/fluentd_logger.rb', line 11

def json_parser
  @json_parser
end

.loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/rack/fluentd_logger.rb', line 11

def logger
  @logger
end

.preprocessorObject (readonly)

Returns the value of attribute preprocessor.



11
12
13
# File 'lib/rack/fluentd_logger.rb', line 11

def preprocessor
  @preprocessor
end

Class Method Details

.configure(name: ENV['FLUENTD_NAME'], host: ENV['FLUENTD_HOST'], port: (ENV['FLUENTD_PORT'] || 24_224).to_i, json_parser: ->(str) { JSON.parse(str) }, preprocessor: ->(d) { d }, max_body_non_json: 256) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/fluentd_logger.rb', line 13

def configure(
  name: ENV['FLUENTD_NAME'],
  host: ENV['FLUENTD_HOST'],
  port: (ENV['FLUENTD_PORT'] || 24_224).to_i,
  json_parser: ->(str) { JSON.parse(str) },
  preprocessor: ->(d) { d },
  max_body_non_json: 256
)
  @logger = Fluent::Logger::FluentLogger.new(name, host: host, port: port)

  @json_parser = json_parser
  @preprocessor = preprocessor
  @max_body_non_json = max_body_non_json
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
# File 'lib/rack/fluentd_logger.rb', line 36

def call(env)
  start = Time.now
  response = @app.call(env)
ensure
  log_request(env, response || $ERROR_INFO, Time.now - start)
end