Class: TeLogger::Tlogger

Inherits:
Object
  • Object
show all
Defined in:
lib/teLogger/tlogger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Tlogger

Returns a new instance of Tlogger.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/teLogger/tlogger.rb', line 21

def initialize(*args , &block)
  # default to console
  if args.length == 0
    args << STDOUT
  end

  @opts = {}
  if args[-1].is_a?(Hash)
    @opts = opts
    @opts = { } if @opts.nil?
    args = args[0..-2]
  end

  # allow caller application to create the logger instance instead of internal created
  @logger = @opts[:logger_instance] || Logger.new(*args,&block)
  @disabled = []
  @dHistory = {}
  @include_caller = false
  @tag = nil

  @genable = true
  @exception = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mtd, *args, &block) ⇒ Object

:method: method_missing

This is where the delegation to the Logger object happen or no_method_exception shall be thrown



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/teLogger/tlogger.rb', line 152

def method_missing(mtd, *args, &block)

  if [:debug, :error, :info, :warn].include?(mtd)
    
    # proxy the standard API call for logger
    # Shall print out if the tag is active and vice versa
    
    if args.length > 0 and args[0].is_a?(Symbol)
      tag = args[0]
      args = args[1..-1]
    else
      tag = @tag
    end 
    
    if is_genabled?(tag) and not tag_disabled?(tag) 

      if block
        if not (tag.nil? or tag.empty?) and args.length == 0 
          args = [ format_message(tag) ]
        end

        out = block
      else
        if not (tag.nil? or tag.empty?)
          str = args[0]
          args = [ format_message(tag) ]
          out = Proc.new { str }
        else
          out = block
        end
      end

      @logger.send(mtd, *args, &out)

    end # if not disabled


  elsif [:tdebug, :terror, :tinfo, :twarn].include?(mtd)
    
    # new API that allow caller to include the tag as first parameter and 
    # all subsequent parameters will pass to underlying Logger object
    
    key = args[0]
   
    if is_genabled?(key) and not tag_disabled?(key.to_sym)
      if block
        out = Proc.new { block.call }
        args = [ format_message(key) ]
      else
        str = args[1]
        out = Proc.new { str }
        args = [ format_message(key) ]
      end

      mtd = mtd.to_s[1..-1].to_sym
      @logger.send(mtd, *args, &out)
    end

  elsif [:odebug, :oerror, :oinfo, :owarn].include?(mtd)
    
    # new API that allow caller to include the tag as first parameter and 
    # all subsequent parameters will pass to underlying Logger object
    # however only print the message once for each instance
    
    key = args[0]

    if is_genabled?(key) and not tag_disabled?(key)

      if block
        out = Proc.new { block.call }
        args = [ format_message(key) ]
      else
        str = args[1]
        out = Proc.new { str }
        args = [ format_message(key) ]
      end

      msg = out.call
      if not (msg.nil? or msg.empty?) 
        if not already_shown_or_add(key,msg)
          mtd = mtd.to_s[1..-1].to_sym
          @logger.send(mtd, *args, &out)
        end
      end

    end

  elsif [:ifdebug, :iferror, :ifinfo, :ifwarn].include?(mtd)  
 
    # new API that allow caller to include 
    # the condition as first parameter
    # tag as second parameter
    # Rest of the parameters will be passed to underlying Logger object
    
    cond = args[0]
    key = args[1]

    if cond.is_a?(Proc)
      cond = cond.call
    end

    if is_genabled?(key) and not tag_disabled?(key) and cond

      if block
        out = Proc.new { block.call }
        args = [ format_message(key) ]
      else
        str = args[2]
        out = Proc.new { str }
        args = [ format_message(key) ]
      end

      msg = out.call
      if not (msg.nil? or msg.empty?) 
        mtd = mtd.to_s[2..-1].to_sym
        @logger.send(mtd, *args, &out)
      end

    end

  elsif @logger.respond_to?(mtd)
    
    # rest of the method which not recognized, just passed to underlying Logger object
   
    @logger.send(mtd, *args, &block)

  else
    super
  end
end

Instance Attribute Details

#include_callerObject

include_caller (true/false) tell the logger to print the caller together with the tag



17
18
19
# File 'lib/teLogger/tlogger.rb', line 17

