Module: MiniLogger

Extended by:
MiniLogger
Included in:
MiniLogger
Defined in:
lib/version.rb,
lib/mini_logger.rb

Defined Under Namespace

Classes: Version

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/mini_logger.rb', line 113

def method_missing(method, *arguments, &block)
 
  if @logger && [:debug, :info, :warn, :error, :fatal, :debug?, :info?, :warn?, :error?, :fatal?].include?(method)

    @logger.send(method, *arguments, &block)
  end
end

Class Method Details

.standarize_log_level(ll) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/mini_logger.rb', line 33

def self.standarize_log_level(ll)

  case ll
    when String then LLM[ll.downcase.to_sym]
    when Symbol then LLM[ll.to_s.downcase.to_sym]
  end
end

.validate_log_level?(ll) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#configure(*arguments) ⇒ Object

Raises:

  • (ArgumentError)


52
53
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
79
80
81
82
83
84
# File 'lib/mini_logger.rb', line 52

def configure(*arguments)
 
  configuration = {}
  
  case arguments[0]
    when String
      configuration.merge!(YAML.load_file(arguments[0]))
    when Hash                        
      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] unless configuration[:dev]
  configuration[:level] = configuration[:log_level]   unless configuration[:level]   
  
  raise ArgumentError.new("Invalid log level") unless validate_log_level?(configuration[:level])

  configuration[:dev] = case configuration[:dev]
    when /(STDOUT|stdout)/ then STDOUT
    when /(STDERR|stderr)/ then STDERR
    when :stdout           then STDOUT
    when :stderr           then STDERR
    else configuration[:dev]
  end

  @logger       = ::Logger.new(configuration[:dev])
  @logger.level = standarize_log_level(configuration[:level])
  
  self
end

#levelObject



108
109
110
111
# File 'lib/mini_logger.rb', line 108

def level
   
  RLLM[@logger.level] if @logger
end

#level!(nll) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/mini_logger.rb', line 97

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

#level=(nll) ⇒ Object



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

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