Module: Alog

Defined in:
lib/alog.rb,
lib/alog/version.rb

Defined Under Namespace

Classes: AOlogger, AlogException, Alogger

Constant Summary collapse

LogTag =

allow application to provide which tag should print out

[:global]
LogFacts =

allow application to configure multiple logging log factories configurations

{}
GLog =

multi logger created from LogFacts entry given by application

{}
CondLog =

Actual logic of detecting a tag should be activated and on which logger should it written to

Proc.new do |msg, params = {}, &block|
  key = params[:key] || :global
  type = params[:type] || :debug
  activeTag = params[:active_tag] || LogTag
  #if defined?(:LogTag) and LogTag.is_a?(Array) and (LogTag.include?(key) or LogTag.include?(:all)) or type == :error
  if (activeTag.include?(key) or activeTag.include?(:all)) or type == :error
    logEng = params[:logEng]
    if logEng == nil or (logEng != nil and logEng.empty?)
      logEng = (LogFacts.length > 0 ? [LogFacts.keys[0]] : [:default])
    end
    
    logEng = [logEng] if not logEng.is_a?(Array)

    # allow written to multiple logger
    logEng.each do |e|
      
      if GLog[e] == nil

        lp = LogFacts[e]
        if lp == nil
          # default if empty
          lp = [STDOUT]
        end

        # ensure the same configuration only created a logger object once
        GLog[e] = Alogger.new(lp)

      end
     
      4.downto(0).each do |i|
        c = caller[i]
        next if c == nil
        @caller = c
        break
      end
      GLog[e].log("#{caller.length > 3 ? "[#{File.basename(@caller)}]" : ""} [#{key}] #{msg}", type, &block)
    end
    
  end
  
end
VERSION =
"1.0.4"

Instance Method Summary collapse

Instance Method Details

#add_log_fact(key, conf) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/alog.rb', line 19

def add_log_fact(key, conf)
  # add new log fact to global if not defined
  if LogFacts.include?(key)
  else
    LogFacts[key] = conf
  end
end

#clog(msg, ltype = :debug, key = :global, logEng = []) ⇒ Object

Module level clog() method Meant to be called by application INSIDE the l()‘s block



165
166
167
168
169
# File 'lib/alog.rb', line 165

def clog(msg, ltype = :debug, key = :global, logEng = [])
  log(msg, { type: @lType != ltype ? ltype : @lType, 
             key: (key != @lKey ? key : @lKey),
             logEng: @llEng  })
end

#clog_contextObject



157
158
159
# File 'lib/alog.rb', line 157

def clog_context
  { key: @lKey, type: @lType, engine: @llEng }
end

#l(key = :global, params = { type: :debug, logEng: [] }, &block) ⇒ Object

Provide a block construct that can set values consistantly for multiple clog() call TODO How to make this thread safe?



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/alog.rb', line 143

def l(key = :global, params = { type: :debug, logEng: [] } ,&block)
  # this construct try to make the variable private to the block
  # Still not sure will error condition exist for multi threaded application
  b = Proc.new do |key, params, &block|
    @lKey = key 
    @lType = params[:type]
    @llEng = params[:logEng]
    if block
      block.call
    end
  end
  b.call(key, params, &block)
end

#log(msg, params = { }, &block) ⇒ Object

provide module level method to write to the logger object



222
223
224
# File 'lib/alog.rb', line 222

def log(msg, params = { }, &block)
  CondLog.call(msg, params, &block)
end

#selected_tags_onlyObject



135
136
137
# File 'lib/alog.rb', line 135

def selected_tags_only
  LogTag.delete(:all)
end

#show_all_tagsObject

end class AloggerObject



131
132
133
# File 'lib/alog.rb', line 131

def show_all_tags
  LogTag << :all
end