class Cequel::Metal::RequestLogger

The Logger class encapsulates logging functionality for {Keyspace}.

@api private

Attributes

logger[RW]

@return [::Logger] An instance of Logger that responds to methods for

standard severity levels
slowlog_threshold[RW]

@return [Integer] Only log queries that take longer than threshold ms

Public Class Methods

new() click to toggle source
# File lib/cequel/metal/request_logger.rb, line 16
def initialize
  self.slowlog_threshold = 2000
end

Public Instance Methods

log(label, statement) { || ... } click to toggle source

Log a CQL statement

@param label [String] a logical label for this statement @param statement [String,Statement,Batch] the CQL statement to log @return [void]

# File lib/cequel/metal/request_logger.rb, line 27
def log(label, statement)
  return yield if logger.nil?

  response = nil
  begin
    time = Benchmark.ms { response = yield }
    generate_message = lambda do
      format_for_log(label, "#{time.round.to_i}ms", statement)
    end

    if time >= slowlog_threshold
      logger.warn(&generate_message)
    else
      logger.debug(&generate_message)
    end
  rescue Exception => e
    logger.error { format_for_log(label, 'ERROR', statement) }
    raise
  end
  response
end

Private Instance Methods

format_for_log(label, timing, statement) click to toggle source
# File lib/cequel/metal/request_logger.rb, line 51
def format_for_log(label, timing, statement)
  cql_for_log =
    case statement
    when String
      statement
    when Statement
      sanitize(statement.cql, limit_value_length(statement.bind_vars))
    when Cassandra::Statements::Batch
      batch_stmt = "BEGIN #{'UNLOGGED ' if statement.type == :unlogged}BATCH"
      statement.statements.each { |s| batch_stmt << "\n" << sanitize(s.cql, limit_value_length(s.params)) }
      batch_stmt << "END BATCH"
    end

  format('%s (%s) %s', label, timing, cql_for_log)
end
limit_length(str) click to toggle source
# File lib/cequel/metal/request_logger.rb, line 71
def limit_length(str)
  return str if str.length < 100

  str[0..25] + "..." + str[-25..-1]
end
limit_value_length(bind_vars) click to toggle source
# File lib/cequel/metal/request_logger.rb, line 67
def limit_value_length(bind_vars)
  bind_vars.map { |it| String === it ? limit_length(it) : it }
end