Skip to content

Commit

Permalink
Plug logger on common
Browse files Browse the repository at this point in the history
  • Loading branch information
joel committed Apr 14, 2014
1 parent 6c03770 commit 5d7a9d0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 26 deletions.
16 changes: 9 additions & 7 deletions lib/locomotive/common/exception.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
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
end

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
Expand Down
56 changes: 56 additions & 0 deletions lib/locomotive/common/logger.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 0 additions & 19 deletions lib/locomotive/common/plugins/notifier.rb

This file was deleted.

17 changes: 17 additions & 0 deletions lib/plugins/notifier.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5d7a9d0

Please sign in to comment.