Class: Tep::Logger
- Inherits:
-
Object
- Object
- Tep::Logger
- Defined in:
- lib/tep/logger.rb
Instance Attribute Summary collapse
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#min_level ⇒ Object
Returns the value of attribute min_level.
Class Method Summary collapse
-
.level_value(name) ⇒ Object
Class-side helper so the comparison stays a pure function and spinel pins its arg type to :str cleanly via the type-seed in tep.rb.
Instance Method Summary collapse
- #debug(msg) ⇒ Object
- #error(msg) ⇒ Object
- #format_line(level, msg) ⇒ Object
- #info(msg) ⇒ Object
-
#initialize ⇒ Logger
constructor
A new instance of Logger.
- #log(level, msg) ⇒ Object
- #set_level(name) ⇒ Object
- #should_log?(level) ⇒ Boolean
- #to_file(path) ⇒ Object
- #to_stderr ⇒ Object
- #warn(msg) ⇒ Object
Constructor Details
#initialize ⇒ Logger
Returns a new instance of Logger.
28 29 30 31 |
# File 'lib/tep/logger.rb', line 28 def initialize @min_level = "info" @file_path = "" end |
Instance Attribute Details
#file_path ⇒ Object
Returns the value of attribute file_path.
26 27 28 |
# File 'lib/tep/logger.rb', line 26 def file_path @file_path end |
#min_level ⇒ Object
Returns the value of attribute min_level.
26 27 28 |
# File 'lib/tep/logger.rb', line 26 def min_level @min_level end |
Class Method Details
.level_value(name) ⇒ Object
Class-side helper so the comparison stays a pure function and spinel pins its arg type to :str cleanly via the type-seed in tep.rb.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tep/logger.rb', line 63 def self.level_value(name) if name == "debug" return 0 end if name == "info" return 1 end if name == "warn" return 2 end if name == "error" return 3 end # Unknown level -- treat as info so misspelled labels don't # vanish silently. 1 end |
Instance Method Details
#debug(msg) ⇒ Object
37 |
# File 'lib/tep/logger.rb', line 37 def debug(msg); log("debug", msg); end |
#error(msg) ⇒ Object
40 |
# File 'lib/tep/logger.rb', line 40 def error(msg); log("error", msg); end |
#format_line(level, msg) ⇒ Object
81 82 83 |
# File 'lib/tep/logger.rb', line 81 def format_line(level, msg) "[" + Time.now.to_i.to_s + "] [" + level + "] " + msg end |
#info(msg) ⇒ Object
38 |
# File 'lib/tep/logger.rb', line 38 def info(msg); log("info", msg); end |
#log(level, msg) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tep/logger.rb', line 42 def log(level, msg) if !should_log?(level) return end line = format_line(level, msg) if @file_path.length > 0 File.open(@file_path, "a") do |f| f.puts(line) end else $stderr.puts(line) end end |
#set_level(name) ⇒ Object
33 |
# File 'lib/tep/logger.rb', line 33 def set_level(name); @min_level = name; end |
#should_log?(level) ⇒ Boolean
56 57 58 |
# File 'lib/tep/logger.rb', line 56 def should_log?(level) Logger.level_value(level) >= Logger.level_value(@min_level) end |
#to_file(path) ⇒ Object
34 |
# File 'lib/tep/logger.rb', line 34 def to_file(path); @file_path = path; end |
#to_stderr ⇒ Object
35 |
# File 'lib/tep/logger.rb', line 35 def to_stderr; @file_path = ""; end |
#warn(msg) ⇒ Object
39 |
# File 'lib/tep/logger.rb', line 39 def warn(msg); log("warn", msg); end |