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_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
}.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.



65
66
67
68
69
70
71
72
73
# File 'lib/debug_logging/configuration.rb', line 65

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



58
59
60
61
62
63
# File 'lib/debug_logging/configuration.rb', line 58

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



114
115
116
117
# File 'lib/debug_logging/configuration.rb', line 114

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)


92
93
94
95
96
# File 'lib/debug_logging/configuration.rb', line 92

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

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

#class_benchmarks=(class_benchmarks) ⇒ Object



109
110
111
112
# File 'lib/debug_logging/configuration.rb', line 109

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

#exit_scope_markable?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/debug_logging/configuration.rb', line 98

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



104
105
106
107
# File 'lib/debug_logging/configuration.rb', line 104

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

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



75
76
77
78
79
80
81
82
83
84
# File 'lib/debug_logging/configuration.rb', line 75

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)


86
87
88
89
90
# File 'lib/debug_logging/configuration.rb', line 86

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

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

#register(method_lo_log) ⇒ Object



125
126
127
# File 'lib/debug_logging/configuration.rb', line 125

def register(method_lo_log)
  @methods_to_log << method_lo_log
end

#to_hashObject



119
120
121
122
123
# File 'lib/debug_logging/configuration.rb', line 119

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