lib/legion/logging.rb in legion-logging-0.1.0 vs lib/legion/logging.rb in legion-logging-0.1.1

- old
+ new

@@ -9,18 +9,45 @@ module Logging attr_accessor :logger attr_accessor :color # Self Class class << self - attr_accessor :logger - @logger = ::Logger.new($stdout) + attr_accessor :log, :logger + + def setup(options = {}) + @log = ::Logger.new($stdout) if options[:log_file].nil? + @log = ::Logger.new(options[:log_file]) unless options[:log_file].nil? + log_level(options[:level]) + log_format(options) + + @log + end + + def log_format(_options) + @log.formatter = proc do |severity, datetime, _progname, msg| + "[#{datetime}][#{::Process.pid}] #{severity} #{msg}\n" + end + end + + def log_level(level = 'debug') + if level == 'debug' + @log.level = ::Logger::DEBUG + elsif level == 'info' + @log.level = ::Logger::INFO + elsif level == 'warn' + @log.level = ::Logger::WARN + elsif level == 'error' + @log.level = ::Logger::ERROR + elsif level == 'fatal' + @log.level = ::Logger::FATAL + end + end + + def get(options = {}) + setup(options) + end end - def self.test(message) - start_logger if @logger.nil? - message = Rainbow(message).aliceblue if @color - @logger.debug(message) - end def self.debug(message) start_logger if @logger.nil? message = Rainbow(message).blue if @color @logger.debug(message) @@ -49,23 +76,14 @@ message = Rainbow(message).darkred if @color @logger.fatal(message) end def self.start_logger - daemonize = false - - if daemonize - @color = false - log_location = '/var/log/legion.log' - else - @color = true - log_location = $stdout - end + @color = true + log_location = $stdout @logger = Logger.new(log_location) @logger.level = Logger::DEBUG @logger.formatter = proc do |severity, datetime, _progname, msg| - # default - # SeverityID, [DateTime #pid] SeverityLabel -- ProgName: message "[#{datetime}][#{::Process.pid}] #{severity} #{msg}\n" end end end end