object_logging 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ 0.2.0
2
+ * feature: threadsafety
3
+ * feature: including ObjectLogging in a class's metaclass works as expected
4
+
5
+ 0.1.1
6
+ * bugfix: ObjectLogging::Log not being loaded
7
+
8
+ 0.1.0
9
+ * initial release
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -1,21 +1,24 @@
1
+ require "thread"
2
+
1
3
  module ObjectLogging
2
4
  module Log
3
5
  class Base
4
6
 
5
7
  def initialize(object, options)
6
8
  @storage = []
9
+ @lock = Mutex.new
7
10
  end
8
11
 
9
12
  def log(level, context, message)
10
- @storage << [level, context, message]
13
+ @lock.synchronize{ @storage << [level, context, message] }
11
14
  end
12
15
 
13
16
  def entries
14
- @storage
17
+ @lock.synchronize{ @storage.dup }
15
18
  end
16
19
 
17
20
  def clear
18
- @storage = []
21
+ @lock.synchronize{ @storage.clear }
19
22
  end
20
23
 
21
24
  end
@@ -38,7 +38,7 @@ module ObjectLogging
38
38
  private
39
39
 
40
40
  def instantiate_log(object)
41
- type, options = object.class.object_logging
41
+ type, options = object.metaclass.object_logging
42
42
  type = "base", options = {} if type.nil?
43
43
  class_name = type.to_s.split("_").collect{ |word| word.capitalize }.join
44
44
  klass = ObjectLogging::Log.const_get(class_name)
@@ -48,7 +48,8 @@ module ObjectLogging
48
48
  def make_entry(level, message, options)
49
49
  method_name = options[:method_name] || get_method_name
50
50
  class_name = options[:class_name] || get_class_name(method_name)
51
- context = "#{class_name}##{method_name}"
51
+ invoker = @object.instance_of?(Class) ? "." : "#"
52
+ context = "#{class_name}#{invoker}#{method_name}"
52
53
  @log.log(level.upcase, context, message)
53
54
  end
54
55
 
@@ -63,7 +64,7 @@ module ObjectLogging
63
64
 
64
65
  def get_class_name(method_name)
65
66
  if @object.respond_to?(method_name.to_sym)
66
- @object.class.name
67
+ @object.instance_of?(Class) ? @object.name : @object.class.name
67
68
  else
68
69
  "(unknown)"
69
70
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{object_logging}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Christopher J. Bottaro"]
12
- s.date = %q{2010-05-10}
12
+ s.date = %q{2010-05-11}
13
13
  s.description = %q{It's a pain to trudge through large log files. Sometimes you just want to see log messages for one particular object.}
14
14
  s.email = %q{cjbottaro@alumni.cs.utexas.edu}
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ "CHANGELOG",
22
23
  "LICENSE",
23
24
  "README.rdoc",
24
25
  "Rakefile",
@@ -54,4 +54,18 @@ class ObjectLoggingTest < Test::Unit::TestCase
54
54
  assert_equal([:rails_log, {}], s.class.object_logging)
55
55
  end
56
56
 
57
+ def test_static
58
+ Static.foo
59
+ assert_equal([["DEBUG", "Static.foo", "hio"]], Static.logger.entries)
60
+ end
61
+
62
+ def test_threads
63
+ u = User.new
64
+ threads = (1..10).collect{ Thread.new{ u.thr } }
65
+ threads.each{ |thread| thread.join }
66
+ expected = threads.collect{ |thread| "[INFO] User#thr from thread #{thread}" }.to_set
67
+ actual = u.logger.entries.collect{ |level, context, message| "[#{level}] #{context} #{message}" }.to_set
68
+ assert_equal expected, actual
69
+ end
70
+
57
71
  end
@@ -4,6 +4,7 @@ require 'test/unit'
4
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
5
  $LOAD_PATH.unshift(File.dirname(__FILE__))
6
6
  require 'object_logging'
7
+ require "set"
7
8
 
8
9
  class Test::Unit::TestCase
9
10
  end
@@ -18,6 +19,10 @@ class User
18
19
  def bar
19
20
  logger.info("bar")
20
21
  end
22
+
23
+ def thr
24
+ logger.info("from thread #{Thread.current}")
25
+ end
21
26
  end
22
27
 
23
28
  class Account
@@ -39,4 +44,13 @@ class Person
39
44
  end
40
45
 
41
46
  class Schmuck < Person
47
+ end
48
+
49
+ class Static
50
+ class << self
51
+ include ObjectLogging
52
+ def foo
53
+ logger.debug "hio"
54
+ end
55
+ end
42
56
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Christopher J. Bottaro
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-10 00:00:00 -05:00
17
+ date: 2010-05-11 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -30,6 +30,7 @@ extra_rdoc_files:
30
30
  files:
31
31
  - .document
32
32
  - .gitignore
33
+ - CHANGELOG
33
34
  - LICENSE
34
35
  - README.rdoc
35
36
  - Rakefile