Class: Drum::Logger

Inherits:
Object show all
Defined in:
lib/drum/utils/log.rb

Overview

A simple logging facility.

Defined Under Namespace

Modules: Level

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level: Level::INFO, output: method(:puts)) ⇒ Logger

Creates a new logger with the given level.

Parameters:

  • level (Logger::Level) (defaults to: Level::INFO)

    The minimum level of messages to be logged.

  • output (Proc) (defaults to: method(:puts))

    A function taking a string for outputting a message.



27
28
29
30
# File 'lib/drum/utils/log.rb', line 27

def initialize(level: Level::INFO, output: method(:puts))
  self.level = level
  self.output = output
end

Instance Attribute Details

#levelLogger::Level

Returns The minimum level of messages to be logged.

Returns:



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

#outputProc

Returns A function taking a string for outputting a message.

Returns:

  • (Proc)

    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.

Parameters:

  • msg (String)

    The message to log.



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.

Parameters:

  • msg (String)

    The message to log.



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.

Parameters:

  • msg (String)

    The message to log.



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.

Parameters:

  • msg (String)

    The message to log.



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.

Parameters:

  • level (Logger::Level)

    The level to log the message at.

  • msg (String)

    The message to log.



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

#trace(msg) ⇒ Object

Logs a message at the TRACE level.

Parameters:

  • msg (String)

    The message to log.



80
81
82
# File 'lib/drum/utils/log.rb', line 80

def trace(msg)
  self.log(Level::TRACE, msg)
end

#warn(msg) ⇒ Object

Logs a message at the WARN level.

Parameters:

  • msg (String)

    The message to log.



59
60
61
# File 'lib/drum/utils/log.rb', line 59

def warn(msg)
  self.log(Level::WARN, msg)
end