Class: Drum::Logger
Overview
A simple logging facility.
Defined Under Namespace
Modules: Level
Instance Attribute Summary collapse
-
#level ⇒ Logger::Level
The minimum level of messages to be logged.
-
#output ⇒ Proc
A function taking a string for outputting a message.
Instance Method Summary collapse
-
#all(msg) ⇒ Object
Logs a message at the ALL level.
-
#debug(msg) ⇒ Object
Logs a message at the DEBUG level.
-
#error(msg) ⇒ Object
Logs a message at the ERROR level.
-
#info(msg) ⇒ Object
Logs a message at the INFO level.
-
#initialize(level: Level::INFO, output: method(:puts)) ⇒ Logger
constructor
Creates a new logger with the given level.
-
#log(level, msg) ⇒ Object
Logs a message at the given level.
-
#trace(msg) ⇒ Object
Logs a message at the TRACE level.
-
#warn(msg) ⇒ Object
Logs a message at the WARN level.
Constructor Details
Instance Attribute Details
#level ⇒ Logger::Level
Returns The minimum level of messages to be logged.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 |
# File 'lib/drum/utils/log.rb', line 10 class Logger module Level ALL = 100 ERROR = 2 WARN = 1 INFO = 0 DEBUG = -1 TRACE = -2 end attr_accessor :level attr_accessor :output # Creates a new logger with the given level. # # @param [Logger::Level] level The minimum level of messages to be logged. # @param [Proc] output A function taking a string for outputting a message. def initialize(level: Level::INFO, output: method(:puts)) self.level = level self.output = output end # Logs a message at the given level. # # @param [Logger::Level] level The level to log the message at. # @param [String] msg The message to log. def log(level, msg) if level >= self.level self.output.(msg) end end # Logs a message at the ALL level. # # @param [String] msg The message to log. def all(msg) self.log(Level::ALL, msg) end # Logs a message at the ERROR level. # # @param [String] msg The message to log. def error(msg) self.log(Level::ERROR, msg) end # Logs a message at the WARN level. # # @param [String] msg The message to log. def warn(msg) self.log(Level::WARN, msg) end # Logs a message at the INFO level. # # @param [String] msg The message to log. def info(msg) self.log(Level::INFO, msg) end # Logs a message at the DEBUG level. # # @param [String] msg The message to log. def debug(msg) self.log(Level::DEBUG, msg) end # Logs a message at the TRACE level. # # @param [String] msg The message to log. def trace(msg) self.log(Level::TRACE, msg) end end |
#output ⇒ Proc
Returns A function taking a string for outputting a message.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 |
# File 'lib/drum/utils/log.rb', line 10 class Logger module Level ALL = 100 ERROR = 2 WARN = 1 INFO = 0 DEBUG = -1 TRACE = -2 end attr_accessor :level attr_accessor :output # Creates a new logger with the given level. # # @param [Logger::Level] level The minimum level of messages to be logged. # @param [Proc] output A function taking a string for outputting a message. def initialize(level: Level::INFO, output: method(:puts)) self.level = level self.output = output end # Logs a message at the given level. # # @param [Logger::Level] level The level to log the message at. # @param [String] msg The message to log. def log(level, msg) if level >= self.level self.output.(msg) end end # Logs a message at the ALL level. # # @param [String] msg The message to log. def all(msg) self.log(Level::ALL, msg) end # Logs a message at the ERROR level. # # @param [String] msg The message to log. def error(msg) self.log(Level::ERROR, msg) end # Logs a message at the WARN level. # # @param [String] msg The message to log. def warn(msg) self.log(Level::WARN, msg) end # Logs a message at the INFO level. # # @param [String] msg The message to log. def info(msg) self.log(Level::INFO, msg) end # Logs a message at the DEBUG level. # # @param [String] msg The message to log. def debug(msg) self.log(Level::DEBUG, msg) end # Logs a message at the TRACE level. # # @param [String] msg The message to log. def trace(msg) self.log(Level::TRACE, msg) end end |
Instance Method Details
#all(msg) ⇒ Object
Logs a message at the ALL level.
45 46 47 |
# File 'lib/drum/utils/log.rb', line 45 def all(msg) self.log(Level::ALL, msg) end |
#debug(msg) ⇒ Object
Logs a message at the DEBUG level.
73 74 75 |
# File 'lib/drum/utils/log.rb', line 73 def debug(msg) self.log(Level::DEBUG, msg) end |
#error(msg) ⇒ Object
Logs a message at the ERROR level.
52 53 54 |
# File 'lib/drum/utils/log.rb', line 52 def error(msg) self.log(Level::ERROR, msg) end |
#info(msg) ⇒ Object
Logs a message at the INFO level.
66 67 68 |
# File 'lib/drum/utils/log.rb', line 66 def info(msg) self.log(Level::INFO, msg) end |
#log(level, msg) ⇒ Object
Logs a message at the given level.
36 37 38 39 40 |
# File 'lib/drum/utils/log.rb', line 36 def log(level, msg) if level >= self.level self.output.(msg) end end |