Class: EasyLogger::EasyLogger
- Inherits:
-
Object
- Object
- EasyLogger::EasyLogger
- 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
Instance Attribute Summary collapse
-
#classes ⇒ Array
readonly
the classes to match in the log.
-
#log ⇒ String
readonly
the log file/stream that we’ll look into.
-
#methods ⇒ Array
readonly
the methods to match in the log.
-
#separator ⇒ String
readonly
the separator of the values in the log.
-
#verbose ⇒ Boolean
readonly
the verbosity of the output.
Instance Method Summary collapse
-
#file(log, classes, methods, verbose, separator = '|') ⇒ Object
Display the relevant line of the log matching the classes/methods.
- #match(line, classes, methods, separator) ⇒ Object
-
#tail(log, classes, methods, verbose, separator = '|') ⇒ Object
Display the relevant <u>new</u> lines of the log matching the classes/methods.
Instance Attribute Details
#classes ⇒ Array (readonly)
the classes to match in the log
28 29 30 |
# File 'lib/easy_logger/easy_logger.rb', line 28 def classes @classes end |
#log ⇒ String (readonly)
the log file/stream that we’ll look into
28 29 30 |
# File 'lib/easy_logger/easy_logger.rb', line 28 def log @log end |
#methods ⇒ Array (readonly)
the methods to match in the log
28 29 30 |
# File 'lib/easy_logger/easy_logger.rb', line 28 def methods @methods end |
#separator ⇒ String (readonly)
the separator of the values in the log. it’s “|” per default
28 29 30 |
# File 'lib/easy_logger/easy_logger.rb', line 28 def separator @separator end |
#verbose ⇒ Boolean (readonly)
the verbosity of the output
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
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
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 |