@@ -8,58 +8,105 @@ module Shards
8
8
end
9
9
end
10
10
11
- # Using Crystal < 0.34 logger module
11
+ {% if compare_versions(Crystal ::VERSION , " 0.34.0-0" ) > 0 % }
12
+ require " log"
12
13
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
+ )
14
21
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 ]
38
47
else
39
- message
48
+ message.colorize(color)
40
49
end
41
50
else
42
- io << severity.to_s[ 0 ] << " : " << message
51
+ message
43
52
end
53
+ else
54
+ io << entry.severity.label[0 ] << " : " << message
44
55
end
45
56
end
46
57
end
58
+ {% else % }
59
+ require " logger"
47
60
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
+ }
51
68
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
55
75
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
61
90
end
62
91
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
64
111
end
65
- end
112
+ { % end % }
0 commit comments