Skip to content

Commit

Permalink
Refactoring logging to support external loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
gcao committed Oct 9, 2012
1 parent 7f7d930 commit c70855b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 100 deletions.
1 change: 1 addition & 0 deletions lib/aspector.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'aspector/logging'
require 'aspector/logger'

require 'aspector/object_extension'
Expand Down
66 changes: 35 additions & 31 deletions lib/aspector/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ def disabled?
end

def aop_logger
@aop_logger ||= Logger.new(self, self.class.logger.level)
return @aop_logger if @aop_logger

@aop_logger = Logging.get_logger(self)
@aop_logger.level = self.class.logger.level
@aop_logger
end
alias logger aop_logger

Expand Down Expand Up @@ -120,7 +124,7 @@ def aop_apply_to_method method, advices, scope = nil
advices = aop_filter_advices advices, method
return if advices.empty?

aop_logger.log Logger::APPLY_TO_METHOD, method
aop_logger.log Logging::DEBUG, 'apply-to-method', method
before_apply_to_method method, advices

scope ||=
Expand Down Expand Up @@ -255,7 +259,7 @@ def aop_recreate_method method, advices, scope
rescue
# ignore undefined method error
if @aop_options[:old_methods_only]
aop_logger.log Logger::METHOD_NOT_FOUND, method
aop_logger.log Logging::WARN, 'method-not-found', method
end

return
Expand All @@ -281,7 +285,7 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
aspect = self

code = METHOD_TEMPLATE.result(binding)
aspect.aop_logger.log Logger::GENERATE_CODE, method, code
aspect.aop_logger.log Logging::DEBUG, 'generate-code', method, code
@aop_context.class_eval code, __FILE__, __LINE__ + 4
end

Expand All @@ -293,13 +297,13 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
% end
define_method :<%= method %> do |*args, &block|
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::ENTER_GENERATED_METHOD
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'enter-generated-method'
% end
if aspect.aop_disabled?
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::EXIT_BECAUSE_DISABLED
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'exit--generated-method'
% end
return orig_method.bind(self).call(*args, &block)
end
Expand All @@ -312,20 +316,20 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
# Before advices
% end
% before_advices.each do |advice|
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_INVOKE_ADVICE, '<advice <%= advice.index %>>'
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-advice', '<advice <%= advice.index %>>'
% end
result = <%= advice.with_method %> <%
if advice.options[:aspect_arg] %>aspect, <% end %><%
if advice.options[:method_arg] %>'<%= method %>', <% end
%>*args
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_INVOKE_ADVICE, '<advice <%= advice.index %>>'
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--invoke-advice', '<advice <%= advice.index %>>'
% end
% if advice.options[:skip_if_false]
unless result
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::EXIT_FROM_BEFORE_FILTER, '<advice <%= advice.index %>>'
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'exit-method-due-to-before-filter', '<advice <%= advice.index %>>'
% end
return
end
Expand All @@ -334,14 +338,14 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
% if around_advice
# Around advice
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_INVOKE_ADVICE, '<advice <%= around_advice.index %>>'
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-advice', '<advice <%= around_advice.index %>>'
% end
% if aop_logger.visible?(Logger::TRACE)
% if aop_logger.visible?(Logging::TRACE)
proxy = lambda do |*args, &block|
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_INVOKE_PROXY
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-proxy'
res = wrapped_method.bind(self).call *args, &block
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_INVOKE_PROXY
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--invoke-proxy'
res
end
result = <%= around_advice.with_method %> <%
Expand All @@ -354,28 +358,28 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
if around_advice.options[:method_arg] %>'<%= method %>', <% end
%>wrapped_method.bind(self), *args, &block
% end
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_INVOKE_ADVICE, '<advice <%= around_advice.index %>>'
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--invoke-advice', '<advice <%= around_advice.index %>>'
% end
% else
# Invoke original method
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_WRAPPED_METHOD
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-wrapped-method'
% end
result = orig_method.bind(self).call *args, &block
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_WRAPPED_METHOD
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--wrapped-method'
% end
% end
% unless after_advices.empty?
# After advices
% after_advices.each do |advice|
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::BEFORE_INVOKE_ADVICE, '<advice <%= advice.index %>>'
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'before-invoke-advice', '<advice <%= advice.index %>>'
% end
% if advice.options[:result_arg]
result = <%= advice.with_method %> <%
Expand All @@ -389,8 +393,8 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
if advice.options[:method_arg] %>'<%= method %>', <% end
%>*args
% end
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::AFTER_INVOKE_ADVICE, '<advice <%= advice.index %>>'
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'after--invoke-advice', '<advice <%= advice.index %>>'
% end
% end
% end
Expand All @@ -401,8 +405,8 @@ def aop_recreate_method_with_advices method, before_advices, after_advices, arou
end # end of catch
% end
% if aop_logger.visible?(Logger::TRACE)
aspect.aop_logger.log_method_call '<%= method %>', Aspector::Logger::EXIT_GENERATED_METHOD
% if aop_logger.visible?(Logging::TRACE)
aspect.aop_logger.log <%= Logging::TRACE %>, '<%= method %>', 'exit--generated-method'
% end
result
end
Expand Down
26 changes: 17 additions & 9 deletions lib/aspector/base_class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ClassMethods
::Aspector::Base.extend(self)

