class Pakyow::Logger

Logs messages throughout the lifetime of an environment, connection, etc.

In addition to logging standard messages, this class provides a way to log a `prologue` and `epilogue` for a connection, as well as a `houston` method for logging errors.

Attributes

id[R]

@!attribute [r] id

@return [String] the unique id of the logger instance
started_at[R]

@!attribute [r] started_at

@return [Time] the time when logging started
type[R]

@!attribute [r] type

@return [Symbol] the type of logger

Public Class Methods

new(type, started_at: Time.now, id: SecureRandom.hex(4), output:, level:) click to toggle source

@param type [Symbol] the type of logging being done (e.g. :http, :sock) @param started_at [Time] when the logging began @param output [Object] the object that will perform the logging @param id [String] a unique id used to identify the request

Calls superclass method
# File lib/pakyow/logger.rb, line 33
def initialize(type, started_at: Time.now, id: SecureRandom.hex(4), output:, level:)
  @type, @started_at, @id = type, started_at, id

  level = case level
  when :all
    0
  when :off
    7
  when Symbol
    self.class.const_get(:LEVELS)[level]
  else
    level
  end

  super(output, level: level)
end

Public Instance Methods

<<(message) click to toggle source
# File lib/pakyow/logger.rb, line 68
def <<(message)
  add(:unknown, message)
end
add(level, message = nil, &block) click to toggle source
# File lib/pakyow/logger.rb, line 72
def add(level, message = nil, &block)
  public_send(level, message, &block)
end
Also aliased as: log
elapsed() click to toggle source
# File lib/pakyow/logger.rb, line 102
def elapsed
  (Time.now - @started_at)
end
epilogue(connection) click to toggle source

Logs the conclusion of a request, including the response status.

@param res [Array] the rack response array

# File lib/pakyow/logger.rb, line 90
def epilogue(connection)
  info { formatted_epilogue(connection) }
end
houston(error) click to toggle source

Logs an error raised when processing the request.

@param error [Object] the error object

# File lib/pakyow/logger.rb, line 98
def houston(error)
  error { formatted_error(error) }
end
log(level, message = nil, &block)
Alias for: add
prologue(connection) click to toggle source

Logs the beginning of a request, including the time, request method, request uri, and originating ip address.

@param env [Hash] the rack env for the request

# File lib/pakyow/logger.rb, line 82
def prologue(connection)
  info { formatted_prologue(connection) }
end
silence(temporary_level = :error) { || ... } click to toggle source

Temporarily silences logs, up to temporary_level.

# File lib/pakyow/logger.rb, line 52
def silence(temporary_level = :error)
  original_level = @level
  self.level = self.class.const_get(:LEVELS)[temporary_level]
  yield
ensure
  self.level = original_level
end

Private Instance Methods

decorate(message = nil) { || ... } click to toggle source
# File lib/pakyow/logger.rb, line 108
def decorate(message = nil)
  message = yield if block_given?
  { "logger" => self, "message" => message }
end
formatted_epilogue(connection) click to toggle source
# File lib/pakyow/logger.rb, line 117
def formatted_epilogue(connection)
  { "epilogue" => connection }
end
formatted_error(error) click to toggle source
# File lib/pakyow/logger.rb, line 121
def formatted_error(error)
  { "error" => error }
end
formatted_prologue(connection) click to toggle source
# File lib/pakyow/logger.rb, line 113
def formatted_prologue(connection)
  { "prologue" => connection }
end