Class: Bridgetown::LogAdapter

Inherits:
Object
  • Object
show all
Defined in:
bridgetown-core/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, prefix: [nil, nil]) ⇒ LogAdapter

Create a new instance of a log writer

Parameters:

  • writer (Logger)

    compatible instance

  • log_level (Symbol)
    the log level (debug info warn error)
  • prefix (Array) (defaults to: [nil, nil])

    an optional prefix — the first element is the string, and the second is the color



20
21
22
23
24
25
26
27
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 20

def initialize(writer, level = :info, prefix: [nil, nil])
  @messages = []
  @writer = writer
  @prefix = prefix

  self.log_level = level
  set_prefix
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



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

def level
  @level
end

#messagesObject (readonly)

Returns the value of attribute messages.



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

def messages
  @messages
end

#writerObject (readonly)

Returns the value of attribute writer.



5
6
7
# File 'bridgetown-core/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



85
86
87
88
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 85

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

#adjust_verbosity(**options) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 39

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



53
54
55
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 53

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



77
78
79
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 77

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



111
112
113
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 111

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



61
62
63
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 61

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)


32
33
34
35
36
37
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 32

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)


95
96
97
98
99
100
101
102
103
104
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 95

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

#remove_prefixObject



136
137
138
139
140
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 136

def remove_prefix
  @prefix = [nil, nil]
  writer.remove_prefix \
    if writer.respond_to?(:remove_prefix)
end

#set_prefix(prefix = , color: ) ⇒ Object



142
143
144
145
146
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 142

def set_prefix(prefix = @prefix[0], color: @prefix[1])
  if writer.respond_to?(:set_prefix) && prefix && color # rubocop:disable Style/GuardClause
    writer.set_prefix(prefix, color: color)
  end
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



69
70
71
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 69

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.



130
131
132
133
134
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 130

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



119
120
121
# File 'bridgetown-core/lib/bridgetown-core/log_adapter.rb', line 119

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