Class: MiniLogger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mini_logger.rb

Constant Summary collapse

DEBUG =
:debug
INFO =
:info
WARN =
:warn
ERROR =
:error
FATAL =
:fatal
LLM =
{
  :debug =>::Logger::DEBUG,
  :info  =>::Logger::INFO,
  :warn  =>::Logger::WARN,
  :error =>::Logger::ERROR,
  :fatal =>::Logger::FATAL
}
RLLM =
{
  ::Logger::DEBUG => DEBUG,
  ::Logger::INFO  => INFO,
  ::Logger::WARN  => WARN,
  ::Logger::ERROR => ERROR,
  ::Logger::FATAL => FATAL
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MiniLogger

Returns a new instance of MiniLogger.



80
81
82
83
84
85
86
# File 'lib/mini_logger.rb', line 80

def initialize(options)
  
  @logger       = ::Logger.new(options[:dev])
  @logger.level = MiniLogger.standarize_log_level(options[:level])
  
  self 
end

Class Method Details

.configure(*arguments) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mini_logger.rb', line 54

def self.configure(*arguments)
 
  configuration = Hash.new
  
  case arguments[0]
    when String then configuration.merge!(YAML.load_file(arguments[0]))
    when Hash   then configuration.merge!(arguments[0])
    else configuration = { :dev=>STDERR, :level=>:debug }
  end

  configuration = { :log_channel=>STDERR, :log_level=>:info }.merge(configuration)  
    
  configuration[:dev]   ||= configuration[:log_channel]
  configuration[:level] ||= configuration[:log_level]   
  
  raise ArgumentError.new("Invalid log level") unless validate_log_level?(configuration[:level])

  configuration[:dev] = case configuration[:dev].to_s.downcase
    when /stdout/ then STDOUT
    when /stderr/ then STDERR
    else configuration[:dev]
  end

  self.new(configuration)
end

.validate_log_level?(level) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/mini_logger.rb', line 44

def self.validate_log_level?(level)
  
  case level
    when String then LLM.has_key?(level.downcase.to_sym)
    when Symbol then LLM.has_key?(level.to_s.downcase.to_sym)
    else false
  end
end

Instance Method Details

#levelObject



96
97
98
99
# File 'lib/mini_logger.rb', line 96

def level
   
  RLLM[@logger.level]
end

#level=(level) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
# File 'lib/mini_logger.rb', line 88

def level=(level)
  
  raise ArgumentError.new("Invalid log level #{level.class.name}:'#{level}'") unless MiniLogger.validate_log_level?(level)
  @logger.level = MiniLogger.standarize_log_level(level)

  self
end