Class: Logging::Logger

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(label = nil, &block) {|logger| ... } ⇒ Logger

Create a new logger.

Examples:

# Create logger with default values, specifying
# only the label (mandatory when not specifying io).
logger = Logging::Logger.new('logs.my_app.db')

# Create a logger specifying a custom formatter and io.
logger = Logging::Logger.new('logs.my_app.db') do |logger|
  logger.io = Logging::IO::Pipe.new(logger.label, '/var/mypipe')
  logger.formatter = Logging::Formatters::Colourful.new
end

Parameters:

  • label (String, nil) (defaults to: nil)

    Label. For instance ‘logs.myapp.db`.

  • block (Proc)

    Block with the newly created instance.

Yields:

  • (logger)

    The logger instance which just has been created.



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

#formatterObject

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

#ioObject

The cached io instance. If there’s none, one will be created.

Raises:

  • (RuntimeError)

    If the label isn’t specified (when creating new deafult io).



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

#labelObject (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.

Parameters:

  • messages (Array<#to_s>)

    Messages to be logged.



78
79
80
81
82
# File 'lib/logging/logger.rb', line 78

LEVELS.each do |level|
  define_method(level) do |*messages|
    log(level, *messages)
  end
end

#info(*messages) ⇒ Object

Log an info message.

Parameters:

  • messages (Array<#to_s>)

    Messages to be logged.



78
79
80
81
82
# File 'lib/logging/logger.rb', line 78

LEVELS.each do |level|
  define_method(level) do |*messages|
    log(level, *messages)
  end
end

#inspect(*objects) ⇒ Object #inspect(label, *objects) ⇒ Object

Note:

This method is defined in logging/code.rb and requires coderay.

Inspect Ruby objects with syntax highlighting in JSON format.

Examples:

# Single object, no label.
logger.inspect(path: "/", time: 0.0001)

# Single object with String label.
logger.inspect("Request data", path: "/", time: 0.0001)

# Single object with Symbol label.
logger.inspect(:request, {path: "/", time: 0.0001})

# Multiple objects, no label.
logger.inspect({path: "/", time: 0.001}, {path: "/test"})

# Multiple objects with label.
logger.inspect("Requests", {path: "/", time: 0.001}, {path: "/test"})

Overloads:

  • #inspect(*objects) ⇒ Object

    Parameters:

    • objects (Array<#to_json>)

      List of objects for inspection.

  • #inspect(label, *objects) ⇒ Object

    Parameters:

    • label (String, Symbol)

      Label. For instance “Request time”.

    • objects (Array<#to_json>)

      List of objects for inspection.



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.

Parameters:

  • level (Symbol)

    Log level.

  • messages (Array<#to_s>)

    Messages to be logged.



89
90
91
92
93
94
95
# File 'lib/logging/logger.rb', line 89

def log(level, *messages)
  if messages.length == 1
    self.io.write_single_message(self.formatter, level, messages.first)
  else
    self.io.write_multiple_messages(self.formatter, level, messages)
  end
end

#measure_time(label, &block) ⇒ Object

Note:

This method is defined in logging/code.rb and requires coderay.

Measure how long does it take to execute provided block.

Examples:

logger.measure_time("Request took %s") do
  sleep 0.1
end

Parameters:

  • label (#%)

    Formatting string.

  • block (Proc)

    The block of which we’ll measure the execution time.



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.

Parameters:

  • messages (Array<#to_s>)

    Messages to be logged.



78
79
80
81
82
# File 'lib/logging/logger.rb', line 78

LEVELS.each do |level|
  define_method(level) do |*messages|
    log(level, *messages)
  end
end

#write(message) ⇒ Object

Delegate to ‘self.io#write.

Parameters:

  • message (#to_s)

    Message to be written on the IO object.



100
101
102
# File 'lib/logging/logger.rb', line 100

def write(message)
  self.io.write(message)
end