lib/locomotive/common/logger.rb in locomotivecms_common-0.4.0 vs lib/locomotive/common/logger.rb in locomotivecms_common-0.5.0
- old
+ new
@@ -1,12 +1,12 @@
+# frozen_string_literal: true
+
require 'colorize'
module Locomotive
module Common
-
class Logger
-
attr_accessor :logger
def initialize
self.logger = nil
end
@@ -17,57 +17,53 @@
# Sample /home/locomotivecms/log/server.log (default: nil => Stdout)
#
def setup(log_file_full_path = nil)
require 'logger'
- output = begin
- if log_file_full_path
- log_file_path log_file_full_path
- else
- STDOUT
- end
- end
+ output = if log_file_full_path
+ log_file_path log_file_full_path
+ else
+ $stdout
+ end
self.logger = ::Logger.new(output).tap do |log|
log.level = ::Logger::DEBUG
- log.formatter = proc do |severity, datetime, progname, msg|
+ log.formatter = proc do |_severity, _datetime, _progname, msg|
"#{msg}\n"
end
end
end
def self.instance
- @@instance ||= self.new
+ @@instance ||= new
end
def self.setup(*args)
if args.size > 1
- puts "[DEPRECATION] Logger.setup(path, stdout=false) is deprecated. " \
- "Please use Logger.setup(log_file_full_path) instead, " \
- "like: /home/locomotivecms/log/server.log"
+ puts '[DEPRECATION] Logger.setup(path, stdout=false) is deprecated. ' \
+ 'Please use Logger.setup(log_file_full_path) instead, ' \
+ 'like: /home/locomotivecms/log/server.log'
end
- self.instance.setup args.first
+ instance.setup args.first
end
def self.close
- self.instance.logger.close
+ instance.logger.close
end
class << self
- %w(debug info warn error fatal unknown).each do |name|
+ %w[debug info warn error fatal unknown].each do |name|
define_method(name) do |message|
- if self.instance.logger
- self.instance.logger.send(name.to_sym, message)
- end
+ instance.logger&.send(name.to_sym, message)
end
end
end
private
def log_file_path(log_file_full_path)
if File.directory? log_file_full_path
- puts "[DEPRECATION] Please use fully log file path like: /home/locomotivecms/log/server.log"
+ puts '[DEPRECATION] Please use fully log file path like: /home/locomotivecms/log/server.log'
File.expand_path(File.join(log_file_full_path, 'log', 'locomotivecms.log'))
else
log_file_full_path
end.tap { |path| FileUtils.mkdir_p(File.dirname(path)) }
end