Class: ColouredLogger::CLogger
- Inherits:
-
Object
- Object
- ColouredLogger::CLogger
- 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
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
-
.debug(method, message) ⇒ Object
low-level information for developers.
- .error(method, message) ⇒ Object
- .fatal(method, message) ⇒ Object
-
.info(method, message) ⇒ Object
generic (useful) information about system operation.
-
.log_time(method, start_time, task_name) ⇒ Object
Logs time taken to execute a task.
- .unknown(method, message) ⇒ Object
- .warn(method, message) ⇒ Object
Instance Method Summary collapse
- #formatted_method(method) ⇒ Object
-
#initialize ⇒ CLogger
constructor
A new instance of CLogger.
Constructor Details
#initialize ⇒ CLogger
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
#logger ⇒ Object (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, ) formatted_method = CLogger.instance.formatted_method(method) CLogger.instance.logger.debug("#{formatted_method}: #{}") end |
.error(method, message) ⇒ Object
50 51 52 53 |
# File 'lib/coloured_logger.rb', line 50 def self.error(method, ) formatted_method = CLogger.instance.formatted_method(method) CLogger.instance.logger.error("#{formatted_method}: #{}") end |
.fatal(method, message) ⇒ Object
55 56 57 58 |
# File 'lib/coloured_logger.rb', line 55 def self.fatal(method, ) formatted_method = CLogger.instance.formatted_method(method) CLogger.instance.logger.fatal("#{formatted_method}: #{}") 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, ) formatted_method = CLogger.instance.formatted_method(method) CLogger.instance.logger.info("#{formatted_method}: #{}") 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 |
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 |