forked from crystal-lang/shards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request crystal-lang#335 from bcardiff/logger
Use Crystal 0.34 Log module (if possible)
- Loading branch information
Showing
17 changed files
with
134 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,112 @@ | ||
require "logger" | ||
require "colorize" | ||
|
||
module Shards | ||
LOGGER_COLORS = { | ||
"ERROR" => :red, | ||
"WARN" => :light_yellow, | ||
"INFO" => :light_green, | ||
"DEBUG" => :light_gray, | ||
} | ||
|
||
@@colors = true | ||
|
||
def self.colors=(value) | ||
@@colors = value | ||
end | ||
end | ||
|
||
{% if compare_versions(Crystal::VERSION, "0.34.0-0") > 0 %} | ||
require "log" | ||
|
||
Log.setup_from_env( | ||
level: ENV.fetch("CRYSTAL_LOG_LEVEL", "INFO"), | ||
sources: ENV.fetch("CRYSTAL_LOG_SOURCES", "shards.*"), | ||
backend: Log::IOBackend.new.tap do |backend| | ||
backend.formatter = Shards::FORMATTER | ||
end | ||
) | ||
|
||
module Shards | ||
Log = ::Log.for(self) | ||
|
||
def self.set_warning_log_level | ||
Log.level = ::Log::Severity::Warning | ||
end | ||
|
||
@@logger : Logger? | ||
def self.set_debug_log_level | ||
Log.level = ::Log::Severity::Debug | ||
end | ||
|
||
LOGGER_COLORS = { | ||
::Log::Severity::Error => :red, | ||
::Log::Severity::Warning => :light_yellow, | ||
::Log::Severity::Info => :light_green, | ||
::Log::Severity::Debug => :light_gray, | ||
} | ||
|
||
def self.logger | ||
@@logger ||= Logger.new(STDOUT).tap do |logger| | ||
logger.progname = "shards" | ||
logger.level = Logger::Severity::INFO | ||
FORMATTER = ::Log::Formatter.new do |entry, io| | ||
message = entry.message | ||
|
||
logger.formatter = Logger::Formatter.new do |severity, _datetime, _progname, message, io| | ||
if @@colors | ||
io << if color = LOGGER_COLORS[severity.to_s]? | ||
if idx = message.index(' ') | ||
message[0...idx].colorize(color).to_s + message[idx..-1] | ||
if @@colors | ||
io << if color = LOGGER_COLORS[entry.severity]? | ||
if idx = message.index(' ') | ||
message[0...idx].colorize(color).to_s + message[idx..-1] | ||
else | ||
message.colorize(color) | ||
end | ||
else | ||
message | ||
end | ||
else | ||
io << entry.severity.label[0] << ": " << message | ||
end | ||
end | ||
end | ||
{% else %} | ||
require "logger" | ||
|
||
module Shards | ||
LOGGER_COLORS = { | ||
"ERROR" => :red, | ||
"WARN" => :light_yellow, | ||
"INFO" => :light_green, | ||
"DEBUG" => :light_gray, | ||
} | ||
|
||
@@logger : Logger? | ||
|
||
def self.logger | ||
@@logger ||= Logger.new(STDOUT).tap do |logger| | ||
logger.progname = "shards" | ||
logger.level = Logger::Severity::INFO | ||
|
||
logger.formatter = Logger::Formatter.new do |severity, _datetime, _progname, message, io| | ||
if @@colors | ||
io << if color = LOGGER_COLORS[severity.to_s]? | ||
if idx = message.index(' ') | ||
message[0...idx].colorize(color).to_s + message[idx..-1] | ||
else | ||
message.colorize(color) | ||
end | ||
else | ||
message.colorize(color) | ||
message | ||
end | ||
else | ||
message | ||
io << severity.to_s[0] << ": " << message | ||
end | ||
else | ||
io << severity.to_s[0] << ": " << message | ||
end | ||
end | ||
end | ||
|
||
def self.set_warning_log_level | ||
logger.level = Logger::Severity::WARN | ||
end | ||
|
||
def self.set_debug_log_level | ||
logger.level = Logger::Severity::DEBUG | ||
end | ||
|
||
module Log | ||
{% for severity in %w(debug info warn error fatal) %} | ||
def self.{{severity.id}} | ||
Shards.logger.{{severity.id}} do | ||
yield | ||
end | ||
end | ||
{% end %} | ||
end | ||
end | ||
end | ||
{% end %} |
Oops, something went wrong.