Class: Loops::Logger::LoggerImplementation

Inherits:
Logger
  • Object
show all
Defined in:
lib/loops/logger.rb

Defined Under Namespace

Classes: Formatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_device, number_of_files = 10, max_file_size = 10 * 1024 * 1024, write_to_console = true, colorful_logs = false) ⇒ LoggerImplementation

Returns a new instance of LoggerImplementation.



169
170
171
172
173
174
175
# File 'lib/loops/logger.rb', line 169

def initialize(log_device, number_of_files = 10, max_file_size = 10 * 1024 * 1024, write_to_console = true, colorful_logs = false)
  super(log_device, number_of_files, max_file_size)
  self.formatter    = Formatter.new(self)
  @write_to_console = write_to_console
  @colorful_logs    = colorful_logs
  @prefix           = nil
end

Instance Attribute Details

#colorful_logsObject

Returns the value of attribute colorful_logs.



152
153
154
# File 'lib/loops/logger.rb', line 152

def colorful_logs
  @colorful_logs
end

#prefixObject (readonly)

Returns the value of attribute prefix.



150
151
152
# File 'lib/loops/logger.rb', line 150

def prefix
  @prefix
end

#write_to_consoleObject

Returns the value of attribute write_to_console.



152
153
154
# File 'lib/loops/logger.rb', line 152

def write_to_console
  @write_to_console
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/loops/logger.rb', line 177

def add(severity, message = nil, progname = nil, &block)
  begin
    if @colorful_logs
      message = color_errors(severity, message)
      progname = color_errors(severity, progname)
    end
    super(severity, message, progname, &block)
    if @write_to_console && (message || progname)
      puts self.formatter.call(%w(D I W E F A)[severity] || 'A', Time.now, progname, message)
    end
  rescue
    # ignore errors in logging
  end
end

#color_errors(severity, line) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/loops/logger.rb', line 203

def color_errors(severity, line)
  if severity < ::Logger::ERROR
    line
  else
    if line && line !~ /\e/
      "\e[31m#{line}\e[0m"
    else
      line
    end
  end
end

#with_prefix(prefix) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/loops/logger.rb', line 192

def with_prefix(prefix)
  raise "No block given" unless block_given?
  old_prefix = @prefix
  @prefix = prefix
  begin
    yield
  ensure
    @prefix = old_prefix
  end
end