sawmill 0.0.1

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.
Files changed (38) hide show
  1. data/History.rdoc +3 -0
  2. data/README.rdoc +81 -0
  3. data/Rakefile +147 -0
  4. data/lib/sawmill/entry.rb +307 -0
  5. data/lib/sawmill/entry_classifier.rb +75 -0
  6. data/lib/sawmill/entry_processor/build_records.rb +157 -0
  7. data/lib/sawmill/entry_processor/conditionals.rb +444 -0
  8. data/lib/sawmill/entry_processor/filter_basic_fields.rb +145 -0
  9. data/lib/sawmill/entry_processor/format.rb +228 -0
  10. data/lib/sawmill/entry_processor/simple_queue.rb +116 -0
  11. data/lib/sawmill/entry_processor.rb +158 -0
  12. data/lib/sawmill/errors.rb +66 -0
  13. data/lib/sawmill/level.rb +264 -0
  14. data/lib/sawmill/log_record_middleware.rb +93 -0
  15. data/lib/sawmill/logger.rb +373 -0
  16. data/lib/sawmill/parser.rb +181 -0
  17. data/lib/sawmill/record.rb +255 -0
  18. data/lib/sawmill/record_processor/conditionals.rb +330 -0
  19. data/lib/sawmill/record_processor/decompose.rb +75 -0
  20. data/lib/sawmill/record_processor/filter_by_attributes.rb +87 -0
  21. data/lib/sawmill/record_processor/filter_by_record_id.rb +77 -0
  22. data/lib/sawmill/record_processor/format.rb +88 -0
  23. data/lib/sawmill/record_processor/simple_queue.rb +117 -0
  24. data/lib/sawmill/record_processor.rb +137 -0
  25. data/lib/sawmill/rotater/base.rb +90 -0
  26. data/lib/sawmill/rotater/date_based_log_file.rb +145 -0
  27. data/lib/sawmill/rotater/shifting_log_file.rb +166 -0
  28. data/lib/sawmill/rotater.rb +236 -0
  29. data/lib/sawmill/util/queue.rb +138 -0
  30. data/lib/sawmill/version.rb +47 -0
  31. data/lib/sawmill.rb +78 -0
  32. data/tests/tc_entry_processors.rb +138 -0
  33. data/tests/tc_formatter_parser.rb +144 -0
  34. data/tests/tc_levels.rb +118 -0
  35. data/tests/tc_logger.rb +315 -0
  36. data/tests/tc_record_processors.rb +117 -0
  37. data/tests/tc_records.rb +206 -0
  38. metadata +116 -0
