-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
log_adapter.rb
122 lines (109 loc) · 3.3 KB
/
log_adapter.rb
1
2
3
4
5
6
7
8
9
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# frozen_string_literal: true
module Jekyll
class LogAdapter
attr_reader :writer, :messages
LOG_LEVELS = {
:debug => ::Logger::DEBUG,
:info => ::Logger::INFO,
:warn => ::Logger::WARN,
:error => ::Logger::ERROR,
}.freeze
# Public: Create a new instance of a log writer
#
# writer - Logger compatible instance
# log_level - (optional, symbol) the log level
#
# Returns nothing
def initialize(writer, level = :info)
@messages = []
@writer = writer
self.log_level = level
end
# Public: Set the log level on the writer
#
# level - (symbol) the log level
#
# Returns nothing
def log_level=(level)
writer.level = LOG_LEVELS.fetch(level)
end
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
# Public: Print a debug message
#
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
# message - the message detail
#
# Returns nothing
def debug(topic, message = nil, &block)
writer.debug(message(topic, message, &block))
end
# Public: Print a message
#
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
# message - the message detail
#
# Returns nothing
def info(topic, message = nil, &block)
writer.info(message(topic, message, &block))
end
# Public: Print a message
#
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
# message - the message detail
#
# Returns nothing
def warn(topic, message = nil)
writer.warn(message(topic, message, &block))
end
# Public: Print an error message
#
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
# message - the message detail
#
# Returns nothing
def error(topic, message = nil)
writer.error(message(topic, message, &block))
end
# Public: Print an error message and immediately abort the process
#
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
# message - the message detail (can be omitted)
#
# Returns nothing
def abort_with(topic, message = nil, &block)
error(topic, message, &block)
abort
end
# Internal: Build a topic method
#
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
# message - the message detail
#
# Returns the formatted message
def message(topic, message = nil, &block)
raise ArgumentError, "block or message, not both" if block_given? && message
message = block.call if block_given?
message = message.to_s.gsub(%r!\s+!, " ")
topic = formatted_topic(topic, block_given?)
out = topic + message
messages << out
out
end
# Internal: Format the topic
#
# topic - the topic of the message, e.g. "Configuration file", "Deprecation", etc.
#
# Returns the formatted topic statement
def formatted_topic(topic, colon = false)
"#{topic}#{colon ? ": " : " "}".rjust(20)
end
end
end