def aop_enable
aop_logger.log Logger::ENABLE_ASPECT
aop_logger.log Logging::INFO, 'enable-aspect'
send :define_method, :aop_disabled? do
end

Expand All @@ -13,7 +13,7 @@ def aop_enable
alias enable aop_enable

def aop_disable
aop_logger.log Logger::DISABLE_ASPECT
aop_logger.log Logging::INFO, 'disable-aspect'
send :define_method, :aop_disabled? do
true
end
Expand All @@ -22,11 +22,19 @@ def aop_disable
end
alias disable aop_disable

# if ENV["ASPECTOR_LOGGER"] is set, use it
# else try to load logem and use Logem::Logger
# else use built in logger
def aop_logger
@aop_logger ||= Logger.new(self)
@aop_logger ||= Logging.get_logger(self)
end
alias logger aop_logger

def aop_logger= logger
@aop_logger = logger
end
alias logger= aop_logger=

def aop_advices
@aop_advices ||= []
end
Expand All @@ -42,7 +50,7 @@ def aop_apply target, *rest

targets = rest.unshift target
result = targets.map do |target|
aop_logger.log Logger::APPLY, target, options.inspect
aop_logger.log Logging::INFO, 'apply', target, options.inspect
aspect_instance = new(target, options)
aspect_instance.send :aop_apply
aspect_instance
Expand All @@ -64,39 +72,39 @@ def aop_default options
def aop_before *methods, &block
aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::BEFORE, self, methods, &block)
advice.index = aop_advices.size
aop_logger.log Logger::DEFINE_ADVICE, advice
aop_logger.log Logging::INFO, 'define-advice', advice
advice
end
alias before aop_before

def aop_before_filter *methods, &block
aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::BEFORE_FILTER, self, methods, &block)
advice.index = aop_advices.size
aop_logger.log Logger::DEFINE_ADVICE, advice
aop_logger.log Logging::INFO, 'define-advice', advice
advice
end
alias before_filter aop_before_filter

def aop_after *methods, &block
aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::AFTER, self, methods, &block)
advice.index = aop_advices.size
aop_logger.log Logger::DEFINE_ADVICE, advice
aop_logger.log Logging::INFO, 'define-advice', advice
advice
end
alias after aop_after

def aop_around *methods, &block
aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::AROUND, self, methods, &block)
advice.index = aop_advices.size
aop_logger.log Logger::DEFINE_ADVICE, advice
aop_logger.log Logging::INFO, 'define-advice', advice
advice
end
alias around aop_around

