Module: SmartCacheTenant::Logger

Defined in:
lib/smart_cache_tenant/smart_cache_logger.rb

Constant Summary collapse

RESET =
"\e[0m"
BOLD =
"\e[1m"
CYAN =
"\e[36m"
BLUE =
"\e[34m"
YELLOW =
"\e[33m"

Class Method Summary collapse

Class Method Details

.colorize(text, color_code) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/smart_cache_tenant/smart_cache_logger.rb', line 34

def self.colorize(text, color_code)
  if ActiveSupport::LogSubscriber.colorize_logging
    "#{color_code}#{text}#{RESET}"
  else
    text
  end
end

.format_duration(ms) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/smart_cache_tenant/smart_cache_logger.rb', line 26

def self.format_duration(ms)
  if ms >= 1000
    "#{(ms / 1000.0).round(1)}s"
  else
    "#{ms.round(1)}ms"
  end
end

.log_cache_hit(operation_name, duration_ms, sql) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/smart_cache_tenant/smart_cache_logger.rb', line 11

def self.log_cache_hit(operation_name, duration_ms, sql)
  return unless should_log?

  opt_part = colorize("SMART CACHE #{operation_name} (#{format_duration(duration_ms)})", CYAN + BOLD)
  sql_part = colorize("  #{sql}", BLUE + BOLD)

  Rails.logger.debug("  #{opt_part}#{sql_part}")
end

.should_log?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/smart_cache_tenant/smart_cache_logger.rb', line 22

def self.should_log?
  SmartCacheTenant.config.log_queries && Rails.logger.debug? && !Rails.env.production?
end