Class: DebugLogging::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/debug_logging/configuration.rb

Constant Summary collapse

DEFAULT_ELLIPSIS =
' ✂️ …'
CONFIG_ATTRS_DEFAULTS =
{
  enabled: true,
  logger: Logger.new($stdout),
  log_level: :debug,
  multiple_last_hashes: false,
  last_hash_to_s_proc: nil,
  last_hash_max_length: 1_000,
  args_to_s_proc: nil,
  args_max_length: 1_000,
  colorized_chain_for_method: false,
  colorized_chain_for_class: false,
  add_invocation_id: true,
  ellipsis: DEFAULT_ELLIPSIS,
  mark_scope_exit: false,
  add_payload: true, # Can also be a proc returning a string, which will be called when printing the payload
  payload_max_length: 1_000
}.freeze
CONFIG_ATTRS =
CONFIG_ATTRS_DEFAULTS.keys
CONFIG_READERS_DEFAULTS =
{
  instance_benchmarks: false,
  class_benchmarks: false,
  active_support_notifications: false
}.freeze
CONFIG_READERS =
CONFIG_READERS_DEFAULTS.keys
CONFIG_KEYS =
CONFIG_ATTRS + CONFIG_READERS

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Configuration

Returns a new instance of Configuration.



68
69
70
71
72
73
74
75
76
# File 'lib/debug_logging/configuration.rb', line 68

def initialize(**options)
  CONFIG_ATTRS.each do |key|
    send("#{key}=", get_attr_from_options(options, key))
  end
  CONFIG_READERS.each do |key|
    send("#{key}=", get_reader_from_options(options, key))
  end
  @methods_to_log = []
end

Class Method Details

.config_pointer(type, method_to_log) ⇒ Object



61
62
63
64
65
66
# File 'lib/debug_logging/configuration.rb', line 61

def config_pointer(type, method_to_log)
  # Methods names that do not match the following regex can't be part of an ivar name
  #   /[a-zA-Z_][a-zA-Z0-9_]*/
  # Thus we have to use a different form of the method name that is compatible with ivar name conventions
  "@debug_logging_config_#{type}_#{Digest::MD5.hexdigest(method_to_log.to_s)}".to_sym
end

Instance Method Details

#active_support_notifications=(active_support_notifications) ⇒ Object



117
118
119
120
# File 'lib/debug_logging/configuration.rb', line 117

def active_support_notifications=(active_support_notifications)
  require 'debug_logging/active_support_notifications' if active_support_notifications
  @active_support_notifications = active_support_notifications
end

#benchmarkable_for?(benchmarks) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/debug_logging/configuration.rb', line 95

def benchmarkable_for?(benchmarks)
  return @benchmarkable if defined?(@benchmarkable)

  @benchmarkable = loggable? && send(benchmarks)
end

#class_benchmarks=(class_benchmarks) ⇒ Object



112
113
114
115
# File 'lib/debug_logging/configuration.rb', line 112

def class_benchmarks=(class_benchmarks)
  require 'benchmark' if class_benchmarks
  @class_benchmarks = class_benchmarks
end

#exit_scope_markable?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/debug_logging/configuration.rb', line 101

def exit_scope_markable?
  return @exit_scope_markable if defined?(@exit_scope_markable)

  @exit_scope_markable = loggable? && mark_scope_exit
end

#instance_benchmarks=(instance_benchmarks) ⇒ Object



107
108
109
110
# File 'lib/debug_logging/configuration.rb', line 107

def instance_benchmarks=(instance_benchmarks)
  require 'benchmark' if instance_benchmarks
  @instance_benchmarks = instance_benchmarks
end

#log(message = nil, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/debug_logging/configuration.rb', line 78

def log(message = nil, &block)
  return unless enabled
  return unless logger

  if block
    logger.send(log_level, &block)
  else
    logger.send(log_level, message)
  end
end

#loggable?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/debug_logging/configuration.rb', line 89

def loggable?
  return @loggable if defined?(@loggable)

  @loggable = logger.send("#{log_level}?")
end

#register(method_lo_log) ⇒ Object



128
129
130
# File 'lib/debug_logging/configuration.rb', line 128

def register(method_lo_log)
  @methods_to_log << method_lo_log
end

#to_hashObject



122
123
124
125
126
# File 'lib/debug_logging/configuration.rb', line 122

def to_hash
  CONFIG_KEYS.each_with_object({}) do |key, hash|
    hash[key] = instance_variable_get("@#{key}")
  end
end