Class: Zephira::Logger
- Inherits:
-
Object
- Object
- Zephira::Logger
- Defined in:
- lib/zephira/logger.rb
Constant Summary collapse
- LOG_LEVELS =
%i[debug info warn error].freeze
Instance Attribute Summary collapse
-
#log_level ⇒ Object
readonly
Returns the value of attribute log_level.
-
#logfile ⇒ Object
readonly
Returns the value of attribute logfile.
Instance Method Summary collapse
- #debug(message, **args) ⇒ Object
- #error(message, **args) ⇒ Object
- #info(message, **args) ⇒ Object
-
#initialize(file_path:, log_level: :debug) ⇒ Logger
constructor
A new instance of Logger.
- #log(level, message, **args) ⇒ Object
- #should_log?(level) ⇒ Boolean
- #warn(message, **args) ⇒ Object
Constructor Details
#initialize(file_path:, log_level: :debug) ⇒ Logger
Returns a new instance of Logger.
11 12 13 14 15 16 17 |
# File 'lib/zephira/logger.rb', line 11 def initialize(file_path:, log_level: :debug) FileUtils.mkdir_p(File.dirname(file_path)) @logfile = File.open(file_path, "a") @log_level = log_level raise ArgumentError, "Invalid log level: #{log_level}" unless LOG_LEVELS.include?(log_level) end |
Instance Attribute Details
#log_level ⇒ Object (readonly)
Returns the value of attribute log_level.
9 10 11 |
# File 'lib/zephira/logger.rb', line 9 def log_level @log_level end |
#logfile ⇒ Object (readonly)
Returns the value of attribute logfile.
9 10 11 |
# File 'lib/zephira/logger.rb', line 9 def logfile @logfile end |
Instance Method Details
#debug(message, **args) ⇒ Object
19 20 21 |
# File 'lib/zephira/logger.rb', line 19 def debug(, **args) log(:debug, , **args) end |
#error(message, **args) ⇒ Object
27 28 29 |
# File 'lib/zephira/logger.rb', line 27 def error(, **args) log(:error, , **args) end |
#info(message, **args) ⇒ Object
23 24 25 |
# File 'lib/zephira/logger.rb', line 23 def info(, **args) log(:info, , **args) end |
#log(level, message, **args) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/zephira/logger.rb', line 35 def log(level, , **args) return unless should_log?(level) @logfile.puts "#{Time.now} - #{level.to_s.strip.upcase} - #{} - #{args.inspect}" @logfile.flush end |
#should_log?(level) ⇒ Boolean
42 43 44 |
# File 'lib/zephira/logger.rb', line 42 def should_log?(level) LOG_LEVELS.index(level) >= LOG_LEVELS.index(@log_level) end |
#warn(message, **args) ⇒ Object
31 32 33 |
# File 'lib/zephira/logger.rb', line 31 def warn(, **args) log(:warn, , **args) end |