lib/adhearsion/logging.rb in adhearsion-1.2.6 vs lib/adhearsion/logging.rb in adhearsion-2.0.0.alpha1
- old
+ new
@@ -1,110 +1,106 @@
-require 'log4r'
+require 'logging'
module Adhearsion
module Logging
- @@logging_level_lock = Mutex.new
+ LOG_LEVELS = %w(TRACE DEBUG INFO WARN ERROR FATAL)
+ METHOD = :logger
+
class << self
+ ::Logging.color_scheme 'bright',
+ :levels => {
+ :info => :green,
+ :warn => :yellow,
+ :error => :red,
+ :fatal => [:white, :on_red]
+ },
+ :date => :blue,
+ :logger => :cyan,
+ :message => :magenta
+
+ def adhearsion_pattern
+ '[%d] %-5l %c: %m\n'
+ end
+
def silence!
self.logging_level = :fatal
end
def unsilence!
self.logging_level = :info
end
- def logging_level=(new_logging_level)
- new_logging_level = Log4r.const_get(new_logging_level.to_s.upcase)
- @@logging_level_lock.synchronize do
- @@logging_level = new_logging_level
- Log4r::Logger.each_logger do |logger|
- logger.level = new_logging_level
- end
- end
+ def reset
+ ::Logging.reset
end
- alias :level= :logging_level=
- def logging_level(level = nil)
- return self.logging_level= level unless level.nil?
- @@logging_level_lock.synchronize do
- return @@logging_level ||= Log4r::INFO
+ def start
+ ::Logging.init LOG_LEVELS
+ ::Logging::Logger[:root].level = :info
+ ::Logging.logger.root.appenders = [::Logging.appenders.stdout('stdout')]
+ self.send :_set_formatter, ::Logging::Layouts.basic(:format_as => :string, :backtrace => true)
+
+ LOG_LEVELS.each do |level|
+ Adhearsion::Logging.const_defined?(level) or Adhearsion::Logging.const_set(level, ::Logging::LEVELS[::Logging.levelify(level)])
end
end
- alias :level :logging_level
- end
- class AdhearsionLogger < Log4r::Logger
+ def logging_level=(new_logging_level)
+ ::Logging::Logger[:root].level = new_logging_level
+ end
- @@outputters = [Log4r::Outputter.stdout]
+ alias :level= :logging_level=
- class << self
- def sanitized_logger_name(name)
- name.to_s.gsub(/\W/, '').downcase
- end
+ def logging_level
+ ::Logging::Logger[:root].level
+ end
- def outputters
- @@outputters
- end
+ def get_logger(logger_name)
+ ::Logging::Logger[logger_name]
+ end
- def outputters=(other)
- @@outputters = other
- end
+ alias :level :logging_level
- def formatters
- @@outputters.map &:formatter
- end
+ def sanitized_logger_name(name)
+ name.to_s.gsub(/\W/, '').downcase
+ end
- def formatters=(other)
- other.each_with_index do |formatter, i|
- outputter = @@outputters[i]
- outputter.formatter = formatter if outputter
- end
- end
+ def outputters=(outputters)
+ ::Logging.logger.root.appenders = outputters
end
- def initialize(*args)
- super
- redefine_outputters
+ alias :appenders= :outputters=
+
+ def outputters
+ ::Logging.logger.root.appenders
end
- def redefine_outputters
- self.outputters = @@outputters
+ alias :appenders :outputters
+
+ def formatter=(formatter)
+ _set_formatter(formatter)
end
- def method_missing(logger_name, *args, &block)
- define_logging_method logger_name, self.class.new(logger_name.to_s)
- send self.class.sanitized_logger_name(logger_name), *args, &block
+ alias :layout= :formatter=
+
+ def formatter
+ ::Logging.logger.root.appenders.first.layout
end
+ alias :layout :formatter
+
private
- def define_logging_method(name, logger)
- # Can't use Module#define_method() because blocks in Ruby 1.8.x can't
- # have their own block arguments.
- self.class.class_eval(<<-CODE, __FILE__, __LINE__)
- def #{self.class.sanitized_logger_name name}(*args, &block)
- logger = Log4r::Logger['#{name}']
- if args.any? || block_given?
- logger.info(*args, &block)
- else
- logger
- end
- end
- CODE
+ def _set_formatter(formatter)
+ ::Logging.logger.root.appenders.each do |appender|
+ appender.layout = formatter
+ end
end
+
end
- DefaultAdhearsionLogger = AdhearsionLogger.new 'ahn'
-
- end
-end
-
-def ahn_log(*args)
- if args.any?
- Adhearsion::Logging::DefaultAdhearsionLogger.info(*args)
- else
- Adhearsion::Logging::DefaultAdhearsionLogger
+ start unless ::Logging.const_defined? :MAX_LEVEL_LENGTH
end
end