tagged_logger 0.2.1 → 0.2.5

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.
data/.gitignore CHANGED
@@ -2,4 +2,5 @@ pkg
2
2
  doc
3
3
  Manifest
4
4
  README.html
5
+ CHANGES.html
5
6
  *.log
data/CHANGES.markdown ADDED
@@ -0,0 +1,13 @@
1
+ # tagged_logger changelog
2
+
3
+ ## Version 0.2.5
4
+
5
+ * If somebody is not ready to define any logging rules, but still want to use #logger everywhere, use new _init()_ method:
6
+
7
+ _TaggedLogger.init_
8
+
9
+ * Runs about 3 times faster by executing code responsible for rule matching to the first _#logger()_ call and "precompiling" it.
10
+ So rather than iterating over **all** rules on every _#logger_ call only subset of rules specific to _#logger()_ callee are executed.
11
+
12
+ * Code simplified
13
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.5
data/lib/tagged_logger.rb CHANGED
@@ -2,30 +2,32 @@ require 'delegate'
2
2
  require 'facets/dictionary'
3
3
 
4
4
  class TaggedLogger
5
- @logger_generator_exists = false
6
5
  @rename_rules = Dictionary.new
7
6
  @tag_blocks = Dictionary.new
8
-
7
+
9
8
  class << self
10
9
  def reset
11
10
  @rename_rules = Dictionary.new
12
11
  @tag_blocks = Dictionary.new
12
+ ObjectSpace.each_object(ClassSpecificLogger) { |obj| obj.detach }
13
13
  end
14
14
 
15
15
  def rules(&block)
16
16
  instance_eval(&block)
17
17
  end
18
18
 
19
- def log(level, tag, msg)
19
+ def blocks_for(tag)
20
+ blocks = []
20
21
  tag_aliases(tag) do |tag_alias|
21
22
  tag_blocks(tag_alias) do |tag_block|
22
- tag_block.call(level, tag_alias, msg)
23
+ blocks << [tag_alias, tag_block]
23
24
  end
24
25
  end
26
+ blocks
25
27
  end
26
28
 
27
29
  def init
28
- add_logger_generator_in_Object
30
+ add_logger_generator_in_Kernel
29
31
  end
30
32
 
31
33
  def output_everything_to(logger = nil, &block)
@@ -37,7 +39,7 @@ class TaggedLogger
37
39
  end
38
40
 
39
41
  def output(spec, &block)
40
- add_logger_generator_in_Object
42
+ add_logger_generator_in_Kernel
41
43
  if spec.is_a? Hash
42
44
  spec.each do |what, where|
43
45
  @tag_blocks[tag_matcher(what)] = lambda { |level, tag, msg | where.send(level, "#{tag}: #{msg}\n") }
@@ -96,30 +98,43 @@ class TaggedLogger
96
98
  end
97
99
  end
98
100
 
99
- def add_logger_generator_in_Object
100
- return if @logger_generator_exists
101
- @logger_generator_exists = true
102
- Object.send(:define_method, "logger") do
103
- self.class.class_eval do
104
- attr_reader :class_logger
105
- @class_logger = nil
106
- def logger
107
- return class_logger if class_logger
108
- @class_logger = SimpleDelegator.new(TaggedLogger)
109
- class_logging_tag = self.class == Class ? self.to_s : self.class.to_s
110
- [:debug, :warn, :info, :error, :fatal].each do |method|
111
- (class << @class_logger;self;end).instance_eval do
112
- define_method method do |msg|
113
- __getobj__.log(method, class_logging_tag, msg)
114
- end
115
- end
116
- end
101
+ def add_logger_generator_in_Kernel
102
+ return if Kernel.respond_to?(:logger, true) #no harm
103
+ Kernel.class_eval do
104
+ def logger
105
+ klass = self.class == Class ? self : self.class
106
+ result = klass.class_eval do
107
+ return @class_logger if @class_logger
108
+ @class_logger = ClassSpecificLogger.new(klass)
117
109
  @class_logger
118
110
  end
111
+ result
119
112
  end
120
- logger
121
113
  end
122
114
  end
123
115
 
124
116
  end # class methods
