mtn_log4r 1.1.11

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 (106) hide show
  1. checksums.yaml +15 -0
  2. data/doc/content/contact.html +22 -0
  3. data/doc/content/contribute.html +21 -0
  4. data/doc/content/index.html +90 -0
  5. data/doc/content/license.html +56 -0
  6. data/doc/content/manual.html +449 -0
  7. data/doc/dev/README.developers +55 -0
  8. data/doc/dev/checklist +23 -0
  9. data/doc/dev/things-to-do +5 -0
  10. data/doc/images/log4r-logo.png +0 -0
  11. data/doc/images/logo2.png +0 -0
  12. data/doc/log4r.css +111 -0
  13. data/doc/rdoc-log4r.css +696 -0
  14. data/doc/templates/main.html +147 -0
  15. data/examples/README +19 -0
  16. data/examples/ancestors.rb +53 -0
  17. data/examples/chainsaw_settings.xml +7 -0
  18. data/examples/customlevels.rb +34 -0
  19. data/examples/filelog.rb +25 -0
  20. data/examples/fileroll.rb +40 -0
  21. data/examples/gmail.rb +30 -0
  22. data/examples/gmail.yaml +95 -0
  23. data/examples/log4r_yaml.yaml +0 -0
  24. data/examples/logclient.rb +25 -0
  25. data/examples/logserver.rb +18 -0
  26. data/examples/moderate.xml +29 -0
  27. data/examples/moderateconfig.rb +66 -0
  28. data/examples/myformatter.rb +23 -0
  29. data/examples/outofthebox.rb +21 -0
  30. data/examples/rdoc-gen +2 -0
  31. data/examples/rrconfig.xml +63 -0
  32. data/examples/rrsetup.rb +42 -0
  33. data/examples/simpleconfig.rb +39 -0
  34. data/examples/syslogcustom.rb +52 -0
  35. data/examples/xmlconfig.rb +25 -0
  36. data/examples/yaml.rb +30 -0
  37. data/lib/log4r/GDC.rb +41 -0
  38. data/lib/log4r/MDC.rb +59 -0
  39. data/lib/log4r/NDC.rb +86 -0
  40. data/lib/log4r/base.rb +74 -0
  41. data/lib/log4r/config.rb +9 -0
  42. data/lib/log4r/configurator.rb +224 -0
  43. data/lib/log4r/formatter/formatter.rb +109 -0
  44. data/lib/log4r/formatter/log4jxmlformatter.rb +65 -0
  45. data/lib/log4r/formatter/patternformatter.rb +145 -0
  46. data/lib/log4r/lib/drbloader.rb +52 -0
  47. data/lib/log4r/lib/xmlloader.rb +24 -0
  48. data/lib/log4r/logevent.rb +28 -0
  49. data/lib/log4r/logger.rb +199 -0
  50. data/lib/log4r/loggerfactory.rb +89 -0
  51. data/lib/log4r/logserver.rb +28 -0
  52. data/lib/log4r/outputter/consoleoutputters.rb +18 -0
  53. data/lib/log4r/outputter/datefileoutputter.rb +117 -0
  54. data/lib/log4r/outputter/emailoutputter.rb +143 -0
  55. data/lib/log4r/outputter/fileoutputter.rb +56 -0
  56. data/lib/log4r/outputter/iooutputter.rb +55 -0
  57. data/lib/log4r/outputter/outputter.rb +134 -0
  58. data/lib/log4r/outputter/outputterfactory.rb +61 -0
  59. data/lib/log4r/outputter/rabbitoutputter.rb +70 -0
  60. data/lib/log4r/outputter/remoteoutputter.rb +40 -0
  61. data/lib/log4r/outputter/rollingfileoutputter.rb +234 -0
  62. data/lib/log4r/outputter/scribeoutputter.rb +37 -0
  63. data/lib/log4r/outputter/staticoutputter.rb +30 -0
  64. data/lib/log4r/outputter/syslogoutputter.rb +130 -0
  65. data/lib/log4r/outputter/udpoutputter.rb +53 -0
  66. data/lib/log4r/rdoc/GDC +14 -0
  67. data/lib/log4r/rdoc/MDC +16 -0
  68. data/lib/log4r/rdoc/NDC +41 -0
  69. data/lib/log4r/rdoc/configurator +243 -0
  70. data/lib/log4r/rdoc/emailoutputter +103 -0
  71. data/lib/log4r/rdoc/formatter +39 -0
  72. data/lib/log4r/rdoc/log4jxmlformatter +21 -0
  73. data/lib/log4r/rdoc/log4r +89 -0
  74. data/lib/log4r/rdoc/logger +175 -0
  75. data/lib/log4r/rdoc/logserver +85 -0
  76. data/lib/log4r/rdoc/outputter +108 -0
  77. data/lib/log4r/rdoc/patternformatter +128 -0
  78. data/lib/log4r/rdoc/scribeoutputter +16 -0
  79. data/lib/log4r/rdoc/syslogoutputter +29 -0
  80. data/lib/log4r/rdoc/win32eventoutputter +7 -0
  81. data/lib/log4r/rdoc/yamlconfigurator +20 -0
  82. data/lib/log4r/repository.rb +88 -0
  83. data/lib/log4r/staticlogger.rb +49 -0
  84. data/lib/log4r/version.rb +4 -0
  85. data/lib/log4r/yamlconfigurator.rb +198 -0
  86. data/lib/log4r.rb +18 -0
  87. data/tests/README +10 -0
  88. data/tests/testGDC.rb +24 -0
  89. data/tests/testMDC.rb +40 -0
  90. data/tests/testNDC.rb +25 -0
  91. data/tests/test_helper.rb +12 -0
  92. data/tests/testall.rb +6 -0
  93. data/tests/testbase.rb +48 -0
  94. data/tests/testchainsaw.rb +42 -0
  95. data/tests/testconf.xml +37 -0
  96. data/tests/testcustom.rb +30 -0
  97. data/tests/testformatter.rb +31 -0
  98. data/tests/testlogger.rb +200 -0
  99. data/tests/testoutputter.rb +143 -0
  100. data/tests/testpatternformatter.rb +76 -0
  101. data/tests/testthreads.rb +31 -0
  102. data/tests/testxmlconf.rb +48 -0
  103. data/tests/testyaml.rb +39 -0
  104. data/tests/testyaml_arrays.yaml +25 -0
  105. data/tests/testyaml_injection.yaml +22 -0
  106. metadata +193 -0