def aop_raw *methods, &block
aop_advices << advice = aop_create_advice(Aspector::AdviceMetadata::RAW, self, methods, &block)
advice.index = aop_advices.size
aop_logger.log Logger::DEFINE_ADVICE, advice
aop_logger.log Logging::INFO, 'define-advice', advice
advice
end
alias raw aop_raw
Expand Down
76 changes: 16 additions & 60 deletions lib/aspector/logger.rb
Original file line number Diff line number Diff line change
@@ -1,67 +1,23 @@
module Aspector
class Logger
# Log levels
ERROR = 50
WARN = 40
INFO = 30
DEBUG = 20
TRACE = 10

DEFAULT_VISIBLE_LEVEL = INFO

# Actions
DEFINE_ADVICE = ["define-advice" , INFO]
APPLY = ["apply" , INFO]
APPLY_TO_METHOD = ["apply-to-method", DEBUG]
ENABLE_ASPECT = ["enable-aspect" , INFO]
DISABLE_ASPECT = ["disable-aspect" , INFO]
GENERATE_CODE = ["generate-code" , DEBUG]

ENTER_GENERATED_METHOD = ["enter-generated-method", TRACE]
EXIT_GENERATED_METHOD = ["exit--generated-method", TRACE]
EXIT_BECAUSE_DISABLED = ["exit--because-disabled", TRACE]
BEFORE_INVOKE_ADVICE = ["before-invoke-advice" , TRACE]
AFTER_INVOKE_ADVICE = ["after--invoke-advice" , TRACE]
BEFORE_WRAPPED_METHOD = ["before-wrapped-method" , TRACE]
AFTER_WRAPPED_METHOD = ["after--wrapped-method" , TRACE]
BEFORE_INVOKE_PROXY = ["before-invoke-proxy" , TRACE]
AFTER_INVOKE_PROXY = ["after--invoke-proxy" , TRACE]

# Unexpected behaviors
METHOD_NOT_FOUND = ["method-not-found" , WARN]

attr_reader :context
attr_writer :level
attr_accessor :level

def initialize context, level = nil
def initialize context
@context = context
@level = level
end

def level
return @level if @level

if (level_string = ENV['ASPECTOR_LOG_LEVEL'])
@level = string_to_level(level_string)
else
@level = DEFAULT_VISIBLE_LEVEL
@level = Logging::DEFAULT_VISIBLE_LEVEL
end
end

def log action_level, *args
action, level = *action_level

return if self.level > level

puts log_prefix(level) << action << " | " << args.join(" | ")
end

def log_method_call method, action_level, *args
action, level = *action_level

def log level, *args
return if self.level > level

puts log_prefix(level) << method << " | " << action << " | " << args.join(" | ")
puts log_prefix(level) << args.join(" | ")
end

def visible? level
Expand All @@ -81,24 +37,24 @@ def log_prefix level

def level_to_string level
case level
when ERROR then "ERROR"
when WARN then "WARN "
when INFO then "INFO "
when DEBUG then "DEBUG"
when TRACE then "TRACE"
when Logging::ERROR then "ERROR"
when Logging::WARN then "WARN "
when Logging::INFO then "INFO "
when Logging::DEBUG then "DEBUG"
when Logging::TRACE then "TRACE"
else level.to_s
end
end

def string_to_level level_string
return DEFAULT_VISIBLE_LEVEL if level_string.nil? or level_string.strip == ''
return Logging::DEFAULT_VISIBLE_LEVEL if level_string.nil? or level_string.strip == ''

case level_string.downcase
when 'error' then ERROR
when 'warn' then WARN
when 'info' then INFO
when 'debug' then DEBUG
when 'trace' then TRACE
when 'error' then Logging::ERROR
when 'warn' then Logging::WARN
when 'info' then Logging::INFO
when 'debug' then Logging::DEBUG
when 'trace' then Logging::TRACE
end
end
end
Expand Down
Loading

0 comments on commit c70855b

Please sign in to comment.