@@ -0,0 +1,315 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Sawmill: tests on the basic logger
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2009 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+
35
+
36
+ require 'test/unit'
37
+ require ::File.expand_path("#{::File.dirname(__FILE__)}/../lib/sawmill.rb")
38
+
39
+
40
+ module Sawmill
41
+ module Tests # :nodoc:
42
+
43
+ class TestLogger < ::Test::Unit::TestCase # :nodoc:
44
+
45
+
46
+ def setup
47
+ @entries = ::Sawmill::EntryProcessor::SimpleQueue.new
48
+ @logger = ::Sawmill::Logger.new(:processor => @entries)
49
+ @levels = ::Sawmill::STANDARD_LEVELS
50
+ end
51
+
52
+
53
+ # Test basic log messages using the add method
54
+
55
+ def test_add
56
+ @logger.add(:INFO, 'Hello 1')
57
+ entry_ = @entries.dequeue
58
+ assert_equal(@levels.get(:INFO), entry_.level)
59
+ assert_equal('sawmill', entry_.progname)
60
+ assert_nil(entry_.record_id)
61
+ assert_equal('Hello 1', entry_.message)
62
+
63
+ @logger.add(:ERROR, 'Hello 2', 'altprog')
64
+ entry_ = @entries.dequeue
65
+ assert_equal(@levels.get(:ERROR), entry_.level)
66
+ assert_equal('altprog', entry_.progname)
67
+ assert_nil(entry_.record_id)
68
+ assert_equal('Hello 2', entry_.message)
69
+
70
+ @logger.add(:WARN){ 'Hello 3' }
71
+ entry_ = @entries.dequeue
72
+ assert_equal(@levels.get(:WARN), entry_.level)
73
+ assert_equal('sawmill', entry_.progname)
74
+ assert_nil(entry_.record_id)
75
+ assert_equal('Hello 3', entry_.message)
76
+
77
+ @logger.add(:DEBUG, 'Hello 4')
78
+ assert_equal(0, @entries.size)
79
+ end
80
+
81
+
82
+ # Test convenience logging methods
83
+
84
+ def test_convenience_add
85
+ @logger.info('Hello 1')
86
+ entry_ = @entries.dequeue
87
+ assert_equal(@levels.get(:INFO), entry_.level)
88
+ assert_equal('sawmill', entry_.progname)
89
+ assert_nil(entry_.record_id)
90
+ assert_equal('Hello 1', entry_.message)
91
+
92
+ @logger.error('altprog'){ 'Hello 2' }
93
+ entry_ = @entries.dequeue
94
+ assert_equal(@levels.get(:ERROR), entry_.level)
95
+ assert_equal('altprog', entry_.progname)
96
+ assert_nil(entry_.record_id)
97
+ assert_equal('Hello 2', entry_.message)
98
+
99
+ @logger.warn(){ 'Hello 3' }
100
+ entry_ = @entries.dequeue
101
+ assert_equal(@levels.get(:WARN), entry_.level)
102
+ assert_equal('sawmill', entry_.progname)
103
+ assert_nil(entry_.record_id)
104
+ assert_equal('Hello 3', entry_.message)
105
+
106
+ @logger.debug('Hello 4')
107
+ assert_equal(0, @entries.size)
108
+
109
+ @logger.fatal('Hello 5')
110
+ entry_ = @entries.dequeue
111
+ assert_equal(@levels.get(:FATAL), entry_.level)
112
+ assert_equal('sawmill', entry_.progname)
113
+ assert_nil(entry_.record_id)
114
+ assert_equal('Hello 5', entry_.message)
115
+
116
+ @logger.any('Hello 6')
117
+ entry_ = @entries.dequeue
118
+ assert_equal(@levels.get(:ANY), entry_.level)
119
+ assert_equal('sawmill', entry_.progname)
120
+ assert_nil(entry_.record_id)
121
+ assert_equal('Hello 6', entry_.message)
122
+
123
+ @logger.unknown('Hello 7')
124
+ entry_ = @entries.dequeue
125
+ assert_equal(@levels.get(:ANY), entry_.level)
126
+ assert_equal('sawmill', entry_.progname)
127
+ assert_nil(entry_.record_id)
128
+ assert_equal('Hello 7', entry_.message)
129
+
130
+ assert_raise(::NoMethodError) do
131
+ @logger.always('Hello 8')
132
+ end
133
+ end
134
+
135
+
136
+ # Test current level queries
137
+
138
+ def test_level_queries
139
+ assert_equal(@levels.get(:INFO), @logger.level)
140
+ assert_equal(false, @logger.debug?)
141
+ assert_equal(true, @logger.info?)
142
+ assert_equal(true, @logger.warn?)
143
+ assert_equal(true, @logger.error?)
144
+ assert_equal(true, @logger.fatal?)
145
+ assert_equal(true, @logger.any?)
146
+ assert_equal(true, @logger.unknown?)
147
+ @logger.level = :ERROR
148
+ assert_equal(@levels.get(:ERROR), @logger.level)
149
+ assert_equal(false, @logger.debug?)
150
+ assert_equal(false, @logger.info?)
151
+ assert_equal(false, @logger.warn?)
152
+ assert_equal(true, @logger.error?)
153
+ assert_equal(true, @logger.fatal?)
154
+ assert_equal(true, @logger.any?)
155
+ @logger.level = ::Logger::DEBUG
156
+ assert_equal(@levels.get(:DEBUG), @logger.level)
157
+ assert_equal(true, @logger.debug?)
158
+ assert_equal(true, @logger.info?)
159
+ assert_equal(true, @logger.warn?)
160
+ assert_equal(true, @logger.error?)
161
+ assert_equal(true, @logger.fatal?)
162
+ assert_equal(true, @logger.any?)
163
+ end
164
+
165
+
166
+ # Test setting the progname
167
+
168
+ def test_setting_progname
169
+ assert_equal('sawmill', @logger.progname)
170
+ @logger.info('Hello 1')
171
+ entry_ = @entries.dequeue
172
+ assert_equal('sawmill', entry_.progname)
173
+ assert_equal('Hello 1', entry_.message)
174
+ @logger.progname = 'rails'
175
+ assert_equal('rails', @logger.progname)
176
+ @logger.info('Hello 2')
177
+ entry_ = @entries.dequeue
178
+ assert_equal('rails', entry_.progname)
179
+ assert_equal('Hello 2', entry_.message)
180
+ end
181
+
182
+
183
+ # Test record delimiters
184
+
185
+ def test_record_delimiters_auto_id
186
+ id_ = @logger.begin_record
187
+ @logger.info('Hello 1')
188
+ @logger.end_record
189
+ entry_ = @entries.dequeue
190
+ assert_equal(:begin_record, entry_.type)
191
+ assert_equal(@levels.get(:ANY), entry_.level)
192
+ assert_equal('sawmill', entry_.progname)
193
+ assert_equal(id_, entry_.record_id)
194
+ entry_ = @entries.dequeue
195
+ assert_equal(:message, entry_.type)
196
+ assert_equal(@levels.get(:INFO), entry_.level)
197
+ assert_equal('sawmill', entry_.progname)
198
+ assert_equal(id_, entry_.record_id)
199
+ assert_equal('Hello 1', entry_.message)
200
+ entry_ = @entries.dequeue
201
+ assert_equal(:end_record, entry_.type)
202
+ assert_equal(@levels.get(:ANY), entry_.level)
203
+ assert_equal('sawmill', entry_.progname)
204
+ assert_equal(id_, entry_.record_id)
205
+ assert_equal(0, @entries.size)
206
+ end
207
+
208
+
209
+ # Test record delimiters
210
+
211
+ def test_record_delimiters_custom_id
212
+ @logger.begin_record('1234')
213
+ @logger.info('Hello 2')
214
+ @logger.end_record
215
+ entry_ = @entries.dequeue
216
+ assert_equal(:begin_record, entry_.type)
217
+ assert_equal(@levels.get(:ANY), entry_.level)
218
+ assert_equal('sawmill', entry_.progname)
219
+ assert_equal('1234', entry_.record_id)
220
+ entry_ = @entries.dequeue
221
+ assert_equal(:message, entry_.type)
222
+ assert_equal(@levels.get(:INFO), entry_.level)
223
+ assert_equal('sawmill', entry_.progname)
224
+ assert_equal('1234', entry_.record_id)
225
+ assert_equal('Hello 2', entry_.message)
226
+ entry_ = @entries.dequeue
227
+ assert_equal(:end_record, entry_.type)
228
+ assert_equal(@levels.get(:ANY), entry_.level)
229
+ assert_equal('sawmill', entry_.progname)
230
+ assert_equal('1234', entry_.record_id)
231
+ assert_equal(0, @entries.size)
232
+ end
233
+
234
+
235
+ # Test record delimiters
236
+
237
+ def test_message_outside_record
238
+ @logger.begin_record
239
+ @logger.end_record
240
+ @logger.info('Hello 3')
241
+ entry_ = @entries.dequeue
242
+ assert_equal(:begin_record, entry_.type)
243
+ entry_ = @entries.dequeue
244
+ assert_equal(:end_record, entry_.type)
245
+ entry_ = @entries.dequeue
246
+ assert_equal(:message, entry_.type)
247
+ assert_equal(@levels.get(:INFO), entry_.level)
248
+ assert_equal('sawmill', entry_.progname)
249
+ assert_nil(entry_.record_id)
250
+ assert_equal('Hello 3', entry_.message)
251
+ assert_equal(0, @entries.size)
252
+ end
253
+
254
+
255
+ # Test attribute
256
+
257
+ def test_attribute
258
+ id_ = @logger.begin_record
259
+ @logger.set_attribute('user', 'daniel')
260
+ @logger.end_record
261
+ entry_ = @entries.dequeue
262
+ assert_equal(:begin_record, entry_.type)
263
+ assert_equal(id_, entry_.record_id)
264
+ entry_ = @entries.dequeue
265
+ assert_equal(:attribute, entry_.type)
266
+ assert_equal(@levels.get(:ANY), entry_.level)
267
+ assert_equal('sawmill', entry_.progname)
268
+ assert_equal(id_, entry_.record_id)
269
+ assert_equal(:set, entry_.operation)
270
+ assert_equal('user', entry_.key)
271
+ assert_equal('daniel', entry_.value)
272
+ entry_ = @entries.dequeue
273
+ assert_equal(:end_record, entry_.type)
274
+ assert_equal(id_, entry_.record_id)
275
+ assert_equal(0, @entries.size)
276
+ end
277
+
278
+
279
+ # Test multi-attribute
280
+
281
+ def test_multi_attribute
282
+ id_ = @logger.begin_record
283
+ @logger.append_attribute('event', 'click')
284
+ @logger.attribute('event', 'drag', :append, :INFO)
285
+ @logger.end_record
286
+ entry_ = @entries.dequeue
287
+ assert_equal(:begin_record, entry_.type)
288
+ assert_equal(id_, entry_.record_id)
289
+ entry_ = @entries.dequeue
290
+ assert_equal(:attribute, entry_.type)
291
+ assert_equal(@levels.get(:ANY), entry_.level)
292
+ assert_equal('sawmill', entry_.progname)
293
+ assert_equal(id_, entry_.record_id)
294
+ assert_equal(:append, entry_.operation)
295
+ assert_equal('event', entry_.key)
296
+ assert_equal('click', entry_.value)
297
+ entry_ = @entries.dequeue
298
+ assert_equal(:attribute, entry_.type)
299
+ assert_equal(@levels.get(:INFO), entry_.level)
300
+ assert_equal('sawmill', entry_.progname)
301
+ assert_equal(id_, entry_.record_id)
302
+ assert_equal(:append, entry_.operation)
303
+ assert_equal('event', entry_.key)
304
+ assert_equal('drag', entry_.value)
305
+ entry_ = @entries.dequeue
306
+ assert_equal(:end_record, entry_.type)
307
+ assert_equal(id_, entry_.record_id)
308
+ assert_equal(0, @entries.size)
309
+ end
310
+
311
+
312
+ end
313
+
314
+ end
315
+ end
@@ -0,0 +1,117 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Sawmill: tests on record processors
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2009 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+
35
+
36
+ require 'test/unit'
37
+ require ::File.expand_path("#{::File.dirname(__FILE__)}/../lib/sawmill.rb")
38
+
39
+
40
+ module Sawmill
41
+ module Tests # :nodoc:
42
+
43
+ class TestRecordProcessors < ::Test::Unit::TestCase # :nodoc:
44
+
45
+
46
+ def setup
47
+ @records = ::Sawmill::RecordProcessor::SimpleQueue.new
48
+ @levels = ::Sawmill::STANDARD_LEVELS
49
+ end
50
+
51
+
52
+ # Test a basic filter that checks the record ID
53
+
54
+ def test_basic_record_id_filter
55
+ processor_ = ::Sawmill::RecordProcessor::build do
56
+ If(FilterByRecordID('12345678'), @records)
57
+ end
58
+ @logger = ::Sawmill::Logger.new(:processor => ::Sawmill::RecordBuilder.new(processor_))
59
+ @logger.begin_record('asdf')
60
+ @logger.end_record
61
+ @logger.begin_record('12345678')
62
+ @logger.end_record
63
+ @logger.begin_record('qwerty')
64
+ @logger.end_record
65
+ assert_equal('12345678', @records.dequeue.record_id)
66
+ assert_equal(0, @records.size)
67
+ end
68
+
69
+
70
+ # Test a basic filter that checks an attribute
71
+
72
+ def test_basic_attribute_filter
73
+ processor_ = ::Sawmill::RecordProcessor::build do
74
+ If(FilterByAttributes('user' => 'daniel'), @records)
75
+ end
76
+ @logger = ::Sawmill::Logger.new(:processor => ::Sawmill::RecordBuilder.new(processor_))
77
+ @logger.begin_record('1')
78
+ @logger.attribute('user', 'bill')
79
+ @logger.end_record
80
+ @logger.begin_record('2')
81
+ @logger.attribute('user', 'daniel')
82
+ @logger.end_record
83
+ @logger.begin_record('3')
84
+ @logger.attribute('admin', 'daniel')
85
+ @logger.end_record
86
+ assert_equal('2', @records.dequeue.record_id)
87
+ assert_equal(0, @records.size)
88
+ end
89
+
90
+
91
+ # Test a basic filter that checks two attributes
92
+
93
+ def test_two_attributes_filter
94
+ processor_ = ::Sawmill::RecordProcessor::build do
95
+ If(FilterByAttributes('user' => 'daniel', 'type' => 'admin'), @records)
96
+ end
97
+ @logger = ::Sawmill::Logger.new(:processor => ::Sawmill::RecordBuilder.new(processor_))
98
+ @logger.begin_record('1')
99
+ @logger.attribute('user', 'bill')
100
+ @logger.attribute('type', 'admin')
101
+ @logger.end_record
102
+ @logger.begin_record('2')
103
+ @logger.attribute('user', 'daniel')
104
+ @logger.end_record
105
+ @logger.begin_record('3')
106
+ @logger.attribute('user', 'daniel')
107
+ @logger.attribute('type', 'admin')
108
+ @logger.end_record
109
+ assert_equal('3', @records.dequeue.record_id)
110
+ assert_equal(0, @records.size)
111
+ end
112
+
113
+
114
+ end
115
+
116
+ end
117
+ end
@@ -0,0 +1,206 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # Sawmill: tests on log record construction
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2009 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+
35
+
36
+ require 'test/unit'
37
+ require ::File.expand_path("#{::File.dirname(__FILE__)}/../lib/sawmill.rb")
38
+
39
+
40
+ module Sawmill
41
+ module Tests # :nodoc:
42
+
43
+ class TestRecords < ::Test::Unit::TestCase # :nodoc:
44
+
45
+
46
+ def setup
47
+ @records = ::Sawmill::RecordProcessor::SimpleQueue.new
48
+ @logger = ::Sawmill::Logger.new(:processor => ::Sawmill::RecordBuilder.new(@records))
49
+ @levels = ::Sawmill::STANDARD_LEVELS
50
+ end
51
+
52
+
53
+ # Test basic record creation
54
+
55
+ def test_basic_create
56
+ id_ = @logger.begin_record
57
+ @logger.end_record
58
+ record_ = @records.dequeue
59
+ assert(record_.started?)
60
+ assert(record_.complete?)
61
+ assert_equal(id_, record_.record_id)
62
+ assert_equal(0, record_.message_count)
63
+ assert_equal(2, record_.entry_count)
64
+ entries_ = record_.all_entries
65
+ assert_equal(:begin_record, entries_[0].type)
66
+ assert_equal(@levels.get(:ANY), entries_[0].level)
67
+ assert_equal('sawmill', entries_[0].progname)
68
+ assert_equal(id_, entries_[0].record_id)
69
+ assert_equal(:end_record, entries_[1].type)
70
+ assert_equal(@levels.get(:ANY), entries_[1].level)
71
+ assert_equal('sawmill', entries_[1].progname)
72
+ assert_equal(id_, entries_[1].record_id)
73
+ end
74
+
75
+
76
+ # Test record messages
77
+
78
+ def test_messages
79
+ id_ = @logger.begin_record
80
+ @logger.info('Hello 1')
81
+ @logger.error('rails'){ 'Hello 2' }
82
+ @logger.end_record
83
+ record_ = @records.dequeue
84
+ assert_equal(2, record_.message_count)
85
+ assert_equal(4, record_.entry_count)
86
+ meessages_ = record_.all_messages
87
+ assert_equal(:message, meessages_[0].type)
88
+ assert_equal(@levels.get(:INFO), meessages_[0].level)
89
+ assert_equal('sawmill', meessages_[0].progname)
90
+ assert_equal(id_, meessages_[0].record_id)
91
+ assert_equal('Hello 1', meessages_[0].message)
92
+ assert_equal(:message, meessages_[1].type)
93
+ assert_equal(@levels.get(:ERROR), meessages_[1].level)
94
+ assert_equal('rails', meessages_[1].progname)
95
+ assert_equal(id_, meessages_[1].record_id)
96
+ assert_equal('Hello 2', meessages_[1].message)
97
+ end
98
+
99
+
100
+ # Test record attributes
101
+
102
+ def test_attributes
103
+ id_ = @logger.begin_record
104
+ @logger.attribute('color', 'blue')
105
+ @logger.attribute('size', 'small')
106
+ @logger.attribute(:color, 'red')
107
+ @logger.end_record
108
+ record_ = @records.dequeue
109
+ assert_equal(0, record_.message_count)
110
+ assert_equal(5, record_.entry_count)
111
+ assert_equal(Set.new(['color', 'size']), Set.new(record_.attribute_keys))
112
+ assert_equal('small', record_.attribute('size'))
113
+ assert_equal('red', record_.attribute('color'))
114
+ end
115
+
116
+
117
+ # Test record multi-attributes
118
+
119
+ def test_multi_attributes
120
+ id_ = @logger.begin_record
121
+ @logger.append_attribute('color', 'blue')
122
+ @logger.append_attribute('size', 'small')
123
+ @logger.append_attribute(:color, 'red')
124
+ @logger.end_record
125
+ record_ = @records.dequeue
126
+ assert_equal(0, record_.message_count)
127
+ assert_equal(5, record_.entry_count)
128
+ assert_equal(Set.new(['color', 'size']), Set.new(record_.attribute_keys))
129
+ assert_equal(['small'], record_.attribute('size'))
130
+ assert_equal(['blue', 'red'], record_.attribute('color'))
131
+ end
132
+
133
+
134
+ # Test record decomposition
135
+
136
+ def _test_decompose
137
+ entries_ = ::Sawmill::EntryProcessor::SimpleQueue.new
138
+ id_ = @logger.begin_record
139
+ @logger.info('Hello 1')
140
+ @logger.attribute('color', 'blue')
141
+ @logger.attribute('size', 'small')
142
+ @logger.multi_attribute('shape', 'round')
143
+ @logger.error('rails'){ 'Hello 2' }
144
+ @logger.attribute(:color, 'red')
145
+ @logger.multi_attribute('shape', 'pointy')
146
+ @logger.end_record
147
+ record_ = @records.dequeue
148
+ assert_equal(9, record_.entry_count)
149
+ record_.decompose(entries_)
150
+
151
+ entry_ = entries_.dequeue
152
+ assert_equal(:begin_record, entry_.type)
153
+ assert_equal(@levels.get(:ANY), entry_.level)
154
+ assert_equal('sawmill', entry_.progname)
155
+ assert_equal(id_, entry_.record_id)
156
+
157
+ entry_ = entries_.dequeue
158
+ assert_equal(:message, entry_.type)
159
+ assert_equal(@levels.get(:INFO), entry_.level)
160
+ assert_equal('sawmill', entry_.progname)
161
+ assert_equal(id_, entry_.record_id)
162
+ assert_equal('Hello 1', entry_.message)
163
+
164
+ entry_ = entries_.dequeue
165
+ assert_equal(:attribute, entry_.type)
166
+ assert_equal('color', entry_.key)
167
+ assert_equal('blue', entry_.value)
168
+
169
+ entry_ = entries_.dequeue
170
+ assert_equal(:attribute, entry_.type)
171
+ assert_equal('size', entry_.key)
172
+ assert_equal('small', entry_.value)
173
+
174
+ entry_ = entries_.dequeue
175
+ assert_equal(:multi_attribute, entry_.type)
176
+ assert_equal('shape', entry_.key)
177
+ assert_equal('round', entry_.value)
178
+
179
+ entry_ = entries_.dequeue
180
+ assert_equal(:message, entry_.type)
181
+ assert_equal(@levels.get(:ERROR), entry_.level)
182
+ assert_equal('rails', entry_.progname)
183
+ assert_equal('Hello 2', entry_.message)
184
+
185
+ entry_ = entries_.dequeue
186
+ assert_equal(:attribute, entry_.type)
187
+ assert_equal('color', entry_.key)
188
+ assert_equal('red', entry_.value)
189
+
190
+ entry_ = entries_.dequeue
191
+ assert_equal(:multi_attribute, entry_.type)
192
+ assert_equal('shape', entry_.key)
193
+ assert_equal('pointy', entry_.value)
194
+
195
+ entry_ = entries_.dequeue
196
+ assert_equal(:end_record, entry_.type)
197
+ assert_equal(id_, entry_.record_id)
198
+
199
+ assert_equal(0, entries_.size)
200
+ end
201
+
202
+
203
+ end
204
+
205
+ end
206
+ end