117
+
118
+ class ClassSpecificLogger
119
+ def eigenclass
120
+ class <<self; self; end
121
+ end
122
+
123
+ def initialize(klass)
124
+ @klass = klass
125
+ blocks = TaggedLogger.blocks_for(klass.to_s)
126
+ [:debug, :warn, :info, :error, :fatal].each do |method|
127
+ eigenclass.send(:define_method, method) do |msg|
128
+ blocks.each { |(tag_alias, block)| block.call(method, tag_alias, msg) }
129
+ end
130
+ end
131
+ end
132
+
133
+ def detach
134
+ @klass.class_eval do
135
+ @class_logger = nil
136
+ end
137
+ end
138
+ end
139
+
125
140
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tagged_logger}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Fedor Kocherga"]
12
- s.date = %q{2010-01-02}
12
+ s.date = %q{2010-01-13}
13
13
  s.email = %q{fkocherga@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.html",
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  ]
18
18
  s.files = [
19
19
  ".gitignore",
20
+ "CHANGES.markdown",
20
21
  "MIT-LICENSE",
21
22
  "README.markdown",
22
23
  "Rakefile",
data/test/test.rb CHANGED
@@ -18,7 +18,8 @@ end
18
18
  class TaggedLoggerTest < Test::Unit::TestCase
19
19
  include RR::Adapters::TestUnit
20
20
 
21
- context "@standard_logger writing to device @out1" do
21
+
22
+ context "@standard_logger with stub output @out1;" do
22
23
  setup do
23
24
  @out1 = TestLogDevice.new
24
25
  @standard_logger = Logger.new(@out1)
@@ -26,6 +27,12 @@ class TaggedLoggerTest < Test::Unit::TestCase
26
27
  @standard_logger.formatter = @formatter
27
28
  end
28
29
 
30
+ teardown do
31
+ Kernel.class_eval do
32
+ remove_method :logger
33
+ end
34
+ end
35
+
29
36
  should "be able to intialize with minimal effort" do
30
37
  TaggedLogger.init
31
38
  dont_allow(@out1).write
@@ -33,7 +40,7 @@ class TaggedLoggerTest < Test::Unit::TestCase
33
40
  assert_nothing_raised { Class.new.logger }
34
41
  end
35
42
 
36
- context "any attempt to log something goes to @standard_logger" do
43
+ context "everything gets logged to @out1;" do
37
44
  setup do
38
45
  logger = @standard_logger
39
46
  TaggedLogger.rules do
@@ -42,7 +49,7 @@ class TaggedLoggerTest < Test::Unit::TestCase
42
49
  end
43
50
  end
44
51
 
45
- should "do logging with different levels" do
52
+ should " be possible to log by .logger.debug/info/warn/error/fatal()" do
46
53
  NewClass = Class.new
47
54
  obj = NewClass.new
48
55
  %w[debug info warn error fatal].each do |method|
@@ -78,14 +85,14 @@ class TaggedLoggerTest < Test::Unit::TestCase
78
85
  assert_equal "TEST: foo\n", @out1.to_s
79
86
  end
80
87
 
81
- context "specialized logging" do
88
+ context "@logger2 with stub output @out2;" do
82
89
  setup do
83
90
  @out2 = TestLogDevice.new
84
91
  @logger2 = Logger.new(@out2)
85
92
  @logger2.formatter = @formatter
86
93
  end
87
94
 
88
- should "be possible to speialize logging for tag A by specifying different logger" do
95
+ should "be possible to speialize logging for tag A by specifying another @logger2" do
89
96
  logger2 = @logger2
90
97
  TaggedLogger.rules do
91
98
  output A => logger2
data/todo.txt CHANGED
@@ -1 +1,21 @@
1
- 1. Simplest configuration with warning
1
+ - It is slow: logger on first call has to select only applicable rules and
2
+ run only them afterwards rather than going via all rules each time. It will
3
+ gain almost 300% in parsing speed on my app.
4
+
5
+ - Replace output ... => ... with
6
+ format ...
7
+ output ... => do
8
+ format ....
9
+ end
10
+
11
+ - output ... => ... - may be:
12
+ debug ... => ...
13
+ info ... => ...
14
+
15
+ - Simplest configuration with warning
16
+
17
+ - Switch tests on rr expectations
18
+
19
+ - logger called within Base does not log when Derived is instantiated
20
+
21
+ - writing messages in any encoding
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tagged_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fedor Kocherga
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-02 00:00:00 -06:00
12
+ date: 2010-01-13 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,6 +24,7 @@ extra_rdoc_files:
24
24
  - README.markdown
25
25
  files:
26
26
  - .gitignore
27
+ - CHANGES.markdown
27
28
  - MIT-LICENSE
28
29
  - README.markdown
29
30
  - Rakefile