Class: EasyLogger::EasyLogger

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

Overview

Class EasyLogger which will do all the job of matching the lines and showing it

Attributes

Use

There’s two methods to use the class: file() and tail(). Both of them use the same parameters (the attributes of the class). The log file need to be correctly formatted. the parser assumes that the log file has this format (with ‘|’ as separator in the example):

|some tag (example: the date and time)|some tag|...|class|method| free text

The Separator could be at the end of the free text but never in the middle so choose it carefully!

File

File will open the stream, parse and output the matching line and exit

Tail

Tail will open the end of the stream and parse and output the matching line and exit

See Also:

Author:

  • Sylvain Desbureaux

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#classesArray (readonly)

the classes to match in the log

Returns:

  • (Array)

    the current value of classes



28
29
30
# File 'lib/easy_logger/easy_logger.rb', line 28

def classes
  @classes
end

#logString (readonly)

the log file/stream that we’ll look into

Returns:

  • (String)

    the current value of log



28
29
30
# File 'lib/easy_logger/easy_logger.rb', line 28

def log
  @log
end

#methodsArray (readonly)

the methods to match in the log

Returns:

  • (Array)

    the current value of methods



28
29
30
# File 'lib/easy_logger/easy_logger.rb', line 28

def methods
  @methods
end

#separatorString (readonly)

the separator of the values in the log. it’s “|” per default

Returns:

  • (String)

    the current value of separator



28
29
30
# File 'lib/easy_logger/easy_logger.rb', line 28

def separator
  @separator
end

#verboseBoolean (readonly)

the verbosity of the output

Returns:

  • (Boolean)

    the current value of verbose



28
29
30
# File 'lib/easy_logger/easy_logger.rb', line 28

def verbose
  @verbose
end

Instance Method Details

#file(log, classes, methods, verbose, separator = '|') ⇒ Object

Display the relevant line of the log matching the classes/methods

Parameters:

  • log (String)

    the log file/stream that we’ll look into

  • classes (String)

    the classes to match in the log

  • methods (String)

    the methods to match in the log

  • verbose (Boolean)

    the verbosity of the output

  • separator (String) (defaults to: '|')

    the separator of the values in the log. it’s “|” per default



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/easy_logger/easy_logger.rb', line 37

def file(log, classes, methods, verbose, separator='|')
  result = String.new
  puts "Looking in #{log} I/O for line matching #{classes} classe(s) and #{methods} method(s) with separator '#{separator}'" if verbose && !classes.nil? && !methods.nil?
  puts "Looking in #{log} I/O for line #{methods} method(s) with separator '#{separator}'" if verbose && classes.nil? && !methods.nil?
  puts "Looking in #{log} I/O for line matching #{classes} classe(s) with separator '#{separator}'" if verbose && !classes.nil? && methods.nil?
  puts "Looking in #{log} I/O for all lines" if verbose && classes.nil? && methods.nil?
  classes =classes.split(',') unless classes.nil?
  methods =methods.split(',') unless methods.nil?
  classes.map!{|c| c.downcase} unless classes.nil?
  methods.map!{|m| m.downcase} unless methods.nil?
  line_number = 0
  line_match = 0
  IO.foreach(log) do |line| 
    line_number += 1
    if match(line,classes,methods, separator)
      puts line 
      line_match += 1
    end
    
  end
  puts "#{line_match} lines matched on the #{line_number} lines in the log" if verbose
end

#match(line, classes, methods, separator) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/easy_logger/easy_logger.rb', line 105

def match(line,classes,methods,separator)
  unless line[0..-2].split(separator).length < 3
    methode = line[0..-2].split(separator)[-2].strip.downcase
    classe = line[0..-2].split(separator)[-3].strip.downcase
  
    (classes.nil? || classes.include?(classe)) && (methods.nil? || methods.include?(methode))
  else
    false
  end
end

#tail(log, classes, methods, verbose, separator = '|') ⇒ Object

Display the relevant <u>new</u> lines of the log matching the classes/methods

Parameters:

  • log (String)

    the log file/stream that we’ll look into

  • classes (String)

    the classes to match in the log

  • methods (String)

    the methods to match in the log

  • verbose (Boolean)

    the verbosity of the output

  • separator (String) (defaults to: '|')

    the separator of the values in the log. it’s “|” per default



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/easy_logger/easy_logger.rb', line 63

def tail(log, classes, methods, verbose, separator='|')
  result = String.new
  puts "Tailing in #{log} I/O for line matching #{classes} classe(s) and #{methods} method(s) with separator '#{separator}'" if verbose && !classes.nil? && !methods.nil?
  puts "Tailing in #{log} I/O for line #{methods} method(s) with separator '#{separator}'" if verbose && classes.nil? && !methods.nil?
  puts "Tailing in #{log} I/O for line matching #{classes} classe(s) with separator '#{separator}'" if verbose && !classes.nil? && methods.nil?
  puts "Tailing in #{log} I/O for all lines" if verbose && classes.nil? && methods.nil?
  classes =classes.split(',') unless classes.nil?
  methods =methods.split(',') unless methods.nil?
  classes.map!{|c| c.downcase} unless classes.nil?
  methods.map!{|m| m.downcase} unless methods.nil?
  @line_number = 0
  @line_match = 0
  #      interrupted = false
  trap("INT") do
    puts "#{@line_match} lines matched on the #{@line_number} lines in the log" if verbose 
    break
  end
  begin
    File::Tail::Logfile.tail(log) do |line|
      @line_number += 1
      if match(line,classes,methods, separator)
        puts line 
        @line_match += 1
      end
      #        if interrupted
      #          break
      #        end
    end
  rescue
    nil
  end
end