Class: Logem::BaseLogger
- Inherits:
-
Object
- Object
- Logem::BaseLogger
- Defined in:
- lib/logem/base_logger.rb
Direct Known Subclasses
Constant Summary collapse
- ERROR =
Log levels
50- WARN =
40- INFO =
30- DEBUG =
20- TRACE =
10- DEFAULT_LEVEL =
INFO- DEFAULT_LOG_LEVEL_ENV =
"LOGEM_LOG_LEVEL"
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#level ⇒ Object
Returns the value of attribute level.
-
#log_level_env ⇒ Object
readonly
Returns the value of attribute log_level_env.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(context, options = {}) ⇒ BaseLogger
constructor
A new instance of BaseLogger.
- #log(level, *args) ⇒ Object
- #visible?(level) ⇒ Boolean
Constructor Details
#initialize(context, options = {}) ⇒ BaseLogger
Returns a new instance of BaseLogger.
17 18 19 20 21 22 23 24 |
# File 'lib/logem/base_logger.rb', line 17 def initialize context, = {} @context = context @log_level_env = [:log_level_env ] || DEFAULT_LOG_LEVEL_ENV @level = [:level ] || self.class.string_to_level(ENV[@log_level_env]) || DEFAULT_LEVEL @output = [:output ] || $stdout @time_formatter = [:time_formatter] @output_supports_logem = @output.respond_to? :logem end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
14 15 16 |
# File 'lib/logem/base_logger.rb', line 14 def context @context end |
#level ⇒ Object
Returns the value of attribute level.
15 16 17 |
# File 'lib/logem/base_logger.rb', line 15 def level @level end |
#log_level_env ⇒ Object (readonly)
Returns the value of attribute log_level_env.
13 14 15 |
# File 'lib/logem/base_logger.rb', line 13 def log_level_env @log_level_env end |
Class Method Details
.level_to_string(level) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/logem/base_logger.rb', line 36 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
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/logem/base_logger.rb', line 47 def self.string_to_level level_string return DEFAULT_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_LEVEL).strip}" DEFAULT_LEVEL end end |
Instance Method Details
#log(level, *args) ⇒ Object
26 27 28 29 30 |
# File 'lib/logem/base_logger.rb', line 26 def log level, *args return if @level > level _log_ level, *args end |
#visible?(level) ⇒ Boolean
32 33 34 |
# File 'lib/logem/base_logger.rb', line 32 def visible? level @level <= level end |