2015-11-04 10:48:51 +01:00
|
|
|
# a simple module to be included into a class that want to log
|
|
|
|
#
|
|
|
|
# use the standard logger and the class name as the program name
|
|
|
|
#
|
|
|
|
# The standard functions are available on the log object
|
|
|
|
# And the log level may be set after inclusion with level function (that takes symbols)
|
|
|
|
|
|
|
|
require "logger"
|
|
|
|
|
|
|
|
module Logging
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(Methods)
|
|
|
|
end
|
|
|
|
def log
|
|
|
|
self.class.log
|
|
|
|
end
|
|
|
|
|
|
|
|
module Methods
|
|
|
|
def log
|
|
|
|
return @logger if @logger
|
2016-12-06 15:44:04 +01:00
|
|
|
@logger = Logger.new log_stream
|
2015-11-04 10:48:51 +01:00
|
|
|
@logger.progname = self.name.split("::").last
|
|
|
|
@logger.datetime_format = '%M:%S'
|
|
|
|
@logger.level = Logger::INFO
|
|
|
|
@logger
|
|
|
|
end
|
|
|
|
def log_level l
|
|
|
|
log.level = case l
|
|
|
|
when :unknown
|
|
|
|
Logger::UNKNOWN
|
|
|
|
when :fatal
|
|
|
|
Logger::FATAL
|
|
|
|
when :error
|
|
|
|
Logger::ERROR
|
|
|
|
when :warn
|
|
|
|
Logger::WARN
|
|
|
|
when :info
|
|
|
|
Logger::INFO
|
|
|
|
when :debug
|
|
|
|
Logger::DEBUG
|
|
|
|
else
|
|
|
|
raise "unknown log level #{l}"
|
|
|
|
end
|
|
|
|
end
|
2016-12-06 15:44:04 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
def log_stream
|
|
|
|
STDOUT
|
|
|
|
end
|
2015-11-04 10:48:51 +01:00
|
|
|
end
|
|
|
|
end
|