Class: SpinelKit::Log
- Inherits:
-
Object
- Object
- SpinelKit::Log
- Defined in:
- lib/spinel_kit/log.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 a consumer-side type-seed.
Instance Method Summary collapse
- #debug(msg) ⇒ Object
- #error(msg) ⇒ Object
- #format_line(level, msg) ⇒ Object
- #info(msg) ⇒ Object
-
#initialize ⇒ Log
constructor
A new instance of Log.
- #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 ⇒ Log
Returns a new instance of Log.
31 32 33 34 |
# File 'lib/spinel_kit/log.rb', line 31 def initialize @min_level = "info" @file_path = "" end |
Instance Attribute Details
#file_path ⇒ Object
Returns the value of attribute file_path.
29 30 31 |
# File 'lib/spinel_kit/log.rb', line 29 def file_path @file_path end |
#min_level ⇒ Object
Returns the value of attribute min_level.
29 30 31 |
# File 'lib/spinel_kit/log.rb', line 29 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 a consumer-side type-seed.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/spinel_kit/log.rb', line 65 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
40 |
# File 'lib/spinel_kit/log.rb', line 40 def debug(msg); log("debug", msg); end |
#error(msg) ⇒ Object
43 |
# File 'lib/spinel_kit/log.rb', line 43 def error(msg); log("error", msg); end |
#format_line(level, msg) ⇒ Object
83 84 85 |
# File 'lib/spinel_kit/log.rb', line 83 def format_line(level, msg) "[" + Time.now.to_i.to_s + "] [" + level + "] " + msg end |
#info(msg) ⇒ Object
41 |
# File 'lib/spinel_kit/log.rb', line 41 def info(msg); log("info", msg); end |
#log(level, msg) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/spinel_kit/log.rb', line 45 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
36 |
# File 'lib/spinel_kit/log.rb', line 36 def set_level(name); @min_level = name; end |
#should_log?(level) ⇒ Boolean
59 60 61 |
# File 'lib/spinel_kit/log.rb', line 59 def should_log?(level) Log.level_value(level) >= Log.level_value(@min_level) end |
#to_file(path) ⇒ Object
37 |
# File 'lib/spinel_kit/log.rb', line 37 def to_file(path); @file_path = path; end |
#to_stderr ⇒ Object
38 |
# File 'lib/spinel_kit/log.rb', line 38 def to_stderr; @file_path = ""; end |
#warn(msg) ⇒ Object
42 |
# File 'lib/spinel_kit/log.rb', line 42 def warn(msg); log("warn", msg); end |