From 5d7a9d0b62e2f556d2f53fc0aed010f07ce4ef27 Mon Sep 17 00:00:00 2001 From: Joel AZEMAR Date: Mon, 14 Apr 2014 10:24:15 +0200 Subject: [PATCH] Plug logger on common --- lib/locomotive/common/exception.rb | 16 ++++--- lib/locomotive/common/logger.rb | 56 +++++++++++++++++++++++ lib/locomotive/common/plugins/notifier.rb | 19 -------- lib/plugins/notifier.rb | 17 +++++++ 4 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 lib/locomotive/common/logger.rb delete mode 100644 lib/locomotive/common/plugins/notifier.rb create mode 100644 lib/plugins/notifier.rb diff --git a/lib/locomotive/common/exception.rb b/lib/locomotive/common/exception.rb index 0bb9c82..d610819 100644 --- a/lib/locomotive/common/exception.rb +++ b/lib/locomotive/common/exception.rb @@ -1,17 +1,19 @@ module Locomotive module Common - class Notifier - def method_missing method, *args - nil - end - end + # class Notifier + # + # def method_missing method, *args + # nil + # end + # end class DefaultException < ::Exception attr_accessor :notifier def initialize(message = nil, parent_exception = nil) - self.notifier = Locomotive::Common::Notifier.new + # self.notifier = Locomotive::Common::Notifier.new + self.notifier = Locomotive::Common::Logger.setup('log/locomotivecms.log') self.log_backtrace(parent_exception) if parent_exception super(message) init_plugins @@ -19,7 +21,7 @@ def initialize(message = nil, parent_exception = nil) def init_plugins @plugins = [] - Locomotive::Common::Plugins.constants.each do |name| + ::Plugins.constants.each do |name| @plugins << ::Plugins.const_get(name).new(self) end end diff --git a/lib/locomotive/common/logger.rb b/lib/locomotive/common/logger.rb new file mode 100644 index 0000000..57d3d3f --- /dev/null +++ b/lib/locomotive/common/logger.rb @@ -0,0 +1,56 @@ +module Locomotive + module Common + + class Logger + + attr_accessor :logger + + def initialize + self.logger = nil + end + + # Setup the single instance of the ruby logger. + # + # @param [ String ] path The path to the log file (default: log/locomotivecms.log) + # @param [ Boolean ] stdout Instead of having a file, log to the standard output + # + def setup path + require 'logger' + + out = path ? logfile_path(path) : STDOUT + + self.logger = ::Logger.new(out).tap do |log| + log.level = ::Logger::DEBUG + log.formatter = proc do |severity, datetime, progname, msg| + "#{msg}\n" + end + end + end + + def self.instance + @@instance ||= self.new + end + + def self.setup path=nil + self.instance.setup path + end + + class << self + %w(debug info warn error fatal unknown).each do |name| + define_method(name) do |message| + self.instance.logger.send(name.to_sym, message) + end + end + end + + private + + def logfile_path path + File.expand_path(File.join(path)).tap do |_path| + FileUtils.mkdir_p(File.dirname(_path)) + end + end + + end + end +end \ No newline at end of file diff --git a/lib/locomotive/common/plugins/notifier.rb b/lib/locomotive/common/plugins/notifier.rb deleted file mode 100644 index 57b4bd2..0000000 --- a/lib/locomotive/common/plugins/notifier.rb +++ /dev/null @@ -1,19 +0,0 @@ -module Locomotive - module Common - module Plugins - - class Notifier - def initialize(exception) - exception.notifier.extend(SilentLogger) - end - - module SilentLogger - def fatal(*) - nil # Locomotive::Steam::Logger.fatal args.first - end - end - end - - end - end -end \ No newline at end of file diff --git a/lib/plugins/notifier.rb b/lib/plugins/notifier.rb new file mode 100644 index 0000000..49fb116 --- /dev/null +++ b/lib/plugins/notifier.rb @@ -0,0 +1,17 @@ +# # Sample +# +# module Plugins +# +# class Notifier +# def initialize receiver +# receiver.notifier.extend(SilentLogger) +# end +# +# module SilentLogger +# def fatal(*) +# nil +# end +# end +# end +# +# end