Class: ColouredLogger::CLogger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/coloured_logger.rb

Constant Summary collapse

SEVERITY_TO_COLOR_MAP =
{'DEBUG'=>'36', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37', 'PERF'=>'35'}
SEVERITY_TO_BACKGROUND_COLOR_MAP =
{'DEBUG'=>'0', 'INFO'=>'0', 'WARN'=>'0', 'ERROR'=>'0', 'FATAL'=>'103', 'UNKNOWN'=>'0', 'PERF'=>'0'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLogger

Returns a new instance of CLogger.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/coloured_logger.rb', line 15

def initialize
  @logger = Logger.new(STDOUT)
  # Logger.LEVEL = DEBUG - logs all levels, INFO - would not log debug level.
  @logger.level = Logger::DEBUG
  @logger.formatter = proc do |severity, time, progname, msg|
        formatted_severity = sprintf("%-5s",severity.to_s)
        formatted_time = sprintf("[%s]", time.strftime("%Y-%m-%d %H:%M:%S"))
        date_color ='1;34'
        severity_color = SEVERITY_TO_COLOR_MAP[severity]
        background_color = SEVERITY_TO_BACKGROUND_COLOR_MAP[severity]
        "\033[#{date_color}m#{formatted_time} \033[#{background_color};#{severity_color}m#{formatted_severity}\033[0m-- #{msg.to_s.strip}\n"
    end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Class Method Details

.debug(method, message) ⇒ Object

low-level information for developers



34
35
36
37
# File 'lib/coloured_logger.rb', line 34

def self.debug(method, message)
  formatted_method = CLogger.instance.formatted_method(method)
  CLogger.instance.logger.debug("#{formatted_method}: #{message}")
end

.error(method, message) ⇒ Object



50
51
52
53
# File 'lib/coloured_logger.rb', line 50

def self.error(method, message)
  formatted_method = CLogger.instance.formatted_method(method)
  CLogger.instance.logger.error("#{formatted_method}: #{message}")
end

.fatal(method, message) ⇒ Object



55
56
57
58
# File 'lib/coloured_logger.rb', line 55

def self.fatal(method, message)
  formatted_method = CLogger.instance.formatted_method(method)
  CLogger.instance.logger.fatal("#{formatted_method}: #{message}")
end

.info(method, message) ⇒ Object

generic (useful) information about system operation



40
41
42
43
# File 'lib/coloured_logger.rb', line 40

def self.info(method, message)
  formatted_method = CLogger.instance.formatted_method(method)
  CLogger.instance.logger.info("#{formatted_method}: #{message}")
end

.log_time(method, start_time, task_name) ⇒ Object

Logs time taken to execute a task



66
67
68
69
70
71
72
# File 'lib/coloured_logger.rb', line 66

def self.log_time(method, start_time, task_name)
  end_time = Time.now
  formatted_method = CLogger.instance.formatted_method(method)
  time_taken = end_time - start_time
  formatted_time_taken = sprintf("[%.4f sec]", time_taken)
  CLogger.instance.logger.perf("#{formatted_method}: Time taken to #{task_name} - \033[35m#{formatted_time_taken}\033[0m")
end

.unknown(method, message) ⇒ Object



60
61
62
63
# File 'lib/coloured_logger.rb', line 60

def self.unknown(method, message)
  formatted_method = CLogger.instance.formatted_method(method)
  CLogger.instance.logger.unknown("#{formatted_method}: #{message}")
end

.warn(method, message) ⇒ Object



45
46
47
48
# File 'lib/coloured_logger.rb', line 45

def self.warn(method, message)
  formatted_method = CLogger.instance.formatted_method(method)
  CLogger.instance.logger.warn("#{formatted_method}: #{message}")
end

Instance Method Details

#formatted_method(method) ⇒ Object



29
30
31
# File 'lib/coloured_logger.rb', line 29

def formatted_method(method)
  sprintf("%-20s", method)
end