Class: Log_Wrapper
- Inherits:
-
Object
- Object
- Log_Wrapper
- Defined in:
- lib/log_wrapper.rb
Instance Method Summary collapse
- #grep(search) ⇒ Object
-
#initialize(logdev = 'log.txt', shift_age = 0, shift_size = 1048576) ⇒ Log_Wrapper
constructor
A new instance of Log_Wrapper.
- #method_missing(method, *args, &block) ⇒ Object
- #web_grep(guid) ⇒ Object
Constructor Details
#initialize(logdev = 'log.txt', shift_age = 0, shift_size = 1048576) ⇒ Log_Wrapper
Returns a new instance of Log_Wrapper.
8 9 10 11 |
# File 'lib/log_wrapper.rb', line 8 def initialize(logdev = 'log.txt', shift_age = 0, shift_size = 1048576) @log_file = logdev @log = Logger.new(@log_file, shift_age, shift_size) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/log_wrapper.rb', line 15 def method_missing(method, *args, &block) exception = nil , called_by, guid, hash = get_parameters(args) if block_given? result, exception = execute_block(&block) hash.merge!(result) end @log.__send__(method, called_by) do "| #{guid} | #{} | #{(hash ? hash.to_json : '')}" end p unless method == :debug raise exception if exception end |
Instance Method Details
#grep(search) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/log_wrapper.rb', line 35 def grep(search) open(@log_file) do |f| f.grep(/#{search}/) do |e| severity, date, pid, label, app, , guid, , json = nil e.gsub(/([\w]+),\s+\[([^\]\s]+)\s+#([^\]]+)\]\s+(\w+)\s+--\s+(.+?):\s+\|\s(.+)/) do |match| severity, date, pid, label, app, = $1, Time.parse($2), $3, $4, $5, $6 .gsub(/([\w-]*)\s\|\s(.*)\s\|\s(.*)/) do |parts| guid, , payload = $1, $2, $3 json = JSON.parse(payload) if payload =~ /{.+}/ end end times, payload = nil if json times = json['times'] payload = json['payload'] end { 'severity' => severity, 'date' => date, 'pid' => pid, 'label' => label, 'app' => app, 'message' => , 'guid' => guid, 'times' => times, 'payload' => payload } end end end |
#web_grep(guid) ⇒ Object
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/log_wrapper.rb', line 65 def web_grep(guid) log_lines, start_time, end_time, duration = nil if guid && guid.length>0 log_lines = grep(guid) user, system, elapsed = 0.0, 0.0, 0.0 slowest_duration = -1.0 timings = false if log_lines.length > 0 start_time = end_time = log_lines[0]['date'] log_lines.each do |e| date = e['date'] start_time = date if date < start_time end_time = date if date > end_time if e['times'] user += e['times']['user'] system += e['times']['system'] elapsed += e['times']['elapsed'] timings = true slowest_duration = e['times']['elapsed'] if e['times']['elapsed'] > slowest_duration end end end end t = {} if timings t['times'] = { 'user' => user, 'system' => system, 'elapsed' => elapsed } end duration = (end_time - start_time) if end_time t.merge( { 'guid' => guid, 'log_lines' => log_lines, 'start_time' => start_time, 'end_time' => end_time, 'duration' => duration, 'slowest_duration' => slowest_duration } ) end |