Class: Rack::Timeout::StageChangeLoggingObserver

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/timeout/logger.rb

Constant Summary collapse

STATE_LOG_LEVEL =
{ :expired   => :error,
  :ready     => :info,
  :active    => :debug,
  :timed_out => :error,
  :completed => :info,
}
SIMPLE_FORMATTER =
->(severity, timestamp, progname, msg) { "#{msg} at=#{severity.downcase}\n" }

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger


32
33
34
# File 'lib/rack/timeout/logger.rb', line 32

def logger
  @logger
end

Class Method Details

.mk_logger(device, level = ::Logger::INFO) ⇒ Object


24
25
26
27
28
29
# File 'lib/rack/timeout/logger.rb', line 24

def self.mk_logger(device, level = ::Logger::INFO)
  ::Logger.new(device).tap do |logger|
    logger.level     = level
    logger.formatter = SIMPLE_FORMATTER
  end
end

.register!(logger = nil) ⇒ Object

creates a logger and registers for state change notifications in Rack::Timeout


13
14
15
# File 'lib/rack/timeout/logger.rb', line 13

def self.register!(logger = nil)
  new.register!(logger)
end

Instance Method Details

#log_state_change(env) ⇒ Object

generates the actual log string


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rack/timeout/logger.rb', line 43

def log_state_change(env)
  info = env[ENV_INFO_KEY]
  level = STATE_LOG_LEVEL[info.state]
  logger(env).send(level) do
    s  = 'source=rack-timeout'
    s << ' id='      << info.id           if info.id
    s << ' wait='    << info.ms(:wait)    if info.wait
    s << ' timeout=' << info.ms(:timeout) if info.timeout
    s << ' service=' << info.ms(:service) if info.service
    s << ' state='   << info.state.to_s   if info.state
    s
  end
end

#logger(env = nil) ⇒ Object


34
35
36
37
38
39
40
# File 'lib/rack/timeout/logger.rb', line 34

def logger(env = nil)
  self.class.logger ||
    (defined?(::Rails) && Rails.logger) ||
    (env && !env['rack.logger'].is_a?(::Rack::NullLogger) && env['rack.logger']) ||
    (env && env['rack.errors'] && self.class.mk_logger(env['rack.errors']))      ||
    (@fallback_logger ||= self.class.mk_logger($stderr))
end

#register!(logger = nil, target = ::Rack::Timeout) ⇒ Object

registers for state change notifications in Rack::Timeout (or other explicit target (potentially useful for testing))


18
19
20
21
# File 'lib/rack/timeout/logger.rb', line 18

def register!(logger = nil, target = ::Rack::Timeout)
  @logger = logger
  target.register_state_change_observer(:logger, &method(:log_state_change))
end