@author Sylvain Desbureaux
@attr_reader [String] log the log file/stream that we’ll look into @attr_reader [Array] classes the classes to match in the log @attr_reader [Array] methods the methods to match in the log @attr_reader [Boolean] verbose the verbosity of the output @attr_reader [String] separator the separator of the values in the log. it’s “|” per default
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! @see Logging a working logger example
File will open the stream, parse and output the matching line and exit
Tail will open the end of the stream and parse and output the matching line and exit
Display the relevant line of the log matching the classes/methods
@param [String] log the log file/stream that we’ll look into @param [String] classes the classes to match in the log @param [String] methods the methods to match in the log @param [Boolean] verbose the verbosity of the output @param [String] separator the separator of the values in the log. it’s “|” per default
# File lib/easy_logger/easy_logger.rb, line 37 37: def file(log, classes, methods, verbose, separator='|') 38: result = String.new 39: 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? 40: puts "Looking in #{log} I/O for line #{methods} method(s) with separator '#{separator}'" if verbose && classes.nil? && !methods.nil? 41: puts "Looking in #{log} I/O for line matching #{classes} classe(s) with separator '#{separator}'" if verbose && !classes.nil? && methods.nil? 42: puts "Looking in #{log} I/O for all lines" if verbose && classes.nil? && methods.nil? 43: classes =classes.split(',') unless classes.nil? 44: methods =methods.split(',') unless methods.nil? 45: classes.map!{|c| c.downcase} unless classes.nil? 46: methods.map!{|m| m.downcase} unless methods.nil? 47: line_number = 0 48: line_match = 0 49: IO.foreach(log) do |line| 50: line_number += 1 51: if match(line,classes,methods, separator) 52: puts line 53: line_match += 1 54: end 55: 56: end 57: puts "#{line_match} lines matched on the #{line_number} lines in the log" if verbose 58: end
# File lib/easy_logger/easy_logger.rb, line 105 105: def match(line,classes,methods,separator) 106: unless line[0..2].split(separator).length < 3 107: methode = line[0..2].split(separator)[2].strip.downcase 108: classe = line[0..2].split(separator)[3].strip.downcase 109: 110: (classes.nil? || classes.include?(classe)) && (methods.nil? || methods.include?(methode)) 111: else 112: false 113: end 114: end
Display the relevant new lines of the log matching the classes/methods
@param (see EasyLogger#file)
# File lib/easy_logger/easy_logger.rb, line 63 63: def tail(log, classes, methods, verbose, separator='|') 64: result = String.new 65: 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? 66: puts "Tailing in #{log} I/O for line #{methods} method(s) with separator '#{separator}'" if verbose && classes.nil? && !methods.nil? 67: puts "Tailing in #{log} I/O for line matching #{classes} classe(s) with separator '#{separator}'" if verbose && !classes.nil? && methods.nil? 68: puts "Tailing in #{log} I/O for all lines" if verbose && classes.nil? && methods.nil? 69: classes =classes.split(',') unless classes.nil? 70: methods =methods.split(',') unless methods.nil? 71: classes.map!{|c| c.downcase} unless classes.nil? 72: methods.map!{|m| m.downcase} unless methods.nil? 73: @line_number = 0 74: @line_match = 0 75: # interrupted = false 76: trap("INT") do 77: puts "#{@line_match} lines matched on the #{@line_number} lines in the log" if verbose 78: break 79: end 80: begin 81: File::Tail::Logfile.tail(log) do |line| 82: @line_number += 1 83: if match(line,classes,methods, separator) 84: puts line 85: @line_match += 1 86: end 87: # if interrupted 88: # break 89: # end 90: end 91: rescue 92: nil 93: end 94: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.