Added logfile support
This commit is contained in:
@@ -35,6 +35,7 @@ module Logger
|
||||
|
||||
@@min_level : LOG_LEVEL = LOG_LEVELS["INFO"]
|
||||
@@max_level : LOG_LEVEL = LOG_LEVELS["FATAL"]
|
||||
@@log_file : String = ""
|
||||
@@debug : Bool = false
|
||||
|
||||
LOG_MUTEX = ::Mutex.new
|
||||
@@ -83,6 +84,9 @@ module Logger
|
||||
@@min_level.to_s.downcase
|
||||
end
|
||||
|
||||
def self.log_file=(@@log_file : String)
|
||||
end
|
||||
|
||||
def self.valid?(level : LOG_LEVEL) : Bool
|
||||
level.in?(@@min_level.value..@@max_level.value)
|
||||
end
|
||||
@@ -94,13 +98,21 @@ module Logger
|
||||
Logger.debug "Debug set to #{state}"
|
||||
end
|
||||
|
||||
def self.log(level : String, message : String, *args)
|
||||
def self.log(level : String, message : String, *args) : Nil
|
||||
_level = LOG_LEVELS[level]
|
||||
message = message % args unless args.empty?
|
||||
if valid?(_level) || debug?
|
||||
synchronize {
|
||||
time = Time.local
|
||||
STDERR.puts [time, level.to_s.colorize(_level.color), message].join(" : ")
|
||||
message = [time, level.to_s.colorize(_level.color), message].join(" : ")
|
||||
|
||||
STDERR.puts message
|
||||
|
||||
unless @@log_file.empty?
|
||||
File.open(@@log_file, "a") do |file|
|
||||
file.puts message
|
||||
end
|
||||
end
|
||||
raise "ERROR : Fatal error detected (#{message})" if level == "FATAL"
|
||||
}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user