Class: DebugLogging::Configuration

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

Constant Summary collapse

DEFAULT_ELLIPSIS =
" ✂️ …".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Configuration

Returns a new instance of Configuration.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/debug_logging/configuration.rb', line 62

def initialize(**options)
  @enabled = options.key?(:enabled) ? options[:enabled] : true
  @logger = options.key?(:logger) ? options[:logger] : Logger.new(STDOUT)
  @log_level = options.key?(:log_level) ? options[:log_level] : :debug
  @multiple_last_hashes = options.key?(:multiple_last_hashes) ? options[:multiple_last_hashes] : false
  @last_hash_to_s_proc = options.key?(:last_hash_to_s_proc) ? options[:last_hash_to_s_proc] : nil
  @last_hash_max_length = options.key?(:last_hash_max_length) ? options[:last_hash_max_length] : 1_000
  @args_max_length = options.key?(:args_max_length) ? options[:args_max_length] : 1_000
  @instance_benchmarks = options.key?(:instance_benchmarks) ? options[:instance_benchmarks] : false
  @class_benchmarks = options.key?(:class_benchmarks) ? options[:class_benchmarks] : false
  @colorized_chain_for_method = options.key?(:colorized_chain_for_method) ? options[:colorized_chain_for_method] : false
  @colorized_chain_for_class = options.key?(:colorized_chain_for_class) ? options[:colorized_chain_for_class] : false
  @add_invocation_id = options.key?(:add_invocation_id) ? options[:add_invocation_id] : true
  @ellipsis = options.key?(:ellipsis) ? options[:ellipsis] : DEFAULT_ELLIPSIS
  @mark_scope_exit = options.key?(:mark_scope_exit) ? options[:mark_scope_exit] : false
  @methods_to_log = []
end

Instance Attribute Details

#add_invocation_idObject Also known as: debug_add_invocation_id

Returns the value of attribute add_invocation_id.



17
18
19
# File 'lib/debug_logging/configuration.rb', line 17

def add_invocation_id
  @add_invocation_id
end

#args_max_lengthObject Also known as: debug_args_max_length

Returns the value of attribute args_max_length.



12
13
14
# File 'lib/debug_logging/configuration.rb', line 12

def args_max_length
  @args_max_length
end

#class_benchmarksObject Also known as: debug_class_benchmarks

Returns the value of attribute class_benchmarks.



14
15
16
# File 'lib/debug_logging/configuration.rb', line 14

def class_benchmarks
  @class_benchmarks
end

#colorized_chain_for_classObject Also known as: debug_colorized_chain_for_class

Returns the value of attribute colorized_chain_for_class.



16
17
18
# File 'lib/debug_logging/configuration.rb', line 16

def colorized_chain_for_class
  @colorized_chain_for_class
end

#colorized_chain_for_methodObject Also known as: debug_colorized_chain_for_method

Returns the value of attribute colorized_chain_for_method.



15
16
17
# File 'lib/debug_logging/configuration.rb', line 15

def colorized_chain_for_method
  @colorized_chain_for_method
end

#ellipsisObject Also known as: debug_ellipsis

Returns the value of attribute ellipsis.



18
19
20
# File 'lib/debug_logging/configuration.rb', line 18

def ellipsis
  @ellipsis
end

#enabledObject Also known as: debug_enabled

For reference, log levels as integers mapped to symbols: LEVELS = { 0 => :debug, 1 => :info, 2 => :warn, 3 => :error, 4 => :fatal, 5 => :unknown }



6
7
8
# File 'lib/debug_logging/configuration.rb', line 6

def enabled
  @enabled
end

#instance_benchmarksObject Also known as: debug_instance_benchmarks

Returns the value of attribute instance_benchmarks.



13
14
15
# File 'lib/debug_logging/configuration.rb', line 13

def instance_benchmarks
  @instance_benchmarks
end

#last_hash_max_lengthObject Also known as: debug_last_hash_max_length

Returns the value of attribute last_hash_max_length.



11
12
13
# File 'lib/debug_logging/configuration.rb', line 11

def last_hash_max_length
  @last_hash_max_length
end

#last_hash_to_s_procObject Also known as: debug_last_hash_to_s_proc

Returns the value of attribute last_hash_to_s_proc.



10
11
12
# File 'lib/debug_logging/configuration.rb', line 10

def last_hash_to_s_proc
  @last_hash_to_s_proc
end

#log_levelObject Also known as: debug_log_level

Returns the value of attribute log_level.



8
9
10
# File 'lib/debug_logging/configuration.rb', line 8

def log_level
  @log_level
end

#loggerObject Also known as: debug_logger

Returns the value of attribute logger.



7
8
9
# File 'lib/debug_logging/configuration.rb', line 7

def logger
  @logger
end

#mark_scope_exitObject Also known as: debug_mark_scope_exit

Returns the value of attribute mark_scope_exit.



19
20
21
# File 'lib/debug_logging/configuration.rb', line 19

def mark_scope_exit
  @mark_scope_exit
end

#methods_to_logObject (readonly)

Returns the value of attribute methods_to_log.



20
21
22
# File 'lib/debug_logging/configuration.rb', line 20

def methods_to_log
  @methods_to_log
end

#multiple_last_hashesObject Also known as: debug_multiple_last_hashes

Returns the value of attribute multiple_last_hashes.



9
10
11
# File 'lib/debug_logging/configuration.rb', line 9

def multiple_last_hashes
  @multiple_last_hashes
end

Class Method Details

.config_pointer(type, method_to_log) ⇒ Object



55
56
57
58
59
60
# File 'lib/debug_logging/configuration.rb', line 55

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

#benchmarkable_for?(benchmarks) ⇒ Boolean

Returns:

  • (Boolean)


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

def benchmarkable_for?(benchmarks)
  return @benchmarkable if defined?(@benchmarkable)
  @benchmarkable = loggable? && self.send(benchmarks)
end

#exit_scope_markable?Boolean

Returns:

  • (Boolean)


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

def exit_scope_markable?
  return @exit_scope_markable if defined?(@exit_scope_markable)
  @exit_scope_markable = loggable? && mark_scope_exit
end

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



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

def log(message = nil, &block)
  return unless enabled
  return unless logger
  if block_given?
    logger.send(log_level, &block)
  else
    logger.send(log_level, message)
  end
end

#loggable?Boolean

Returns:

  • (Boolean)


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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/debug_logging/configuration.rb', line 108

def to_hash
  {
      logger: logger,
      log_level: log_level,
      multiple_last_hashes: multiple_last_hashes,
      last_hash_to_s_proc: last_hash_to_s_proc,
      last_hash_max_length: last_hash_max_length,
      args_max_length: args_max_length,
      instance_benchmarks: instance_benchmarks,
      class_benchmarks: class_benchmarks,
      colorized_chain_for_method: colorized_chain_for_method,
      colorized_chain_for_class: colorized_chain_for_class,
      add_invocation_id: add_invocation_id,
      ellipsis: ellipsis,
      mark_scope_exit: mark_scope_exit
  }
end