class Roda::EnhancedLogger::Instance

Logger instance for this application

Attributes

filter[R]

Callable object to filter log entries

log_entries[R]

Log entries generated during request

logger[R]

Logger instance

matches[R]

Route matches during request

root[R]

Application root

timer[R]

Public Class Methods

new(logger, env, instance_id, root, filter) click to toggle source
# File lib/roda/enhanced_logger/instance.rb, line 29
def initialize(logger, env, instance_id, root, filter)
  @logger = logger
  @root = root
  @log_entries = []
  @matches = []
  @timer = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @filter = filter || proc { false }
  if env["enhanced_logger_id"].nil?
    @primary = true
    env["enhanced_logger_id"] = instance_id
  else
    @primary = false
  end
end

Public Instance Methods

add(status, request, trace = false) click to toggle source

Add log entry for request @param status [Integer]

status code for the response

@param request [Roda::RodaRequest]

request object

@param trace [Boolean]

tracing was enabled
# File lib/roda/enhanced_logger/instance.rb, line 56
def add(status, request, trace = false)
  if (last_matched_caller = matches.last)
    handler = format("%s:%d",
      Pathname(last_matched_caller.path).relative_path_from(root),
      last_matched_caller.lineno)
  end

  meth =
    case status
    when 400..499
      :warn
    when 500..599
      :error
    else
      :info
    end

  data = {
    duration: (Process.clock_gettime(Process::CLOCK_MONOTONIC) - timer).round(4),
    status: status,
    verb: request.request_method,
    path: request.path,
    remaining_path: request.remaining_path,
    handler: handler,
    params: request.params
  }

  if (db = Roda::EnhancedLogger::Current.accrued_database_time)
    data[:db] = db.round(6)
  end

  if (query_count = Roda::EnhancedLogger::Current.database_query_count)
    data[:db_queries] = query_count
  end

  if trace
    matches.each do |match|
      add_log_entry([meth, format("  %s (%s:%s)",
        File.readlines(match.path)[match.lineno - 1].strip.sub(" do", ""),
        Pathname(match.path).relative_path_from(root),
        match.lineno)])
    end
  end

  return if filter.call(request.path)

  add_log_entry([meth, "#{request.request_method} #{request.path}", data])
end
add_match(caller) click to toggle source

Add a matched route handler

# File lib/roda/enhanced_logger/instance.rb, line 45
def add_match(caller)
  @matches << caller
end
drain() click to toggle source

Drain the log entry queue, writing each to the logger at their respective level @return [Boolean]

# File lib/roda/enhanced_logger/instance.rb, line 113
def drain
  return unless primary?

  log_entries.each do |args|
    logger.public_send(*args)
  end

  true
end
primary?() click to toggle source

This instance is the primary logger @return [Boolean]

# File lib/roda/enhanced_logger/instance.rb, line 107
def primary?
  @primary
end
reset() click to toggle source

Reset the counters for this thread @return [Boolean]

# File lib/roda/enhanced_logger/instance.rb, line 125
def reset
  Roda::EnhancedLogger::Current.reset
end

Private Instance Methods

add_log_entry(record) click to toggle source
# File lib/roda/enhanced_logger/instance.rb, line 131
def add_log_entry(record)
  @log_entries << record
end