Skip to content

Commit 3ab916c

Browse files
committed
Use log module for Crystal 0.34
1 parent 1722212 commit 3ab916c

File tree

1 file changed

+87
-40
lines changed

1 file changed

+87
-40
lines changed

src/logger.cr

Lines changed: 87 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,105 @@ module Shards
88
end
99
end
1010

11-
# Using Crystal < 0.34 logger module
11+
{% if compare_versions(Crystal::VERSION, "0.34.0-0") > 0 %}
12+
require "log"
1213

13-
require "logger"
14+
Log.setup_from_env(
15+
level: ENV.fetch("CRYSTAL_LOG_LEVEL", "INFO"),
16+
sources: ENV.fetch("CRYSTAL_LOG_SOURCES", "shards.*"),
17+
backend: Log::IOBackend.new.tap do |backend|
18+
backend.formatter = Shards::FORMATTER
19+
end
20+
)
1421

15-
module Shards
16-
LOGGER_COLORS = {
17-
"ERROR" => :red,
18-
"WARN" => :light_yellow,
19-
"INFO" => :light_green,
20-
"DEBUG" => :light_gray,
21-
}
22-
23-
@@logger : Logger?
24-
25-
def self.logger
26-
@@logger ||= Logger.new(STDOUT).tap do |logger|
27-
logger.progname = "shards"
28-
logger.level = Logger::Severity::INFO
29-
30-
logger.formatter = Logger::Formatter.new do |severity, _datetime, _progname, message, io|
31-
if @@colors
32-
io << if color = LOGGER_COLORS[severity.to_s]?
33-
if idx = message.index(' ')
34-
message[0...idx].colorize(color).to_s + message[idx..-1]
35-
else
36-
message.colorize(color)
37-
end
22+
module Shards
23+
Log = ::Log.for(self)
24+
25+
def self.set_warning_log_level
26+
Log.level = ::Log::Severity::Warning
27+
end
28+
29+
def self.set_debug_log_level
30+
Log.level = ::Log::Severity::Debug
31+
end
32+
33+
LOGGER_COLORS = {
34+
::Log::Severity::Error => :red,
35+
::Log::Severity::Warning => :light_yellow,
36+
::Log::Severity::Info => :light_green,
37+
::Log::Severity::Debug => :light_gray,
38+
}
39+
40+
FORMATTER = ::Log::Formatter.new do |entry, io|
41+
message = entry.message
42+
43+
if @@colors
44+
io << if color = LOGGER_COLORS[entry.severity]?
45+
if idx = message.index(' ')
46+
message[0...idx].colorize(color).to_s + message[idx..-1]
3847
else
39-
message
48+
message.colorize(color)
4049
end
4150
else
42-
io << severity.to_s[0] << ": " << message
51+
message
4352
end
53+
else
54+
io << entry.severity.label[0] << ": " << message
4455
end
4556
end
4657
end
58+
{% else %}
59+
require "logger"
4760

48-
def self.set_warning_log_level
49-
logger.level = Logger::Severity::WARN
50-
end
61+
module Shards
62+
LOGGER_COLORS = {
63+
"ERROR" => :red,
64+
"WARN" => :light_yellow,
65+
"INFO" => :light_green,
66+
"DEBUG" => :light_gray,
67+
}
5168

52-
def self.set_debug_log_level
53-
logger.level = Logger::Severity::DEBUG
54-
end
69+
@@logger : Logger?
70+
71+
def self.logger
72+
@@logger ||= Logger.new(STDOUT).tap do |logger|
73+
logger.progname = "shards"
74+
logger.level = Logger::Severity::INFO
5575

56-
module Log
57-
{% for severity in %w(debug info warn error fatal) %}
58-
def self.{{severity.id}}
59-
Shards.logger.{{severity.id}} do
60-
yield
76+
logger.formatter = Logger::Formatter.new do |severity, _datetime, _progname, message, io|
77+
if @@colors
78+
io << if color = LOGGER_COLORS[severity.to_s]?
79+
if idx = message.index(' ')
80+
message[0...idx].colorize(color).to_s + message[idx..-1]
81+
else
82+
message.colorize(color)
83+
end
84+
else
85+
message
86+
end
87+
else
88+
io << severity.to_s[0] << ": " << message
89+
end
6190
end
6291
end
63-
{% end %}
92+
end
93+
94+
def self.set_warning_log_level
95+
logger.level = Logger::Severity::WARN
96+
end
97+
98+
def self.set_debug_log_level
99+
logger.level = Logger::Severity::DEBUG
100+
end
101+
102+
module Log
103+
{% for severity in %w(debug info warn error fatal) %}
104+
def self.{{severity.id}}
105+
Shards.logger.{{severity.id}} do
106+
yield
107+
end
108+
end
109+
{% end %}
110+
end
64111
end
65-
end
112+
{% end %}

0 commit comments

Comments
 (0)