@@ -0,0 +1,200 @@
1
+ require 'test_helper'
2
+
3
+ class MyFormatter1 < Log4r::Formatter
4
+ def format(event)
5
+ return "MyFormatter1\n"
6
+ end
7
+ end
8
+
9
+ class MyFormatter2 < Log4r::Formatter
10
+ def format(event)
11
+ return "MyFormatter2\n"
12
+ end
13
+ end
14
+
15
+ class TestLogger < TestCase
16
+ include Log4r
17
+
18
+ def test_root
19
+ l1 = Logger.root
20
+ l2 = Logger['root']
21
+ l3 = Logger.global
22
+ assert(l1 == l2, "RootLogger wasn't singleton!")
23
+ assert(l1 == l3)
24
+ assert(l1.is_root? == true, "is_root? not working")
25
+ assert(l1.parent == nil, "Root's parent wasn't nil!")
26
+ end
27
+ def test_validation
28
+ assert_raise(ArgumentError) { Logger.new }
29
+ assert_nothing_raised { Logger.new('validate', nil) }
30
+ end
31
+ def test_all_off
32
+ l = Logger.new("create_method")
33
+ l.level = WARN
34
+ assert(l.debug? == false)
35
+ assert(l.info? == false)
36
+ assert(l.warn? == true)
37
+ assert(l.error? == true)
38
+ assert(l.fatal? == true)
39
+ assert(l.off? == false)
40
+ assert(l.all? == false)
41
+ l.level = OFF
42
+ assert(l.off? == true)
43
+ assert(l.all? == false)
44
+ l.level = ALL
45
+ assert(l.off? == false)
46
+ assert(l.all? == true)
47
+ end
48
+ def test_add_outputters
49
+ StdoutOutputter.new('fake1')
50
+ StdoutOutputter.new('fake2')
51
+ a = Logger.new("add")
52
+ assert_raise(TypeError) { a.add 'bogus' }
53
+ assert_raise(TypeError) { a.add Class }
54
+ assert_raise(TypeError) { a.add 'fake1', Class }
55
+ assert_nothing_raised { a.add 'fake1', 'fake2' }
56
+ end
57
+ def test_repository
58
+ assert_raise(NameError) { Logger.get('bogusbogus') }
59
+ assert_nothing_raised { Logger['bogusbogus'] }
60
+ end
61
+ def test_heiarchy
62
+ a = Logger.new("a")
63
+ a.additive = true
64
+ assert(a.name == "a", "name wasn't set properly")
65
+ assert(a.path == "", "path wasn't set properly")
66
+ assert(a.level == Logger.root.level, "didn't inherit root's level")
67
+ assert(a.parent == Logger.root)
68
+ a.level = WARN
69
+ b = Logger.new("a::b")
70
+ assert(b.name == "b", "name wasn't set properly")
71
+ assert(b.path == "a", "path wasn't set properly")
72
+ assert(b.level == a.level, "didn't inherit parent's level")
73
+ assert(b.parent == a, "parent wasn't what is expected")
74
+ c = Logger.new("a::b::c")
75
+ assert(Logger["a::b::c"] == c)
76
+ assert(c.name == "c", "name wasn't set properly")
77
+ assert(c.path == "a::b", "path wasn't set properly")
78
+ assert(c.level == b.level, "didn't inherit parent's level")
79
+ assert(c.parent == b, "parent wasn't what is expected")
80
+ d = Logger.new("a::d")
81
+ assert(Logger["a::d"] == d)
82
+ assert(d.name == "d", "name wasn't set properly")
83
+ assert(d.path == "a", "path wasn't set properly")
84
+ assert(d.level == a.level, "didn't inherit parent's level")
85
+ assert(d.parent == a, "parent wasn't what is expected")
86
+ assert_raise(ArgumentError) { Logger.new("::a") }
87
+ end
88
+ def test_undefined_parents
89
+ a = Logger.new 'has::no::real::parents::me'
90
+ assert(a.parent == Logger.root)
91
+ b = Logger.new 'has::no::real::parents::me::child'
92
+ assert(b.parent == a)
93
+ c = Logger.new 'has::no::real::parents::metoo'
94
+ assert(c.parent == Logger.root)
95
+ p = Logger.new 'has::no::real::parents'
96
+ assert(p.parent == Logger.root)
97
+ assert(a.parent == p)
98
+ assert(b.parent == a)
99
+ assert(c.parent == p)
100
+ Logger.each{|fullname, logger|
101
+ if logger != a and logger != c
102
+ assert(logger.parent != p)
103
+ end
104
+ }
105
+ end
106
+ def test_levels
107
+ l = Logger.new("levels", WARN)
108
+ assert(l.level == WARN, "level wasn't changed")
109
+ assert(l.fatal? == true)
110
+ assert(l.error? == true)
111
+ assert(l.warn? == true)
112
+ assert(l.info? == false)
113
+ assert(l.debug? == false)
114
+ l.debug "debug message should NOT show up"
115
+ l.info "info message should NOT show up"
116
+ l.warn "warn messge should show up. 3 total"
117
+ l.error "error messge should show up. 3 total"
118
+ l.fatal "fatal messge should show up. 3 total"
119
+ l.level = ERROR
120
+ assert(l.level == ERROR, "level wasn't changed")
121
+ assert(l.fatal? == true)
122
+ assert(l.error? == true)
123
+ assert(l.warn? == false)
124
+ assert(l.info? == false)
125
+ assert(l.debug? == false)
126
+ l.debug "debug message should NOT show up"
127
+ l.info "info message should NOT show up"
128
+ l.warn "warn messge should NOT show up."
129
+ l.error "error messge should show up. 2 total"
130
+ l.fatal "fatal messge should show up. 2 total"
131
+ l.level = WARN
132
+ end
133
+ def test_log_blocks
134
+ l = Logger.new 'logblocks'
135
+ l.level = WARN
136
+ l.add(Outputter.stdout)
137
+ assert_nothing_raised {
138
+ l.debug { puts "should not show up"; "LOGBLOCKS" }
139
+ l.fatal { puts "should show up"; "LOGBLOCKS" }
140
+ l.fatal { nil }
141
+ l.fatal {}
142
+ }
143
+ end
144
+ def test_heiarchial_logging
145
+ a = Logger.new("one")
146
+ a.add(StdoutOutputter.new 'so1')
147
+ b = Logger.new("one::two")
148
+ b.add(StdoutOutputter.new 'so2')
149
+ c = Logger.new("one::two::three")
150
+ c.add(StdoutOutputter.new 'so3')
151
+ d = Logger.new("one::two::three::four")
152
+ d.add(StdoutOutputter.new 'so4')
153
+ d.additive = false
154
+ e = Logger.new("one::two::three::four::five")
155
+ e.add(StdoutOutputter.new 'so5')
156
+
157
+ a.fatal "statement from a should show up once"
158
+ b.fatal "statement from b should show up twice"
159
+ c.fatal "statement from c should show up thrice"
160
+ d.fatal "statement from d should show up once"
161
+ e.fatal "statement from e should show up twice"
162
+ end
163
+ def test_multi_outs
164
+ f1 = FileOutputter.new('f1', :filename => "./junk/tmp1.log", :level=>ALL)
165
+ f2 = FileOutputter.new('f2', :filename => "./junk/tmp2.log", :level=>DEBUG)
166
+ f3 = FileOutputter.new('f3', :filename => "./junk/tmp3.log", :level=>ERROR)
167
+ f4 = FileOutputter.new('f4', :filename => "./junk/tmp4.log", :level=>FATAL)
168
+
169
+ l = Logger.new("multi")
170
+ l.add(f1, f3, f4)
171
+
172
+ a = Logger.new("multi::multi2")
173
+ a.level = ERROR
174
+ a.add(f2, f4)
175
+
176
+ l.debug "debug test_multi_outputters"
177
+ l.info "info test_multi_outputters"
178
+ l.warn "warn test_multi_outputters"
179
+ l.error "error test_multi_outputters"
180
+ l.fatal "fatal test_multi_outputters"
181
+
182
+ a.debug "debug test_multi_outputters"
183
+ a.info "info test_multi_outputters"
184
+ a.warn "warn test_multi_outputters"
185
+ a.error "error test_multi_outputters"
186
+ a.fatal "fatal test_multi_outputters"
187
+
188
+ f1.close; f2.close; f3.close; f4.close
189
+ end
190
+ def test_custom_formatter
191
+ l = Logger.new('custom_formatter')
192
+ o = StdoutOutputter.new('formatter'=>MyFormatter1.new)
193
+ l.add o
194
+ l.error "try myformatter1"
195
+ l.fatal "try myformatter1"
196
+ o.formatter = MyFormatter2.new
197
+ l.error "try formatter2"
198
+ l.fatal "try formatter2"
199
+ end
200
+ end
@@ -0,0 +1,143 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class TestOutputter < TestCase
5
+ include Log4r
6
+
7
+ def test_validation
8
+ assert_raise(ArgumentError) { Outputter.new }
9
+ assert_raise(ArgumentError) { Outputter.new 'fonda', :level=>-10}
10
+ assert_raise(TypeError) { Outputter.new 'fonda', :formatter=>-10}
11
+ end
12
+ def test_io
13
+ assert_nothing_raised {
14
+ IOOutputter.new('foo3', $stdout)
15
+ IOOutputter.new('foo4', $stderr)
16
+ }
17
+ f = File.new("junk/tmpx.log", "w")
18
+ o = IOOutputter.new('asdf', f)
19
+ o.close
20
+ assert(f.closed? == true)
21
+ assert(o.level == OFF)
22
+ end
23
+ def test_repository
24
+ assert( Outputter['foo3'].class == IOOutputter )
25
+ assert( Outputter['foo4'].class == IOOutputter )
26
+ assert( Outputter['asdf'].class == IOOutputter )
27
+ end
28
+ def test_validation_and_creation
29
+ assert_nothing_raised {
30
+ StdoutOutputter.new('out', 'level'=>DEBUG)
31
+ FileOutputter.new('file', 'filename'=>'junk/test', :trunc=>true)
32
+ }
33
+ a = StdoutOutputter.new 'out2'
34
+ assert(a.level == Logger.root.level)
35
+ assert(a.formatter.class == DefaultFormatter)
36
+ b = StdoutOutputter.new('ook', :level => DEBUG, :formatter => Formatter)
37
+ assert(b.level == DEBUG)
38
+ assert(b.formatter.class == Formatter)
39
+ c = StdoutOutputter.new('akk', :formatter => Formatter)
40
+ assert(c.level == Logger.root.level)
41
+ assert(c.formatter.class == Formatter)
42
+ c = StderrOutputter.new('iikk', :level => OFF)
43
+ assert(c.level == OFF)
44
+ assert(c.formatter.class == DefaultFormatter)
45
+ o = StderrOutputter.new 'ik'
46
+ assert_nothing_raised(TypeError) { o.formatter = DefaultFormatter }
47
+ assert(o.formatter.class == DefaultFormatter)
48
+ end
49
+ # test the resource= bounds
50
+ def test_boundaries
51
+ o = StderrOutputter.new('ak', :formatter => Formatter)
52
+ assert_raise(TypeError) { o.formatter = nil }
53
+ assert_raise(TypeError) { o.formatter = String }
54
+ assert_raise(TypeError) { o.formatter = "bogus" }
55
+ assert_raise(TypeError) { o.formatter = -3 }
56
+ # the formatter should be preserved
57
+ assert(o.formatter.class == Formatter)
58
+ end
59
+ def test_file
60
+ assert_raise(TypeError) { FileOutputter.new 'f' }
61
+ assert_raise(TypeError) { FileOutputter.new('fa', :filename => DEBUG) }
62
+ assert_raise(TypeError) { FileOutputter.new('fo', :filename => nil) }
63
+ assert_nothing_raised {
64
+ FileOutputter.new('fi', :filename => './junk/tmp')
65
+ FileOutputter.new('fum', :filename=>'./junk/tmp', :trunc => "true")
66
+ }
67
+ fo = FileOutputter.new('food', :filename => './junk/tmp', :trunc => false)
68
+ assert(fo.trunc == false)
69
+ assert(fo.filename == './junk/tmp')
70
+ assert(fo.closed? == false)
71
+ fo.close
72
+ assert(fo.closed? == true)
73
+ assert(fo.level == OFF)
74
+ end
75
+ # test the dynamic definition of outputter log messages
76
+ def test_log_methods
77
+ o = StderrOutputter.new('so1', :level => WARN )
78
+ # test to see if all of the methods are defined
79
+ for mname in LNAMES
80
+ next if mname == 'OFF' || mname == 'ALL'
81
+ assert_respond_to(o, mname.downcase.to_sym, "Test respond to #{mname.to_s}")
82
+ end
83
+ return # cuz the rest is borked
84
+ # we rely on BasicFormatter's inability to reference a nil Logger to test
85
+ # the log methods. Everything from WARN to FATAL should choke.
86
+ event = LogEvent.new(nil, nil, nil, nil)
87
+ assert_nothing_raised { o.debug event }
88
+ assert_nothing_raised { o.info event }
89
+ assert_raise(NameError) { o.warn event }
90
+ assert_raise(NameError) { o.error event }
91
+ assert_raise(NameError) { o.fatal event }
92
+ # now let's dynamically change the level and repeat
93
+ o.level = ERROR
94
+ assert_nothing_raised { o.debug event}
95
+ assert_nothing_raised { o.info event}
96
+ assert_nothing_raised { o.warn event}
97
+ assert_raise(NameError) { o.error event}
98
+ assert_raise(NameError) { o.fatal event}
99
+ end
100
+ def test_only_at_validation
101
+ o = StdoutOutputter.new 'so2'
102
+ assert_raise(ArgumentError) { o.only_at }
103
+ assert_raise(ArgumentError) { o.only_at ALL }
104
+ assert_raise(TypeError) { o.only_at OFF }
105
+ assert_nothing_raised { o.only_at DEBUG, ERROR }
106
+ return # cuz the rest is borked
107
+ # test the methods as before
108
+ event = LogEvent.new(nil,nil,nil,nil)
109
+ assert_raise(NameError) { o.debug event}
110
+ assert_raise(NameError) { o.error event}
111
+ assert_nothing_raised { o.warn event}
112
+ assert_nothing_raised { o.info event}
113
+ assert_nothing_raised { o.fatal event}
114
+ end
115
+ if defined?( Encoding )
116
+ # tests that files are opened in binary mode
117
+ def test_file_encoding
118
+ Encoding.default_internal = Encoding::UTF_8
119
+ File.open( './junk/tmp2', 'w' ) { |f| f.write( 'scheiß encoding' ) }
120
+ fenc = FileOutputter.new('fenc', :filename => './junk/tmp2')
121
+ event = LogEvent.new(1, Logger.root, nil, 'scheiß encoding'.force_encoding('ASCII-8BIT'))
122
+ assert_nothing_raised(Encoding::UndefinedConversionError) do
123
+ fenc.debug event
124
+ end
125
+ end
126
+ end
127
+ def broken_test_threading
128
+ class << self
129
+ def log_work
130
+ o = StdoutOutputter.new 'so2'
131
+ assert_nothing_raised { o.only_at DEBUG, ERROR }
132
+ Thread.current().exit()
133
+ end
134
+ def log_thread_start
135
+ t = Thread.new(log_work)
136
+ t.join
137
+ end
138
+ end
139
+
140
+ ts = Thread.new(log_thread_start)
141
+ ts.join
142
+ end
143
+ end
@@ -0,0 +1,76 @@
1
+ require 'test_helper'
2
+
3
+ class TestPatternFormatter < TestCase
4
+ include Log4r
5
+
6
+ def test_pattern
7
+ l = Logger.new 'test::this::that'
8
+ l.trace = true
9
+ o = StdoutOutputter.new 'test'
10
+ l.add o
11
+ assert_nothing_raised {
12
+ f = PatternFormatter.new :pattern=> "'%t' T-'%T' %d %6l [%C]%c %% %-40.30M"
13
+ #:date_pattern=> "%Y"
14
+ #:date_method => :usec
15
+ Outputter['test'].formatter = f
16
+ l.debug "And this?"
17
+ l.info "How's this?"
18
+ l.error "and a really freaking huge line which we hope will be trimmed?"
19
+ e = ArgumentError.new("something barfed")
20
+ e.set_backtrace Array.new(5, "trace junk at thisfile.rb 154")
21
+ l.fatal e
22
+ l.info [1, 3, 5]
23
+ }
24
+ end
25
+
26
+ def test_ndc
27
+ l = Logger.new 'test::this::that::other'
28
+ l.trace = true
29
+ o = StdoutOutputter.new 'testy'
30
+ l.add o
31
+ f = PatternFormatter.new :pattern=> "%d %6l [%C]%c {%x} %% %-40.30M"
32
+ #:date_pattern=> "%Y"
33
+ #:date_method => :usec
34
+ Outputter['testy'].formatter = f
35
+
36
+ l.info "no NDC"
37
+ NDC.push("start")
38
+ l.info "start NDC"
39
+ NDC.push("finish")
40
+ l.info "start finish NDC"
41
+ NDC.pop()
42
+ l.info "start NDC"
43
+ NDC.remove()
44
+ l.info "no NDC"
45
+ end
46
+
47
+ def test_gdc
48
+ l = Logger.new 'test::this::that::other'
49
+ l.trace = true
50
+ o = StdoutOutputter.new 'testy'
51
+ l.add o
52
+ f = PatternFormatter.new :pattern=> "%d %6l [%C]%c {%g} %% %-40.30M"
53
+ #:date_pattern=> "%Y"
54
+ #:date_method => :usec
55
+ Outputter['testy'].formatter = f
56
+
57
+ l.info "GDC default"
58
+ GDC.set("non-default")
59
+ l.info "GDC non-default"
60
+ end
61
+
62
+ def test_mdc
63
+ l = Logger.new 'test::this::that::other'
64
+ l.trace = true
65
+ o = StdoutOutputter.new 'testy'
66
+ l.add o
67
+ f = PatternFormatter.new :pattern=> "%d %6l [%C]%c {%X{user}} %% %-40.30M"
68
+ #:date_pattern=> "%Y"
69
+ #:date_method => :usec
70
+ Outputter['testy'].formatter = f
71
+
72
+ l.info "no user"
73
+ MDC.put("user","colbygk")
74
+ l.info "user colbygk"
75
+ end
76
+ end
@@ -0,0 +1,31 @@
1
+ # $Id$
2
+ # Test guts sent in by chetreddy bug #27184
3
+ #
4
+ # Note: this test won't always catch a threading problem, as it
5
+ # relies on a brute force approach. NUM_THREADS can be increased
6
+ # to stress the system longer and therefore increasing the chance
7
+ # of exposing a threading issue, however, it is not a definitive
8
+ # test.
9
+ #
10
+
11
+ require 'test_helper'
12
+
13
+ class TestThreads < TestCase
14
+ include Log4r
15
+
16
+ NUMTHREADS = 1000
17
+
18
+ def test_threads
19
+
20
+ assert_nothing_raised do
21
+ (0..NUMTHREADS).map do |i|
22
+ Thread.new do
23
+ Thread.current[:logger] = Log4r::Logger.new "Hello #{i}"
24
+ Thread.current[:logger].outputters = [StdoutOutputter.new("log4r#{i}")]
25
+ Thread.current[:logger].outputters.each { |j| j.flush }
26
+ Thread.current.exit()
27
+ end
28
+ end.each do |thr| thr.join end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ One=<<-EOX
4
+ <log4r_config><pre_config><custom_levels> Foo </custom_levels>
5
+ </pre_config></log4r_config>
6
+ EOX
7
+ Two=<<-EOX
8
+ <log4r_config><pre_config><global level="DEBUG"/></pre_config></log4r_config>
9
+ EOX
10
+ Three=<<-EOX
11
+ <log4r_config><pre_config><custom_levels>Foo</custom_levels>
12
+ <global level="Foo"/></pre_config>
13
+ </log4r_config>
14
+ EOX
15
+
16
+ # must be run independently
17
+ class TestXmlConf < TestCase
18
+ include Log4r
19
+
20
+ def test_load1
21
+ Configurator.load_xml_string(One)
22
+ assert_nothing_raised{
23
+ assert(Foo == 1)
24
+ assert(Logger.global.level == ALL)
25
+ }
26
+ end
27
+ def test_load2
28
+ Configurator.load_xml_string(Two)
29
+ assert_nothing_raised{
30
+ assert(Logger.global.level == DEBUG)
31
+ }
32
+ end
33
+ def test_load3
34
+ Configurator.load_xml_string(Three)
35
+ assert_nothing_raised{
36
+ assert(Foo == 1)
37
+ assert(Logger.global.level == Foo)
38
+ }
39
+ end
40
+ def test_load4
41
+ assert_nothing_raised {
42
+ Configurator['logpath'] = '.'
43
+ Configurator.load_xml_file "testconf.xml"
44
+ a = Logger['first::second']
45
+ a.bing "what the heck"
46
+ }
47
+ end
48
+ end
data/tests/testyaml.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ # Define a custom outputter that allows arrays in configuration hash
4
+ module Log4r
5
+ class TestYamlOutputter < Outputter
6
+ # expose array parameter
7
+ attr_reader :array_param
8
+
9
+ def initialize(name, hash = {})
10
+ @array_param = hash['array_param']
11
+ end
12
+ end
13
+ end
14
+
15
+
16
+ class TestYaml < TestCase
17
+ include Log4r
18
+
19
+ def setup
20
+ @cfg = YamlConfigurator # shorthand
21
+ @cfg['CUSTOM_DOMAIN'] = 'bar.com'
22
+ end
23
+
24
+ def test_injection
25
+ assert_nothing_raised("Exception injected") do
26
+ @cfg.load_yaml_file(File.join(File.dirname(__FILE__),'testyaml_injection.yaml'))
27
+ end
28
+ end
29
+
30
+ def test_arrays
31
+ assert_nothing_raised("Parser couldn't handle arrays in YAML") do
32
+ @cfg.load_yaml_file(File.join(File.dirname(__FILE__),'testyaml_arrays.yaml'))
33
+ end
34
+ log = Logger['mylogger']
35
+ assert_instance_of(Array, log.outputters.first.array_param, 'Array not loaded properly from YAML')
36
+ assert_equal('wilma@bar.com', log.outputters.first.array_param[2], '#{}-style parameter interpolation doesn\'t work properly in arrays')
37
+ end
38
+ end
39
+
@@ -0,0 +1,25 @@
1
+ log4r_config:
2
+
3
+ # define all loggers ...
4
+ loggers:
5
+ - name : mylogger
6
+ level : INFO
7
+ additive : 'false'
8
+ trace : 'false'
9
+ outputters:
10
+ - testyaml
11
+
12
+ # define all outputters (incl. formatters)
13
+ outputters:
14
+ - type : TestYamlOutputter
15
+ name : testyaml
16
+ level : INFO
17
+ array_param:
18
+ - fred@foo.com
19
+ - barney@foo.com
20
+ - 'wilma@#{CUSTOM_DOMAIN}'
21
+ formatter:
22
+ date_pattern: '%y%m%d %H:%M:%S'
23
+ pattern : '%d %l: %m '
24
+ type : PatternFormatter
25
+
@@ -0,0 +1,22 @@
1
+ log4r_config:
2
+
3
+ # define all loggers ...
4
+ loggers:
5
+ - name : mylogger
6
+ level : INFO
7
+ additive : 'false'
8
+ trace : 'false'
9
+ outputters:
10
+ - stderr
11
+
12
+ # define all outputters (incl. formatters)
13
+ outputters:
14
+ - type : StderrOutputter
15
+ name : stderr
16
+ level : INFO
17
+ crash : "'; raise Exception #"
18
+ formatter:
19
+ date_pattern: '%y%m%d %H:%M:%S'
20
+ pattern : '%d %l: %m '
21
+ type : PatternFormatter
22
+