Class: Heathrow::Logger
- Inherits:
-
Object
- Object
- Heathrow::Logger
- Defined in:
- lib/heathrow/logger.rb
Overview
Constant Summary collapse
- LEVELS =
{ debug: ::Logger::DEBUG, info: ::Logger::INFO, warn: ::Logger::WARN, error: ::Logger::ERROR, fatal: ::Logger::FATAL }.freeze
Instance Attribute Summary collapse
-
#log_file ⇒ Object
readonly
Returns the value of attribute log_file.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
-
.configure(log_file: nil, level: :info) ⇒ Object
Configure the singleton instance.
- .instance ⇒ Object
- .reset_instance! ⇒ Object
Instance Method Summary collapse
-
#close ⇒ Object
Close logger.
-
#debug(message, context = {}) ⇒ Object
Log methods.
- #error(message, context = {}) ⇒ Object
- #fatal(message, context = {}) ⇒ Object
- #info(message, context = {}) ⇒ Object
-
#initialize(log_file = nil, level: :info) ⇒ Logger
constructor
A new instance of Logger.
-
#level ⇒ Object
Get current log level as symbol.
-
#level=(level) ⇒ Object
Change log level.
-
#log(level, message, context = {}) ⇒ Object
Generic log method.
- #warn(message, context = {}) ⇒ Object
Constructor Details
#initialize(log_file = nil, level: :info) ⇒ Logger
Returns a new instance of Logger.
26 27 28 29 30 31 32 |
# File 'lib/heathrow/logger.rb', line 26 def initialize(log_file = nil, level: :info) @log_file = log_file || default_log_file ensure_log_directory @logger = ::Logger.new(@log_file, 'daily') @logger.level = LEVELS[level] || ::Logger::INFO @logger.formatter = method(:format_message) end |
Instance Attribute Details
#log_file ⇒ Object (readonly)
Returns the value of attribute log_file.
24 25 26 |
# File 'lib/heathrow/logger.rb', line 24 def log_file @log_file end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
24 25 26 |
# File 'lib/heathrow/logger.rb', line 24 def logger @logger end |
Class Method Details
.configure(log_file: nil, level: :info) ⇒ Object
Configure the singleton instance
116 117 118 119 |
# File 'lib/heathrow/logger.rb', line 116 def configure(log_file: nil, level: :info) reset_instance! @instance = new(log_file, level: level) end |
.instance ⇒ Object
106 107 108 |
# File 'lib/heathrow/logger.rb', line 106 def instance @instance ||= new end |
.reset_instance! ⇒ Object
110 111 112 113 |
# File 'lib/heathrow/logger.rb', line 110 def reset_instance! @instance&.close @instance = nil end |
Instance Method Details
#close ⇒ Object
Close logger
83 84 85 |
# File 'lib/heathrow/logger.rb', line 83 def close @logger.close if @logger end |
#debug(message, context = {}) ⇒ Object
Log methods
35 36 37 |
# File 'lib/heathrow/logger.rb', line 35 def debug(, context = {}) log(:debug, , context) end |
#error(message, context = {}) ⇒ Object
47 48 49 |
# File 'lib/heathrow/logger.rb', line 47 def error(, context = {}) log(:error, , context) end |
#fatal(message, context = {}) ⇒ Object
51 52 53 |
# File 'lib/heathrow/logger.rb', line 51 def fatal(, context = {}) log(:fatal, , context) end |
#info(message, context = {}) ⇒ Object
39 40 41 |
# File 'lib/heathrow/logger.rb', line 39 def info(, context = {}) log(:info, , context) end |
#level ⇒ Object
Get current log level as symbol
78 79 80 |
# File 'lib/heathrow/logger.rb', line 78 def level LEVELS.key(@logger.level) || :info end |
#level=(level) ⇒ Object
Change log level
73 74 75 |
# File 'lib/heathrow/logger.rb', line 73 def level=(level) @logger.level = LEVELS[level] || ::Logger::INFO end |
#log(level, message, context = {}) ⇒ Object
Generic log method
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/heathrow/logger.rb', line 56 def log(level, , context = {}) return unless @logger # Extract error if present if context[:error].is_a?(Exception) error = context.delete(:error) context[:error_class] = error.class.name context[:error_message] = error. context[:backtrace] = error.backtrace&.first(5) end @logger.send(level, ) do context.empty? ? : "#{} #{context.inspect}" end end |
#warn(message, context = {}) ⇒ Object
43 44 45 |
# File 'lib/heathrow/logger.rb', line 43 def warn(, context = {}) log(:warn, , context) end |