log_jam 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/log_jam.rb +59 -0
- data/log_jam_test.rb +53 -0
- data/readme.md +10 -0
- metadata +58 -0
data/log_jam.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module LogJam
|
2
|
+
extend self
|
3
|
+
|
4
|
+
@@logs = []
|
5
|
+
|
6
|
+
def priorities(*list)
|
7
|
+
@@priorities = list
|
8
|
+
end
|
9
|
+
|
10
|
+
def puts(msg)
|
11
|
+
@@logs << msg
|
12
|
+
Rails.logger.info(msg)
|
13
|
+
end
|
14
|
+
|
15
|
+
def write_to_disk
|
16
|
+
logs = drain
|
17
|
+
@@priorities.select {|p| logs.keys.include?(p) }.map do |filter|
|
18
|
+
logs[filter].each do |log|
|
19
|
+
klass = Kernel.const_get(camelize(filter.to_s + "_log"))
|
20
|
+
klass.create("#{filter}_id" => grep_id(filter, log), :message => log)
|
21
|
+
end
|
22
|
+
end.all?
|
23
|
+
end
|
24
|
+
|
25
|
+
def drain
|
26
|
+
result = {}
|
27
|
+
until @@logs.empty? do
|
28
|
+
log = @@logs.pop
|
29
|
+
@@priorities.each do |filter|
|
30
|
+
if filtered_id = grep_id(filter.to_s, log)
|
31
|
+
result[filter] ||= []
|
32
|
+
result[filter] << log
|
33
|
+
break
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
result
|
38
|
+
end
|
39
|
+
|
40
|
+
def grep_id(term, string)
|
41
|
+
if match = string.match(/#{term}=?(\d*)/)
|
42
|
+
id = match.captures[0]
|
43
|
+
if id.respond_to?(:to_i)
|
44
|
+
id.to_i
|
45
|
+
else
|
46
|
+
raise "LogJam Error: expected #{term}=#{id} to contain an integer"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
52
|
+
if first_letter_in_uppercase
|
53
|
+
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
54
|
+
else
|
55
|
+
lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/log_jam_test.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
$:.unshift(".")
|
2
|
+
require 'log_jam.rb'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
class Rails
|
6
|
+
def self.logger
|
7
|
+
def self.info(str)
|
8
|
+
puts str
|
9
|
+
end
|
10
|
+
self
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Log
|
15
|
+
def self.create(*args)
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
class AccountLog < Log; end
|
20
|
+
class InvoiceLog < Log; end
|
21
|
+
|
22
|
+
class LogJamTest < MiniTest::Unit::TestCase
|
23
|
+
|
24
|
+
def test_write_to_disk
|
25
|
+
log_msg = "invoice=123 account=123"
|
26
|
+
LogJam.priorities(:invoice, :account)
|
27
|
+
LogJam.puts(log_msg)
|
28
|
+
assert LogJam.write_to_disk
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_write_to_disk_fails_when_storage_not_defined
|
32
|
+
log_msg = "line_items=123 invoice=123 account=123"
|
33
|
+
LogJam.priorities(:line_item, :invoice, :account)
|
34
|
+
LogJam.puts(log_msg)
|
35
|
+
assert_raises(NameError) { LogJam.write_to_disk }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_drain_with_priority
|
39
|
+
log_msg = "invoice=123 account=123"
|
40
|
+
LogJam.priorities(:invoice, :account)
|
41
|
+
LogJam.puts(log_msg)
|
42
|
+
assert_equal({:invoice => [log_msg]}, LogJam.drain)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_drain_with_priority_when_first_priority_not_present
|
46
|
+
log_msg = "line_item=123 account=123"
|
47
|
+
LogJam.priorities(:invoice, :account)
|
48
|
+
LogJam.puts(log_msg)
|
49
|
+
assert_equal({:account => [log_msg]}, LogJam.drain)
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
end
|
data/readme.md
ADDED
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: log_jam
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Smith
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-15 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: LogJam is a proxy to another logger. Say, the Rails logger. This gem intercepts the log message and writes them to a database every now and then.
|
18
|
+
email: ryan@heroku.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- readme.md
|
27
|
+
- log_jam.rb
|
28
|
+
- log_jam_test.rb
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://ryandotsmith.heroku.com
|
31
|
+
licenses: []
|
32
|
+
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- .
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.6.2
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: logging utility
|
57
|
+
test_files:
|
58
|
+
- log_jam_test.rb
|