Class: DebugLogging::Configuration

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

Constant Summary collapse

DEFAULT_ELLIPSIS =
' ✂️ …'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Configuration

Returns a new instance of Configuration.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/debug_logging/configuration.rb', line 54

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.



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

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.



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

def args_max_length
  @args_max_length
end

#class_benchmarksObject Also known as: debug_class_benchmarks

Returns the value of attribute class_benchmarks.



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

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.



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

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.



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

def colorized_chain_for_method
  @colorized_chain_for_method
end

#ellipsisObject Also known as: debug_ellipsis

Returns the value of attribute ellipsis.



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

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 }



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

def enabled
  @enabled
end

#instance_benchmarksObject Also known as: debug_instance_benchmarks

Returns the value of attribute instance_benchmarks.



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

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.



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

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.



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

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.



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

def log_level
  @log_level
end

#loggerObject Also known as: debug_logger

Returns the value of attribute logger.



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

def logger
  @logger
end

#mark_scope_exitObject Also known as: debug_mark_scope_exit

Returns the value of attribute mark_scope_exit.



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

def mark_scope_exit
  @mark_scope_exit
end

#methods_to_logObject (readonly)

Returns the value of attribute methods_to_log.



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

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



47
48
49
50
51
52
# File 'lib/debug_logging/configuration.rb', line 47

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)


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

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

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

#exit_scope_markable?Boolean

Returns:

  • (Boolean)


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

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



72
73
74
75
76
77
78
79
80
81
# File 'lib/debug_logging/configuration.rb', line 72

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)


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

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

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

#register(method_lo_log) ⇒ Object



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

def register(method_lo_log)
  @methods_to_log << method_lo_log
end

#to_hashObject



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

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