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
@!attribute [r] id
@return [String] the unique id of the logger instance
@!attribute [r] started_at
@return [Time] the time when logging started
@!attribute [r] type
@return [Symbol] the type of logger
Public Class Methods
@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
# 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
# File lib/pakyow/logger.rb, line 68 def <<(message) add(:unknown, message) end
# File lib/pakyow/logger.rb, line 72 def add(level, message = nil, &block) public_send(level, message, &block) end
# File lib/pakyow/logger.rb, line 102 def elapsed (Time.now - @started_at) end
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
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
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
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
# File lib/pakyow/logger.rb, line 108 def decorate(message = nil) message = yield if block_given? { "logger" => self, "message" => message } end
# File lib/pakyow/logger.rb, line 117 def formatted_epilogue(connection) { "epilogue" => connection } end
# File lib/pakyow/logger.rb, line 121 def formatted_error(error) { "error" => error } end
# File lib/pakyow/logger.rb, line 113 def formatted_prologue(connection) { "prologue" => connection } end