log_wrapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/log_wrapper.rb +118 -0
  2. metadata +47 -0
@@ -0,0 +1,118 @@
1
+ require 'logger'
2
+ require 'benchmark'
3
+ require 'time'
4
+ require 'json'
5
+
6
+ class Log_Wrapper
7
+
8
+ def initialize(logdev = 'log.txt', shift_age = 0, shift_size = 1048576)
9
+ @log_file = logdev
10
+ @log = Logger.new(@log_file, shift_age, shift_size)
11
+ end
12
+
13
+ # expects a message (arg[0]) and a hash (arg[1])
14
+ # if the hash includes a payload (and no explicit guid), the payload will be probed for a guid
15
+ def method_missing(method, *args, &block)
16
+
17
+ exception = nil
18
+
19
+ message, called_by, guid, hash = get_parameters(args)
20
+ if block_given?
21
+ result, exception = execute_block(&block)
22
+ hash.merge!(result)
23
+ end
24
+
25
+ @log.__send__(method, called_by) do
26
+ "| #{guid} | #{message} | #{(hash ? hash.to_json : '')}"
27
+ end
28
+
29
+ p message unless method == :debug
30
+
31
+ raise exception if exception
32
+
33
+ end
34
+
35
+ def grep(search)
36
+ open(@log_file) do |f| f.grep(/#{search}/) do |e|
37
+ severity, date, pid, label, app, message, guid, actual_message, json = nil
38
+ e.gsub(/([\w]+),\s+\[([^\]\s]+)\s+#([^\]]+)\]\s+(\w+)\s+--\s+(.+?):\s+\|\s(.+)/) do |match|
39
+ severity, date, pid, label, app, message = $1, Time.parse($2), $3, $4, $5, $6
40
+ message.gsub(/([\w-]*)\s\|\s(.*)\s\|\s(.*)/) do |parts|
41
+ guid, actual_message, payload = $1, $2, $3
42
+ json = JSON.parse(payload) if payload =~ /{.+}/
43
+ end
44
+ end
45
+ times, payload = nil
46
+ if json
47
+ times = json['times']
48
+ payload = json['payload']
49
+ end
50
+ {
51
+ 'severity' => severity,
52
+ 'date' => date,
53
+ 'pid' => pid,
54
+ 'label' => label,
55
+ 'app' => app,
56
+ 'message' => actual_message,
57
+ 'guid' => guid,
58
+ 'times' => times,
59
+ 'payload' => payload
60
+ }
61
+ end
62
+ end
63
+ end
64
+
65
+
66
+ private
67
+
68
+ def execute_block(&block)
69
+
70
+ error_detail, exception = nil
71
+
72
+ timings = Benchmark.measure do
73
+ begin
74
+ block.call
75
+ rescue => e
76
+ exception = e
77
+ error_detail = { 'exception' => {
78
+ 'type' => e.class.to_s,
79
+ 'message' => e.message,
80
+ 'backtrace' => e.backtrace.join("<br/>")
81
+ }
82
+ }
83
+ end
84
+ end
85
+
86
+ t = timings.to_a
87
+ result = { 'times' => {
88
+ 'user' => t[1],
89
+ 'system' => t[2],
90
+ 'elapsed' => t[5]
91
+ }
92
+ }
93
+ result.merge!(error_detail) if error_detail
94
+
95
+ return result, exception
96
+ end
97
+
98
+ def get_parameters(args)
99
+
100
+ message = args[0]
101
+ called_by = caller[1][/`([^']*)'/, 1]
102
+
103
+ hash, guid = nil
104
+ hash = args[1] if args[1] && args[1].is_a?(Hash)
105
+
106
+ if hash
107
+ guid = hash[:guid]
108
+ unless guid
109
+ guid = hash[:payload]['guid'] if hash[:payload]
110
+ end
111
+ end
112
+
113
+ hash ||= {}
114
+
115
+ return message, called_by, guid, hash
116
+ end
117
+
118
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log_wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Renen Watermeyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-29 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A trivial wrapper for the Ruby Logger that adds convenience methods for
15
+ timing execution, for logging in json, and for grep'ing the current log.
16
+ email: renen@121.co.za
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/log_wrapper.rb
22
+ homepage: http://rubygems.org/gems/log_wrapper
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.10
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Adds convenience methods for timing execution, for logging in json, and for
46
+ grep'ing the current log to the Ruby Logger.
47
+ test_files: []