-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
tagged_logging_test.rb
229 lines (187 loc) · 6.87 KB
/
tagged_logging_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# frozen_string_literal: true
require_relative "abstract_unit"
require "active_support/logger"
require "active_support/tagged_logging"
class TaggedLoggingTest < ActiveSupport::TestCase
class MyLogger < ::ActiveSupport::Logger
def flush(*)
info "[FLUSHED]"
end
end
setup do
@output = StringIO.new
@logger = ActiveSupport::TaggedLogging.new(MyLogger.new(@output))
end
test "sets logger.formatter if missing and extends it with a tagging API" do
logger = Logger.new(StringIO.new)
assert_nil logger.formatter
other_logger = ActiveSupport::TaggedLogging.new(logger)
assert_not_nil other_logger.formatter
assert_respond_to other_logger.formatter, :tagged
end
test "tagged once" do
@logger.tagged("BCX") { @logger.info "Funky time" }
assert_equal "[BCX] Funky time\n", @output.string
end
test "tagged twice" do
@logger.tagged("BCX") { @logger.tagged("Jason") { @logger.info "Funky time" } }
assert_equal "[BCX] [Jason] Funky time\n", @output.string
end
test "tagged thrice at once" do
@logger.tagged("BCX", "Jason", "New") { @logger.info "Funky time" }
assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
end
test "tagged are flattened" do
@logger.tagged("BCX", %w(Jason New)) { @logger.info "Funky time" }
assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
end
test "push and pop tags directly" do
assert_equal %w(A B C), @logger.push_tags("A", ["B", " ", ["C"]])
@logger.info "a"
assert_equal %w(C), @logger.pop_tags
@logger.info "b"
assert_equal %w(B), @logger.pop_tags(1)
@logger.info "c"
assert_equal [], @logger.clear_tags!
@logger.info "d"
assert_equal "[A] [B] [C] a\n[A] [B] b\n[A] c\nd\n", @output.string
end
test "does not strip message content" do
@logger.info " Hello"
assert_equal " Hello\n", @output.string
end
test "provides access to the logger instance" do
@logger.tagged("BCX") { |logger| logger.info "Funky time" }
assert_equal "[BCX] Funky time\n", @output.string
end
test "tagged once with blank and nil" do
@logger.tagged(nil, "", "New") { @logger.info "Funky time" }
assert_equal "[New] Funky time\n", @output.string
end
test "keeps each tag in their own thread" do
@logger.tagged("BCX") do
Thread.new do
@logger.info "Dull story"
@logger.tagged("OMG") { @logger.info "Cool story" }
end.join
@logger.info "Funky time"
end
assert_equal "Dull story\n[OMG] Cool story\n[BCX] Funky time\n", @output.string
end
test "keeps each tag in their own thread even when pushed directly" do
Thread.new do
@logger.push_tags("OMG")
@logger.info "Cool story"
end.join
@logger.info "Funky time"
assert_equal "[OMG] Cool story\nFunky time\n", @output.string
end
test "keeps each tag in their own instance" do
other_output = StringIO.new
other_logger = ActiveSupport::TaggedLogging.new(MyLogger.new(other_output))
@logger.tagged("OMG") do
other_logger.tagged("BCX") do
@logger.info "Cool story"
other_logger.info "Funky time"
end
end
assert_equal "[OMG] Cool story\n", @output.string
assert_equal "[BCX] Funky time\n", other_output.string
end
test "does not share the same formatter instance of the original logger" do
other_logger = ActiveSupport::TaggedLogging.new(@logger)
@logger.tagged("OMG") do
other_logger.tagged("BCX") do
@logger.info "Cool story"
other_logger.info "Funky time"
end
end
assert_equal "[OMG] Cool story\n[BCX] Funky time\n", @output.string
end
test "cleans up the taggings on flush" do
@logger.tagged("BCX") do
Thread.new do
@logger.tagged("OMG") do
@logger.flush
@logger.info "Cool story"
end
end.join
end
assert_equal "[FLUSHED]\nCool story\n", @output.string
end
test "mixed levels of tagging" do
@logger.tagged("BCX") do
@logger.tagged("Jason") { @logger.info "Funky time" }
@logger.info "Junky time!"
end
assert_equal "[BCX] [Jason] Funky time\n[BCX] Junky time!\n", @output.string
end
end
class TaggedLoggingWithoutBlockTest < ActiveSupport::TestCase
setup do
@output = StringIO.new
@logger = ActiveSupport::TaggedLogging.new(Logger.new(@output))
end
test "tagged once" do
@logger.tagged("BCX").info "Funky time"
assert_equal "[BCX] Funky time\n", @output.string
end
test "tagged twice" do
@logger.tagged("BCX").tagged("Jason").info "Funky time"
assert_equal "[BCX] [Jason] Funky time\n", @output.string
end
test "tagged thrice at once" do
@logger.tagged("BCX", "Jason", "New").info "Funky time"
assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
end
test "tagged are flattened" do
@logger.tagged("BCX", %w(Jason New)).info "Funky time"
assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
end
test "tagged once with blank and nil" do
@logger.tagged(nil, "", "New").info "Funky time"
assert_equal "[New] Funky time\n", @output.string
end
test "shares tags across threads" do
logger = @logger.tagged("BCX")
Thread.new do
logger.info "Dull story"
logger.tagged("OMG").info "Cool story"
end.join
logger.info "Funky time"
assert_equal "[BCX] Dull story\n[BCX] [OMG] Cool story\n[BCX] Funky time\n", @output.string
end
test "keeps each tag in their own instance" do
other_output = StringIO.new
other_logger = ActiveSupport::TaggedLogging.new(Logger.new(other_output))
tagged_logger = @logger.tagged("OMG")
other_tagged_logger = other_logger.tagged("BCX")
tagged_logger.info "Cool story"
other_tagged_logger.info "Funky time"
assert_equal "[OMG] Cool story\n", @output.string
assert_equal "[BCX] Funky time\n", other_output.string
end
test "does not share the same formatter instance of the original logger" do
other_logger = ActiveSupport::TaggedLogging.new(@logger)
tagged_logger = @logger.tagged("OMG")
other_tagged_logger = other_logger.tagged("BCX")
tagged_logger.info "Cool story"
other_tagged_logger.info "Funky time"
assert_equal "[OMG] Cool story\n[BCX] Funky time\n", @output.string
end
test "mixed levels of tagging" do
logger = @logger.tagged("BCX")
logger.tagged("Jason").info "Funky time"
logger.info "Junky time!"
assert_equal "[BCX] [Jason] Funky time\n[BCX] Junky time!\n", @output.string
end
test "keeps broadcasting functionality" do
broadcast_output = StringIO.new
broadcast_logger = ActiveSupport::TaggedLogging.new(Logger.new(broadcast_output))
@logger.extend(ActiveSupport::Logger.broadcast(broadcast_logger))
tagged_logger = @logger.tagged("OMG")
tagged_logger.info "Broadcasting..."
assert_equal "[OMG] Broadcasting...\n", @output.string
assert_equal "[OMG] Broadcasting...\n", broadcast_output.string
end
end