Class: Bridgetown::LogAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown-core/log_adapter.rb

Constant Summary collapse

LOG_LEVELS =
{
  debug: ::Logger::DEBUG,
  info: ::Logger::INFO,
  warn: ::Logger::WARN,
  error: ::Logger::ERROR,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writer, level = :info) ⇒ LogAdapter

Create a new instance of a log writer

Parameters:

  • writer (Logger)

    compatible instance

  • log_level (Symbol)

    the log level (debug | info | warn | error)



18
19
20
21
22
# File 'lib/bridgetown-core/log_adapter.rb', line 18

def initialize(writer, level = :info)
  @messages = []
  @writer = writer
  self.log_level = level
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



5
6
7
# File 'lib/bridgetown-core/log_adapter.rb', line 5

def level
  @level
end

#messagesObject (readonly)

Returns the value of attribute messages.



5
6
7
# File 'lib/bridgetown-core/log_adapter.rb', line 5

def messages
  @messages
end

#writerObject (readonly)

Returns the value of attribute writer.



5
6
7
# File 'lib/bridgetown-core/log_adapter.rb', line 5

def writer
  @writer
end

Instance Method Details

#abort_with(topic, message = nil) ⇒ Object

Print an error message and immediately abort the process

Parameters:

  • topic (String)

    e.g. "Configuration file", "Deprecation", etc.

  • message (String) (defaults to: nil)

    the message detail



80
81
82
83
# File 'lib/bridgetown-core/log_adapter.rb', line 80

def abort_with(topic, message = nil, &)
  error(topic, message, &)
  abort
end

#adjust_verbosity(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/bridgetown-core/log_adapter.rb', line 34

def adjust_verbosity(options = {})
  # Quiet always wins.
  if options[:quiet]
    self.log_level = :error
  elsif options[:verbose]
    self.log_level = :debug
  end
  debug "Logging at level:", LOG_LEVELS.key(writer.level).to_s
end

#debug(topic, message = nil) ⇒ Object

Print a debug message

Parameters:

  • topic (String)

    e.g. "Configuration file", "Deprecation", etc.

  • message (String) (defaults to: nil)

    the message detail



48
49
50
# File 'lib/bridgetown-core/log_adapter.rb', line 48

def debug(topic, message = nil, &)
  write(:debug, topic, message, &)
end

#error(topic, message = nil) ⇒ Object

Print an error message

Parameters:

  • topic (String)

    e.g. "Configuration file", "Deprecation", etc.

  • message (String) (defaults to: nil)

    the message detail



72
73
74
# File 'lib/bridgetown-core/log_adapter.rb', line 72

def error(topic, message = nil, &)
  write(:error, topic, message, &)
end

#formatted_topic(topic, colon = false) ⇒ String

Format the topic

Parameters:

  • topic (String)

    e.g. "Configuration file", "Deprecation", etc.

  • colon (Boolean) (defaults to: false)

Returns:

  • (String)

    formatted topic statement



106
107
108
# File 'lib/bridgetown-core/log_adapter.rb', line 106

def formatted_topic(topic, colon = false) # rubocop:disable Style/OptionalBooleanParameter
  "#{topic}#{colon ? ": " : " "}".rjust(20)
end

#info(topic, message = nil) ⇒ Object

Print an informational message

Parameters:

  • topic (String)

    e.g. "Configuration file", "Deprecation", etc.

  • message (String) (defaults to: nil)

    the message detail



56
57
58
# File 'lib/bridgetown-core/log_adapter.rb', line 56

def info(topic, message = nil, &)
  write(:info, topic, message, &)
end

#log_level=(level) ⇒ Object

Set the log level on the writer

Parameters:

  • log_level (Symbol)

    the log level (debug | info | warn | error)



27
28
29
30
31
32
# File 'lib/bridgetown-core/log_adapter.rb', line 27

def log_level=(level)
  writer.level = level if level.is_a?(Integer) && level.between?(0, 3)
  writer.level = LOG_LEVELS[level] ||
    raise(ArgumentError, "unknown log level")
  @level = level
end

#message(topic, message = nil) ⇒ String

Build a topic method

Parameters:

  • topic (String)

    e.g. "Configuration file", "Deprecation", etc.

  • message (String) (defaults to: nil)

    the message detail

Returns:

  • (String)

    the formatted message

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
# File 'lib/bridgetown-core/log_adapter.rb', line 90

def message(topic, message = nil)
  raise ArgumentError, "block or message, not both" if block_given? && message

  message = yield if block_given?
  message = message.to_s.gsub(%r!\s+!, " ")
  topic = formatted_topic(topic, block_given?)
  out = topic + message
  messages << out
  out
end

#warn(topic, message = nil) ⇒ Object

Print a warning message

Parameters:

  • topic (String)

    e.g. "Configuration file", "Deprecation", etc.

  • message (String) (defaults to: nil)

    the message detail



64
65
66
# File 'lib/bridgetown-core/log_adapter.rb', line 64

def warn(topic, message = nil, &)
  write(:warn, topic, message, &)
end

#write(level_of_message, topic, message = nil) ⇒ BasicObject

Log a message. If a block is provided containing the message, use that instead.

Parameters:

  • level_of_message (Symbol)

    the message level (debug | info | warn | error)

  • topic (String)

    e.g. "Configuration file", "Deprecation", etc.

  • message (String) (defaults to: nil)

    the message detail

Returns:

  • (BasicObject)

    false if the message was not written, otherwise returns the value of calling the appropriate writer method, e.g. writer.info.



125
126
127
128
129
# File 'lib/bridgetown-core/log_adapter.rb', line 125

def write(level_of_message, topic, message = nil, &)
  return false unless write_message?(level_of_message)

  writer.public_send(level_of_message, message(topic, message, &))
end

#write_message?(level_of_message) ⇒ Boolean

Check if the message should be written given the log level

Parameters:

  • level_of_message (Symbol)

    the message level (debug | info | warn | error)

Returns:

  • (Boolean)

    whether the message should be written to the log



114
115
116
# File 'lib/bridgetown-core/log_adapter.rb', line 114

def write_message?(level_of_message)
  LOG_LEVELS.fetch(level) <= LOG_LEVELS.fetch(level_of_message)
end