class FullRequestLogger::Recorder

Public Class Methods

instance() click to toggle source
# File lib/full_request_logger/recorder.rb, line 7
def self.instance
  @instance ||= new
end

Public Instance Methods

attach_to(logger) click to toggle source

Extends an existing logger instance with a broadcast aspect that'll send a copy of all logging lines to this recorder.

# File lib/full_request_logger/recorder.rb, line 12
def attach_to(logger)
  logger.extend ActiveSupport::Logger.broadcast(ActiveSupport::Logger.new(self))
end
clear() click to toggle source

Clears the current buffer of log messages.

# File lib/full_request_logger/recorder.rb, line 47
def clear
  messages.clear
end
clear_all() click to toggle source

Clear out any messages pending in the buffer as well as all existing stored request logs.

# File lib/full_request_logger/recorder.rb, line 52
def clear_all
  clear
  clear_stored_requests
end
close() click to toggle source

no-op needed for Logger to treat this as a valid log device

# File lib/full_request_logger/recorder.rb, line 58
def close
  redis.disconnect!
end
combined_log() click to toggle source

Return a single string with all the log messages that have been buffered so far.

# File lib/full_request_logger/recorder.rb, line 22
def combined_log
  messages.join.strip
end
retrieve(request_id) click to toggle source

Returns a single string with all the log messages that were captured for the given request_id (or nil if nothing was recorded or it has since expired).

# File lib/full_request_logger/recorder.rb, line 40
def retrieve(request_id)
  if log = redis.get(request_key(request_id))
    uncompress(log).force_encoding("utf-8")
  end
end
store(request_id) click to toggle source

Store all log messages as a single string to the full request logging storage accessible under the request_id.

# File lib/full_request_logger/recorder.rb, line 27
def store(request_id)
  if (log_to_be_stored = combined_log).present?
    redis.setex \
      request_key(request_id),
      FullRequestLogger.ttl,
      compress(log_to_be_stored)
  end
ensure
  clear
end
write(message) click to toggle source

Writes a log message to a buffer that'll be stored when the request is over.

# File lib/full_request_logger/recorder.rb, line 17
def write(message)
  messages << remove_ansi_colors(message)
end

Private Instance Methods

clear_stored_requests() click to toggle source
# File lib/full_request_logger/recorder.rb, line 79
def clear_stored_requests
  if (request_keys = redis.keys(request_key("*"))).any?
    redis.del request_keys
  end
end
compress(text) click to toggle source
# File lib/full_request_logger/recorder.rb, line 85
def compress(text)
  Zlib::Deflate.deflate(text)
end
messages() click to toggle source
# File lib/full_request_logger/recorder.rb, line 67
def messages
  Thread.current[:full_request_logger_messages] ||= []
end
redis() click to toggle source
# File lib/full_request_logger/recorder.rb, line 63
def redis
  @redis ||= Redis.new FullRequestLogger.redis
end
remove_ansi_colors(message) click to toggle source
# File lib/full_request_logger/recorder.rb, line 71
def remove_ansi_colors(message)
  message.remove(/\e\[\d+m/)
end
request_key(id) click to toggle source
# File lib/full_request_logger/recorder.rb, line 75
def request_key(id)
  "full_request_logger/requests/#{id}"
end
uncompress(text) click to toggle source
# File lib/full_request_logger/recorder.rb, line 89
def uncompress(text)
  Zlib::Inflate.inflate(text)
end