tagged_logger 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new do |t|
5
5
  t.libs << "test"
6
- t.test_files = FileList['test/test.rb', 'test/test_examples.rb']
6
+ t.test_files = FileList['test/test.rb', 'test/test_rails.rb', 'test/test_examples.rb']
7
7
  t.verbose = true
8
8
  end
9
9
 
data/lib/tagged_logger.rb CHANGED
@@ -14,14 +14,37 @@ class TaggedLogger
14
14
  end
15
15
 
16
16
  def rules(options = {}, &block)
17
- klasses = []
18
- # klasses << AbstractController::Base if Object.const_defined? :AbstractController
19
- klasses << Object
17
+ @old_methods_restored = false
20
18
  override = options.delete :override
19
+ klasses = overridees
21
20
  klasses = klasses.select{ |klass| !klass.respond_to?(:logger, true)} if !override
22
21
  klasses.each{ |klass| inject_logger_method_in_call_chain(klass)}
23
22
  instance_eval(&block)
24
23
  end
24
+
25
+ def klass_has_method?(klass, method)
26
+ klass.instance_methods(false).include?(RUBY_VERSION >= '1.9' ? method.to_sym : method.to_s)
27
+ end
28
+
29
+ def restore_old_logger_methods
30
+ return if @old_methods_restored
31
+ @old_methods_restored = true
32
+ overridees.each do |klass|
33
+ if klass_has_method?(klass, :tagged_logger_original_logger)
34
+ klass.class_eval {alias_method :logger, :tagged_logger_original_logger}
35
+ elsif klass_has_method?(klass, :logger)
36
+ klass.class_eval {remove_method :logger}
37
+ end
38
+ end
39
+ end
40
+
41
+ def overridees
42
+ klasses = []
43
+ klasses << ActionController::Base if Object.const_defined? :ActionController
44
+ klasses << Object
45
+ klasses
46
+ end
47
+
25
48
 
26
49
  def blocks_for(level, tag)
27
50
  blocks = []
@@ -125,6 +148,10 @@ class TaggedLogger
125
148
  end
126
149
 
127
150
  def inject_logger_method_in_call_chain(definee_klass)
151
+ if klass_has_method?(definee_klass, :logger)
152
+ #so we could resurrect old :logger method if we need
153
+ definee_klass.class_eval { alias_method :tagged_logger_original_logger, :logger }
154
+ end
128
155
  definee_klass.class_eval do
129
156
  def logger
130
157
  klass = self.class == Class ? self : self.class
data/test/test.rb CHANGED
@@ -1,19 +1,6 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'shoulda'
4
- require 'logger'
5
- require 'rr'
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+ require 'test_helper'
6
3
 
7
- require File.dirname(__FILE__) + '/../tagged_logger'
8
-
9
- class TestLogDevice
10
- attr_reader :last
11
- def write(msg); @last = msg; end
12
- def close; end;
13
- def to_s; @last || ""; end
14
- def inspect; to_s; end
15
- def clear; write(""); end
16
- end
17
4
 
18
5
  class TaggedLoggerTest < Test::Unit::TestCase
19
6
  include RR::Adapters::TestUnit
@@ -24,7 +11,7 @@ class TaggedLoggerTest < Test::Unit::TestCase
24
11
  end
25
12
 
26
13
  teardown do
27
- Object.class_eval { remove_method :logger }
14
+ TaggedLogger.restore_old_logger_methods
28
15
  end
29
16
 
30
17
  should "be able to intialize with minimal effort, without defining any rules" do
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../'
2
+ require 'rubygems'
3
+ require 'tagged_logger'
4
+ require 'test/unit'
5
+ require 'shoulda'
6
+ require 'logger'
7
+ require 'rr'
8
+ require 'test_log_device'
@@ -0,0 +1,8 @@
1
+ class TestLogDevice
2
+ attr_reader :last
3
+ def write(msg); @last = msg; end
4
+ def close; end;
5
+ def to_s; @last || ""; end
6
+ def inspect; to_s; end
7
+ def clear; write(""); end
8
+ end
@@ -0,0 +1,57 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+ require 'test_helper'
3
+ require 'rails/all'
4
+
5
+ module Test
6
+ def self.rails_log_device
7
+ @rails_log_device ||= TestLogDevice.new
8
+ end
9
+
10
+ class Application < Rails::Application
11
+ config.logger = Logger.new(Test.rails_log_device)
12
+ end
13
+ end
14
+
15
+ class TestController < ActionController::Base
16
+ def hi
17
+ logger.info "hi"
18
+ end
19
+ end
20
+
21
+ Test::Application.initialize!
22
+
23
+ class TaggedLoggerRailsTest < Test::Unit::TestCase
24
+ include RR::Adapters::TestUnit
25
+
26
+ context "stub output device @@stub_out;" do
27
+ setup do
28
+ @@stub_out = TestLogDevice.new
29
+ end
30
+
31
+ teardown do
32
+ TaggedLogger.restore_old_logger_methods
33
+ end
34
+
35
+ should "be able possible to initialize in the way to override Rails existing logger" do
36
+ assert TestController.new.respond_to? :logger
37
+ TaggedLogger.rules(:override=>true) do
38
+ info /.*/ do |level, tag, message|
39
+ @@stub_out.write(tag.to_s)
40
+ end
41
+ end
42
+ @@stub_out.write("TestController")
43
+ TestController.new.hi
44
+ end
45
+
46
+ should "be possible to restore old logger methods" do
47
+ TaggedLogger.rules(:override=>true) do
48
+ info(/.*/) {|level, tag, message| debugger; stop = 9}
49
+ end
50
+ TaggedLogger.restore_old_logger_methods
51
+ mock(Test.rails_log_device).write("hi\n")
52
+ TestController.new.hi
53
+ end
54
+
55
+ end
56
+ end
57
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tagged_logger
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 3
10
- version: 0.3.3
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Aleksandr Furmanov
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-17 00:00:00 -05:00
18
+ date: 2010-10-19 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -60,6 +60,22 @@ dependencies:
60
60
  version: "0"
61
61
  type: :development
62
62
  version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rails
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">"
70
+ - !ruby/object:Gem::Version
71
+ hash: 7
72
+ segments:
73
+ - 3
74
+ - 0
75
+ - 0
76
+ version: 3.0.0
77
+ type: :development
78
+ version_requirements: *id004
63
79
  description: Detaches _what_ is logged from _how_ it is logged
64
80
  email:
65
81
  - aleksandr.furmanov@gmail.com
@@ -79,7 +95,10 @@ files:
79
95
  - test/expected_examples_output.txt
80
96
  - test/test.rb
81
97
  - test/test_examples.rb
98
+ - test/test_helper.rb
99
+ - test/test_log_device.rb
82
100
  - test/test_performance.rb
101
+ - test/test_rails.rb
83
102
  - examples/changing_format.rb
84
103
  - examples/one_log_per_classes.rb
85
104
  - examples/one_tag_per_classes.rb