lib/tlogger.rb in tlogger-0.4 vs lib/tlogger.rb in tlogger-0.5.0

- old
+ new

@@ -12,19 +12,27 @@ # # Singleton object to facilitate tag management # class TloggerConf include Singleton - attr_reader :active_tags, :scoped_tag, :disabled_tags, :auto_tag + attr_reader :active_tags, :scoped_tag, :blacklisted_tags, :auto_tag #GLOBAL_TAG = :global GLOBAL_TAG = "" + INT_TAG = :tlogger def initialize @active_tags = [] - @disabled_tags = [] + # tag added to black listed will not be printed out even the tag is added later in the code path + @blacklisted_tags = [] @disable_all_tags = false @auto_tag = false + # todo + # allow to redirect certain tag to specific output. tag: [<config>] + @output_map = {} + # if output_map is there, then there should be pre created log file with specific output defined in the map. [<config>] => log_instance + @output_log = {} + # end todo end def auto_tag_on @auto_tag = true end @@ -43,15 +51,15 @@ def deactivate_tag(tag) @active_tags.delete(tag) end - def disable_tag(tag) + def blacklist_tag(tag) if tag.is_a?(Array) - @disabled_tags += tag + @blacklisted_tags += tag else - @disabled_tags << tag + @blacklisted_tags << tag end end def all_tags_off @disable_all_tags = true @@ -59,36 +67,36 @@ def all_tags_on @disable_all_tags = false end - def enable_tag(tag) + def whitelist_tag(tag) if tag.is_a?(Array) tag.each do |t| - @disabled_tags.delete(t) + @blacklisted_tags.delete(t) end else - @disabled_tags.delete(tag) + @blacklisted_tags.delete(tag) end end - + + def whitelist_all_tags + @disabled_tags.clear + end + def is_tag_active?(tag) if @disable_all_tags false else - @active_tags.include?(tag) and not @disabled_tags.include?(tag) + @active_tags.include?(tag) #and not @disabled_tags.include?(tag) end end def remove_all_active_tags @active_tags.clear end - def remove_all_disabled_tags - @disabled_tags.clear - end - def set_scoped_tag(tag) @scoped_tag = tag end def clear_scoped_tag @@ -102,11 +110,12 @@ not @scoped_tag.nil? and not @scoped_tag.empty? end end def is_scoped_tag_active? - not @disabled_tags.include?(@scoped_tag) + #@active_tags.include?(@scoped_tag) + not @blacklisted_tags.include?(@scoped_tag) end def self.method_missing(mtd,*args,&block) if TloggerConf.instance.respond_to?(mtd) TloggerConf.instance.send(mtd,*args,&block) @@ -131,10 +140,12 @@ if args.length == 0 args << STDOUT end @tlogger = Logger.new(*args) @tag = TloggerConf::GLOBAL_TAG + # disable by default + # If there is issue then enable it back in application level self end def tag=(val) @tag = val @@ -142,79 +153,89 @@ self end def tdebug(tag,*args) with_tag(tag) do - debug(*args) + self.debug(*args) end self end def terror(tag, *args) with_tag(tag) do - error(*args) + self.error(*args) end self end def tinfo(tag, *args) with_tag(tag) do - info(*args) + self.info(*args) end self end def twarn(tag, *args) with_tag(tag) do - warn(*args) + self.warn(*args) end self end + def intDebug(msg) + #puts TloggerConf.active_tags + if TloggerConf.instance.is_tag_active?(TloggerConf::INT_TAG) + msg2 = "[#{TloggerConf::INT_TAG}] #{msg}" + #puts "intDebug" + @tlogger.debug(msg2) + end + end + def method_missing(mtd,*args,&block) - @tlogger.debug "[tlogger] method_missing: Method #{mtd}" + #@tlogger.debug "[tlogger] method_missing: Method #{mtd}" + intDebug("method_missing: #{mtd}") if @tlogger.respond_to?(mtd) - if TloggerConf.is_tag_active?(@tag) or TloggerConf.has_scoped_tag? - if PROXY_MTD.include?(mtd) - # All messages should be tagged under this section... + + if PROXY_MTD.include?(mtd) + + if @tag.nil? or @tag.empty? + # no tag. Output like normal log + @tlogger.send(mtd, *args, &block) + + else + if TloggerConf.has_scoped_tag? if TloggerConf.is_scoped_tag_active? + intDebug("Scoped tag detected") args[0] = "[#{TloggerConf.scoped_tag}] #{args[0]}" @tlogger.send(mtd,*args,&block) end - else - if not @tag.nil? and not @tag.empty? - if @tag == TloggerConf::GLOBAL_TAG - if TloggerConf.is_auto_tag_on? - args = tag_class(*args) - @tlogger.send(mtd,*args,&block) - end - else - args[0] = "[#{@tag}] #{args[0]}" - @tlogger.send(mtd,*args,&block) - end - elsif TloggerConf.is_auto_tag_on? - args = tag_class(*args) - @tlogger.send(mtd,*args,&block) - end + elsif TloggerConf.is_auto_tag_on? + intDebug("auto_tag is on...") + args = tag_class(*args) + @tlogger.send(mtd,*args,&block) + elsif TloggerConf.is_tag_active?(@tag) + intDebug("Tagged output...") + args[0] = "[#{@tag}] #{args[0]}" + @tlogger.send(mtd,*args,&block) end - - else - @tlogger.send(mtd,*args,&block) + end - # if in proxy method list - - #@tlogger.send(mtd,*args,&block) - elsif TloggerConf.is_auto_tag_on? - args = tag_class(*args) + else + intDebug("Not proxy method for logger. Pass to logger to handle. (#{mtd})") + ## not the debug, info, warn and error method, no need change message @tlogger.send(mtd, *args, &block) end - elsif TloggerConf.respond_to?(mtd) + + + elsif TloggerConf.instance.respond_to?(mtd) # redirect the config method to make it consistent API + intDebug("Redirect to TloggerConf for consistancy (#{mtd})") TloggerConf.send(mtd, *args, &block) else + intDebug("Call method_missing parent to handle method '#{mtd}'") super end end # end method_missing # @@ -227,10 +248,10 @@ break end if not @cal.nil? and not @cal.empty? wd = Dir.getwd - @scal = @cal[wd.length+1..-1] + @scal = @cal[wd.length..-1] args[0] = "[#{@scal}] #{args[0]}" end args #args[0] = "[#{@cal}] #{args[0]}"