Class: Epuber::Logger::AbstractLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/utils/logger/abstract_logger.rb

Direct Known Subclasses

ConsoleLogger

Constant Summary collapse

LEVELS =
%i[
  error
  warning
  info
  debug
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ AbstractLogger

Returns a new instance of AbstractLogger.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :verbose (Boolean) — default: false
  • :debug_steps_times (Boolean) — default: false


30
31
32
33
34
35
36
37
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 30

def initialize(opts = {})
  @verbose = opts.fetch(:verbose, false)
  @debug_steps_times = opts.fetch(:debug_steps_times, false)

  @current_file = nil
  @sticky_message = nil
  @error_was_logged = false
end

Instance Attribute Details

#debug_steps_timesBoolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 24

def debug_steps_times
  @debug_steps_times
end

#verboseBoolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 20

def verbose
  @verbose
end

Instance Method Details

#debug(message, sticky: false, location: nil, backtrace: caller_locations) ⇒ Object

Report debug message

Parameters:



83
84
85
86
87
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 83

def debug(message, sticky: false, location: nil, backtrace: caller_locations)
  return unless @verbose

  _common_log(:debug, message, sticky: sticky, location: location, backtrace: backtrace)
end

#end_processingObject



99
100
101
102
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 99

def end_processing
  _remove_sticky_message
  @current_file = nil
end

#error(message, sticky: false, location: nil, backtrace: caller_locations) ⇒ Object

Report error message

Parameters:



56
57
58
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 56

def error(message, sticky: false, location: nil, backtrace: caller_locations)
  _common_log(:error, message, sticky: sticky, location: location, backtrace: backtrace)
end

#error!(message, location: nil, backtrace: caller_locations) ⇒ Object

Report error message

Parameters:



45
46
47
48
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 45

def error!(message, location: nil, backtrace: caller_locations)
  _common_log(:error, message, location: location, backtrace: backtrace)
  exit(1)
end

#error?Boolean

Return true if there was any error logged

Returns:

  • (Boolean)


145
146
147
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 145

def error?
  @error_was_logged
end

#info(message, sticky: false, location: nil, backtrace: caller_locations) ⇒ Object

Report info message

Parameters:



74
75
76
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 74

def info(message, sticky: false, location: nil, backtrace: caller_locations)
  _common_log(:info, message, sticky: sticky, location: location, backtrace: backtrace)
end

Parameters:

  • info_text (String)


106
107
108
109
110
111
112
113
114
115
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 106

def print_processing_debug_info(info_text)
  return unless @verbose

  message = if @current_file.nil?
              "#{info_text}"
            else
              "#{@current_file.source_path}: #{info_text}"
            end
  _log(:debug, message)
end

Parameters:

  • step_name (String)
  • time (Fixnum) (defaults to: nil)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 120

def print_step_processing_time(step_name, time = nil)
  return yield unless @debug_steps_times

  if block_given?
    start = Time.now
    returned_value = yield

    time = Time.now - start
  end

  info_text = "Step #{step_name} took #{(time * 1000).round(2)} ms"
  message = if @current_file.nil?
              "#{info_text}"
            else
              "#{@current_file.source_path}: #{info_text}"
            end

  _log(:debug, message)

  returned_value
end

#start_processing_file(file, index, count) ⇒ Object

Parameters:



93
94
95
96
97
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 93

def start_processing_file(file, index, count)
  @current_file = file

  _common_log(:debug, "▸ Processing #{file.source_path} (#{index + 1} of #{count})", sticky: true)
end

#warning(message, sticky: false, location: nil, backtrace: caller_locations) ⇒ Object

Report warning message

Parameters:



65
66
67
# File 'lib/epuber/utils/logger/abstract_logger.rb', line 65

def warning(message, sticky: false, location: nil, backtrace: caller_locations)
  _common_log(:warning, message, sticky: sticky, location: location, backtrace: backtrace)
end