Class: FullRequestLogger::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/full_request_logger/recorder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



7
8
9
# File 'lib/full_request_logger/recorder.rb', line 7

def self.instance
  @instance ||= new
end

Instance Method Details

#attach_to(logger) ⇒ Object

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



12
13
14
# File 'lib/full_request_logger/recorder.rb', line 12

def attach_to(logger)
  logger.extend ActiveSupport::Logger.broadcast(ActiveSupport::Logger.new(self))
end

#clearObject

Clears the current buffer of log messages.



47
48
49
# File 'lib/full_request_logger/recorder.rb', line 47

def clear
  messages.clear
end

#clear_allObject

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



52
53
54
55
# File 'lib/full_request_logger/recorder.rb', line 52

def clear_all
  clear
  clear_stored_requests
end

#closeObject

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



58
59
60
# File 'lib/full_request_logger/recorder.rb', line 58

def close
  redis.disconnect!
end

#combined_logObject

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



22
23
24
# File 'lib/full_request_logger/recorder.rb', line 22

def combined_log
  messages.join.strip
end

#retrieve(request_id) ⇒ Object

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).



40
41
42
43
44
# 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) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object

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



17
18
19
# File 'lib/full_request_logger/recorder.rb', line 17

def write(message)
  messages << remove_ansi_colors(message)
end