def include_caller
  @include_caller
end

#loggerObject (readonly)

logger it is the actual logger instance of this Tlogger



19
20
21
# File 'lib/teLogger/tlogger.rb', line 19

def logger
  @logger
end

#tagObject

tag is the tag that is being set for this logger session.

One session shall only have one specific tag value, which is the default tag for this logger session.

If multiple tags are required, use the method tdebug, terror, twarn, tinfo or #with_tag block to create a new tag

Note that the tag can be in symbol or string, however shall convert to symbol when processing



15
16
17
# File 'lib/teLogger/tlogger.rb', line 15

def tag
  @tag
end

Instance Method Details

#off_all_tagsObject Also known as: all_tags_off, tags_all_off

:method: off_all_tags

All log messages with tag out of your face!



96
97
98
99
# File 'lib/teLogger/tlogger.rb', line 96

def off_all_tags
  @genable = false
  clear_exceptions
end

#off_all_tags_except(*tags) ⇒ Object Also known as: off_all_except, all_off_except

:method: off_all_tags_except

Turn off all tags EXCEPT the tags given.

Note the parameters can be a list (multiple tags with ‘,’ separator)



124
125
126
127
128
# File 'lib/teLogger/tlogger.rb', line 124

def off_all_tags_except(*tags)
  off_all_tags
  clear_exceptions
  @exception.concat tags.map(&:to_sym)  
end

#off_tag(*tags) ⇒ Object Also known as: tag_off

:method: off_tag

Turn off a tag. After turning off, the log message that tie to this tag shall not be printed out

Do note that all tags by default are turned on.



67
68
69
70
71
72
73
# File 'lib/teLogger/tlogger.rb', line 67

def off_tag(*tags)
  tags.each do |tag|
    if not (tag.nil? or tag.empty? or @disabled.include?(tag))
      @disabled << tag.to_sym
    end
  end
end

#on_all_tagsObject Also known as: all_tags_on, tags_all_on

:method: on_all_tags

All log messages now come down on you! RUN!

No wait, you need to read that before you run away…



110
111
112
113
# File 'lib/teLogger/tlogger.rb', line 110

def on_all_tags
  @genable = true
  clear_exceptions
end

#on_all_tags_except(*tags) ⇒ Object Also known as: on_all_except, all_on_except

:method: on_all_tags_except

Turn on all tags EXCEPT the tags given

Note the parameters can be a list (multiple tags with ‘,’ separator)



139
140
141
142
143
# File 'lib/teLogger/tlogger.rb', line 139

def on_all_tags_except(*tags)
  on_all_tags
  clear_exceptions
  @exception.concat tags.map(&:to_sym)
end

#on_tag(*tags) ⇒ Object Also known as: tag_on

:method: on_tag

Turn on a tag.

Note that by default all tags are turned on. This only affect whatever tags that has been turned off via the method #off_tag. It doesn’t work as adding a tag. Adding a tag message should use tdebug, terror, tinfo, twarn or #with_tag



84
85
86
87
88
# File 'lib/teLogger/tlogger.rb', line 84

def on_tag(*tags)
  tags.each do |tag|
    @disabled.delete(tag.to_sym) if not (tag.nil? or tag.empty?)
  end
end

#show_sourceObject

:method: show_source Helper setting the flag include_caller



300
301
302
# File 'lib/teLogger/tlogger.rb', line 300

def show_source 
  @include_caller = true
end

#tag_disabled?(tag) ⇒ Boolean

:method: tag_disabled?

Check if the tag is disabled

Returns:

  • (Boolean)


288
289
290
291
292
293
294
# File 'lib/teLogger/tlogger.rb', line 288

def tag_disabled?(tag)
  if tag.nil? or tag.empty?
    false
  else
    @disabled.include?(tag.to_sym)
  end
end

#with_tag(tag, &block) ⇒ Object

:method: with_tag

Tag all log inside the block with the given tag value

Useful to tag multiple lines of log under single tag



52
53
54
55
56
57
58
# File 'lib/teLogger/tlogger.rb', line 52

def with_tag(tag,&block)
  if block and not tag.nil?
    log = self.clone
    log.tag = tag
    block.call(log)
  end
end