Class: Logging::Logger
- Inherits:
-
Object
- Object
- Logging::Logger
- Defined in:
- lib/logging/logger.rb,
lib/logging/code.rb
Overview
Main class. Instantiate it to start logging. In reality all this class does is to provide convenience proxy to io objects and formatters.
Instance Attribute Summary collapse
-
#formatter ⇒ Object
The cached formatter instance.
-
#io ⇒ Object
The cached io instance.
-
#label ⇒ Object
readonly
Label is required only when you use the default io object.
Instance Method Summary collapse
-
#error(*messages) ⇒ Object
Log an error message.
-
#info(*messages) ⇒ Object
Log an info message.
-
#initialize(label = nil, &block) {|logger| ... } ⇒ Logger
constructor
Create a new logger.
-
#inspect(*objects) ⇒ Object
Inspect Ruby objects with syntax highlighting in JSON format.
-
#log(level, *messages) ⇒ Object
Underlaying function for logging any kind of message.
-
#measure_time(label, &block) ⇒ Object
Measure how long does it take to execute provided block.
-
#warn(*messages) ⇒ Object
Log a warning message.
-
#write(message) ⇒ Object
Delegate to ‘self.io#write.
Constructor Details
#initialize(label = nil, &block) {|logger| ... } ⇒ Logger
Create a new logger.
37 38 39 40 |
# File 'lib/logging/logger.rb', line 37 def initialize(label = nil, &block) @label = label block.call(self) if block end |
Instance Attribute Details
#formatter ⇒ Object
The cached formatter instance. If there’s none, one will be created.
58 59 60 |
# File 'lib/logging/logger.rb', line 58 def formatter @formatter ||= Formatters::Default.new end |
#io ⇒ Object
The cached io instance. If there’s none, one will be created.
45 46 47 48 49 50 51 52 53 |
# File 'lib/logging/logger.rb', line 45 def io @io ||= begin if self.label IO::Raw.new(self.label) else raise "You have to provide label in Logger.new if you want to use the default io object!" end end end |
#label ⇒ Object (readonly)
Label is required only when you use the default io object.
18 19 20 |
# File 'lib/logging/logger.rb', line 18 def label @label end |
Instance Method Details
#error(*messages) ⇒ Object
Log an error message.
78 79 80 81 82 |
# File 'lib/logging/logger.rb', line 78 LEVELS.each do |level| define_method(level) do |*| log(level, *) end end |
#info(*messages) ⇒ Object
Log an info message.
78 79 80 81 82 |
# File 'lib/logging/logger.rb', line 78 LEVELS.each do |level| define_method(level) do |*| log(level, *) end end |
#inspect(*objects) ⇒ Object #inspect(label, *objects) ⇒ Object
This method is defined in logging/code.rb and requires coderay.
Inspect Ruby objects with syntax highlighting in JSON format.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/logging/code.rb', line 38 def inspect(*objects) label = ((objects.first.is_a?(String) || objects.first.is_a?(Symbol)) && objects.length > 1) ? objects.shift : nil code = objects.map do |object| begin json = JSON.generate(object, object_nl: ' ', array_nl: ' ', space: ' ') CodeRay.scan(json, :json).terminal rescue CodeRay.scan(object.inspect, :ruby).terminal end end.join("\n") self.log(:inspect, label ? "#{label}: #{code}" : code) end |
#log(level, *messages) ⇒ Object
Underlaying function for logging any kind of message. The actual functionality is delegated to #io.
89 90 91 92 93 94 95 |
# File 'lib/logging/logger.rb', line 89 def log(level, *) if .length == 1 self.io.(self.formatter, level, .first) else self.io.(self.formatter, level, ) end end |
#measure_time(label, &block) ⇒ Object
This method is defined in logging/code.rb and requires coderay.
Measure how long does it take to execute provided block.
70 71 72 73 |
# File 'lib/logging/code.rb', line 70 def measure_time(label, &block) before = Time.now.to_f; block.call self.info(label % (Time.now.to_f - before)) end |
#warn(*messages) ⇒ Object
Log a warning message.
78 79 80 81 82 |
# File 'lib/logging/logger.rb', line 78 LEVELS.each do |level| define_method(level) do |*| log(level, *) end end |
#write(message) ⇒ Object
Delegate to ‘self.io#write.
100 101 102 |
# File 'lib/logging/logger.rb', line 100 def write() self.io.write() end |