Class: Logem::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/logem/logger.rb

Constant Summary collapse

ERROR =

Log levels

50
WARN =
40
INFO =
30
DEBUG =
20
TRACE =
10
DEFAULT_VISIBLE_LEVEL =
INFO
DEFAULT_LOG_LEVEL_ENV =
"LOGEM_LOG_LEVEL"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options = {}) ⇒ Logger

Returns a new instance of Logger.



16
17
18
19
20
21
22
23
# File 'lib/logem/logger.rb', line 16

def initialize context, options = {}
  @context        = context
  @log_level_env  = options[:log_level_env ] || DEFAULT_LOG_LEVEL_ENV
  @visible_level  = options[:visible_level ] || self.class.string_to_level(ENV[@log_level_env]) || DEFAULT_VISIBLE_LEVEL
  @output         = options[:output        ] || $stdout
  @time_formatter = options[:time_formatter]
  @output_supports_logem = @output.respond_to? :logem
end

Instance Attribute Details

#log_level_envObject (readonly)

Returns the value of attribute log_level_env.



13
14
15
# File 'lib/logem/logger.rb', line 13

def log_level_env
  @log_level_env
end

#visible_levelObject

Returns the value of attribute visible_level.



14
15
16
# File 'lib/logem/logger.rb', line 14

def visible_level
  @visible_level
end

Class Method Details

.level_to_string(level) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/logem/logger.rb', line 35

def self.level_to_string level
  case level
  when ERROR then "ERROR"
  when WARN  then "WARN "
  when INFO  then "INFO "
  when DEBUG then "DEBUG"
  when TRACE then "TRACE"
  else level.to_s
  end
end

.string_to_level(level_string) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/logem/logger.rb', line 46

def self.string_to_level level_string
  return DEFAULT_VISIBLE_LEVEL if level_string.nil? or level_string.strip == ''

  case level_string.strip.downcase
  when 'error' then ERROR
  when 'warn'  then WARN
  when 'info'  then INFO
  when 'debug' then DEBUG
  when 'trace' then TRACE
  else
    $stdout.puts "Logem warning: #{level_string} is not a valid log level, " +
                 "default to #{level_to_string(DEFAULT_VISIBLE_LEVEL).strip}"
    DEFAULT_VISIBLE_LEVEL
  end
end

Instance Method Details

#log(level, *args) ⇒ Object



25
26
27
28
29
# File 'lib/logem/logger.rb', line 25

def log level, *args
  return if @visible_level > level

  _log_ level, *args
end

#visible?(level) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/logem/logger.rb', line 31

def visible? level
  @visible_level <= level
end