proxygen
ConfigUpdateTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (ConfigUpdate, updateLogLevels)
 
 TEST (ConfigUpdate, updateConfig)
 
 TEST (ConfigUpdate, getConfigAnonymousHandlers)
 
 TEST (ConfigUpdate, getFullConfig)
 

Function Documentation

TEST ( ConfigUpdate  ,
updateLogLevels   
)

Definition at line 54 of file ConfigUpdateTest.cpp.

References folly::DBG, folly::DBG5, EXPECT_EQ, folly::kDefaultLogLevel, folly::MAX_LEVEL, folly::parseLogConfig(), folly::LoggerDB::TESTING, and folly::WARN.

54  {
55  LoggerDB db{LoggerDB::TESTING};
56  db.updateConfig(parseLogConfig("foo.bar=dbg5"));
57  EXPECT_EQ(LogLevel::DBG5, db.getCategory("foo.bar")->getLevel());
58  EXPECT_EQ(LogLevel::DBG5, db.getCategory("foo.bar")->getEffectiveLevel());
59  EXPECT_EQ(LogLevel::MAX_LEVEL, db.getCategory("foo")->getLevel());
60  EXPECT_EQ(kDefaultLogLevel, db.getCategory("foo")->getEffectiveLevel());
61  EXPECT_EQ(kDefaultLogLevel, db.getCategory("")->getLevel());
62  EXPECT_EQ(kDefaultLogLevel, db.getCategory("")->getEffectiveLevel());
63 
64  EXPECT_EQ(LogLevel::MAX_LEVEL, db.getCategory("foo.bar.test")->getLevel());
65  EXPECT_EQ(
66  LogLevel::DBG5, db.getCategory("foo.bar.test")->getEffectiveLevel());
67 
68  db.updateConfig(
69  parseLogConfig("sys=warn,foo.test=debug,foo.test.stuff=warn"));
70  EXPECT_EQ(LogLevel::WARN, db.getCategory("sys")->getLevel());
71  EXPECT_EQ(kDefaultLogLevel, db.getCategory("sys")->getEffectiveLevel());
72  EXPECT_EQ(LogLevel::DBG, db.getCategory("foo.test")->getLevel());
73  EXPECT_EQ(LogLevel::DBG, db.getCategory("foo.test")->getEffectiveLevel());
74  EXPECT_EQ(LogLevel::WARN, db.getCategory("foo.test.stuff")->getLevel());
75  EXPECT_EQ(
76  LogLevel::DBG, db.getCategory("foo.test.stuff")->getEffectiveLevel());
77  EXPECT_EQ(LogLevel::DBG5, db.getCategory("foo.bar")->getEffectiveLevel());
78 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogConfig parseLogConfig(StringPiece value)
void updateConfig(const LogConfig &config)
Definition: LoggerDB.cpp:395
constexpr LogLevel kDefaultLogLevel
Definition: LogLevel.h:102
TEST ( ConfigUpdate  ,
updateConfig   
)

Definition at line 80 of file ConfigUpdateTest.cpp.

References folly::DBG2, folly::ERR, EXPECT_EQ, EXPECT_FALSE, EXPECT_THAT, EXPECT_THROW_RE, EXPECT_TRUE, folly::INFO, folly::MAX_LEVEL, folly::parseLogConfig(), folly::LoggerDB::TESTING, testing::UnorderedElementsAre(), and folly::WARN.

80  {
81  LoggerDB db{LoggerDB::TESTING};
83  std::make_unique<TestLogHandlerFactory>("handlerA"));
84  db.registerHandlerFactory(
85  std::make_unique<TestLogHandlerFactory>("handlerB"));
86  EXPECT_EQ(parseLogConfig(".:=INFO:"), db.getConfig());
87 
88  // Create some categories that aren't affected by our config updates below,
89  // just to ensure that they don't show up in getConfig() results since they
90  // have the default config settings.
91  db.getCategory("test.category1");
92  db.getCategory("test.category2");
93  EXPECT_EQ(parseLogConfig(".:=INFO:"), db.getConfig());
94 
95  // Apply an update
96  db.updateConfig(parseLogConfig("INFO:stderr; stderr=handlerA:stream=stderr"));
97  EXPECT_EQ(LogLevel::INFO, db.getCategory("")->getLevel());
99  db.getCategory("")->getHandlers(),
101  MatchLogHandler("handlerA", {{"stream", "stderr"}})));
102  EXPECT_EQ(
103  parseLogConfig(".:=INFO:stderr; stderr=handlerA:stream=stderr"),
104  db.getConfig());
105 
106  // Update the log level for category "foo"
107  // This should not affect the existing settings for the root category
108  EXPECT_EQ(LogLevel::MAX_LEVEL, db.getCategory("foo")->getLevel());
109  EXPECT_TRUE(db.getCategory("foo")->getLevelInfo().second);
110  db.updateConfig(parseLogConfig("foo:=DBG2"));
111  EXPECT_EQ(LogLevel::DBG2, db.getCategory("foo")->getLevel());
112  EXPECT_FALSE(db.getCategory("foo")->getLevelInfo().second);
113  EXPECT_EQ(LogLevel::INFO, db.getCategory("")->getLevel());
114  EXPECT_EQ(1, db.getCategory("")->getHandlers().size());
115  EXPECT_EQ(
117  ".:=INFO:stderr, foo:=DBG2:; stderr=handlerA:stream=stderr"),
118  db.getConfig());
119 
120  // Add 2 log handlers to the "bar" log category.
121  db.updateConfig(
122  parseLogConfig("bar=ERROR:new:h2; "
123  "new=handlerB:key=value; "
124  "h2=handlerA:foo=bar"));
125  EXPECT_EQ(LogLevel::INFO, db.getCategory("")->getLevel());
126  EXPECT_THAT(
127  db.getCategory("")->getHandlers(),
129  MatchLogHandler("handlerA", {{"stream", "stderr"}})));
130  EXPECT_EQ(LogLevel::ERR, db.getCategory("bar")->getLevel());
131  EXPECT_THAT(
132  db.getCategory("bar")->getHandlers(),
134  MatchLogHandler("handlerB", {{"key", "value"}}),
135  MatchLogHandler("handlerA", {{"foo", "bar"}})));
136  EXPECT_EQ(
137  parseLogConfig(".:=INFO:stderr, foo:=DBG2:, bar=ERROR:new:h2; "
138  "stderr=handlerA: stream=stderr; "
139  "new=handlerB: key=value; "
140  "h2=handlerA: foo=bar"),
141  db.getConfig());
142 
143  // Updating the "new" log handler settings should automatically update
144  // the settings we see on the "bar" category, even if we don't explicitly
145  // list "bar" in the config update
146  db.updateConfig(parseLogConfig("; new=handlerB:newkey=newvalue"));
147  EXPECT_EQ(LogLevel::INFO, db.getCategory("")->getLevel());
148  EXPECT_THAT(
149  db.getCategory("")->getHandlers(),
151  MatchLogHandler("handlerA", {{"stream", "stderr"}})));
152  EXPECT_EQ(LogLevel::ERR, db.getCategory("bar")->getLevel());
153  EXPECT_THAT(
154  db.getCategory("bar")->getHandlers(),
156  MatchLogHandler("handlerB", {{"newkey", "newvalue"}}),
157  MatchLogHandler("handlerA", {{"foo", "bar"}})));
158  EXPECT_EQ(
159  parseLogConfig(".:=INFO:stderr, foo:=DBG2:, bar=ERROR:new:h2; "
160  "stderr=handlerA: stream=stderr; "
161  "new=handlerB: newkey=newvalue; "
162  "h2=handlerA: foo=bar"),
163  db.getConfig());
164 
165  // Updating the level settings for the "bar" handler should leave its
166  // handlers unchanged.
167  db.updateConfig(parseLogConfig("bar=WARN"));
168  EXPECT_EQ(LogLevel::INFO, db.getCategory("")->getLevel());
169  EXPECT_THAT(
170  db.getCategory("")->getHandlers(),
172  MatchLogHandler("handlerA", {{"stream", "stderr"}})));
173  EXPECT_EQ(LogLevel::WARN, db.getCategory("bar")->getLevel());
174  EXPECT_THAT(
175  db.getCategory("bar")->getHandlers(),
177  MatchLogHandler("handlerB", {{"newkey", "newvalue"}}),
178  MatchLogHandler("handlerA", {{"foo", "bar"}})));
179  EXPECT_EQ(
180  parseLogConfig(".:=INFO:stderr, foo:=DBG2:, bar=WARN:new:h2; "
181  "stderr=handlerA: stream=stderr; "
182  "new=handlerB: newkey=newvalue; "
183  "h2=handlerA: foo=bar"),
184  db.getConfig());
185 
186  // Update the options for the h2 handler in place, and also add it to the
187  // "test.foo" category. The changes should also be reflected on the "bar"
188  // category.
189  db.updateConfig(
190  parseLogConfig("test.foo=INFO:h2; h2=handlerA:reuse_handler=1,foo=xyz"));
191  EXPECT_EQ(LogLevel::INFO, db.getCategory("")->getLevel());
192  EXPECT_THAT(
193  db.getCategory("")->getHandlers(),
195  MatchLogHandler("handlerA", {{"stream", "stderr"}})));
196  EXPECT_EQ(LogLevel::WARN, db.getCategory("bar")->getLevel());
197  EXPECT_THAT(
198  db.getCategory("bar")->getHandlers(),
200  MatchLogHandler("handlerB", {{"newkey", "newvalue"}}),
201  MatchLogHandler(
202  "handlerA", {{"foo", "xyz"}, {"reuse_handler", "1"}})));
203  EXPECT_EQ(LogLevel::INFO, db.getCategory("test.foo")->getLevel());
204  EXPECT_THAT(
205  db.getCategory("test.foo")->getHandlers(),
206  UnorderedElementsAre(MatchLogHandler(
207  "handlerA", {{"foo", "xyz"}, {"reuse_handler", "1"}})));
208  EXPECT_EQ(
209  parseLogConfig(".:=INFO:stderr, foo:=DBG2:, bar=WARN:new:h2, "
210  "test.foo=INFO:h2; "
211  "stderr=handlerA: stream=stderr; "
212  "new=handlerB: newkey=newvalue; "
213  "h2=handlerA: reuse_handler=1,foo=xyz"),
214  db.getConfig());
215 
216  // Update the options for the h2 handler using partial options
217  db.updateConfig(parseLogConfig("; h2:abc=def"));
218  EXPECT_EQ(LogLevel::INFO, db.getCategory("")->getLevel());
219  EXPECT_THAT(
220  db.getCategory("")->getHandlers(),
222  MatchLogHandler("handlerA", {{"stream", "stderr"}})));
223  EXPECT_EQ(LogLevel::WARN, db.getCategory("bar")->getLevel());
224  EXPECT_THAT(
225  db.getCategory("bar")->getHandlers(),
227  MatchLogHandler("handlerB", {{"newkey", "newvalue"}}),
228  MatchLogHandler(
229  "handlerA",
230  {{"abc", "def"}, {"foo", "xyz"}, {"reuse_handler", "1"}})));
231  EXPECT_EQ(LogLevel::INFO, db.getCategory("test.foo")->getLevel());
232  EXPECT_THAT(
233  db.getCategory("test.foo")->getHandlers(),
234  UnorderedElementsAre(MatchLogHandler(
235  "handlerA",
236  {{"abc", "def"}, {"foo", "xyz"}, {"reuse_handler", "1"}})));
237  EXPECT_EQ(
238  parseLogConfig(".:=INFO:stderr, foo:=DBG2:, bar=WARN:new:h2, "
239  "test.foo=INFO:h2; "
240  "stderr=handlerA: stream=stderr; "
241  "new=handlerB: newkey=newvalue; "
242  "h2=handlerA: reuse_handler=1,abc=def,foo=xyz"),
243  db.getConfig());
244 
245  // Update the options for the "new" handler using partial options
246  db.updateConfig(parseLogConfig("; new:opt1=value1"));
247  EXPECT_EQ(LogLevel::WARN, db.getCategory("bar")->getLevel());
248  EXPECT_THAT(
249  db.getCategory("bar")->getHandlers(),
251  MatchLogHandler(
252  "handlerB", {{"opt1", "value1"}, {"newkey", "newvalue"}}),
253  MatchLogHandler(
254  "handlerA",
255  {{"abc", "def"}, {"foo", "xyz"}, {"reuse_handler", "1"}})));
256  EXPECT_EQ(LogLevel::INFO, db.getCategory("test.foo")->getLevel());
257  EXPECT_THAT(
258  db.getCategory("test.foo")->getHandlers(),
259  UnorderedElementsAre(MatchLogHandler(
260  "handlerA",
261  {{"abc", "def"}, {"foo", "xyz"}, {"reuse_handler", "1"}})));
262  EXPECT_EQ(
263  parseLogConfig(".:=INFO:stderr, foo:=DBG2:, bar=WARN:new:h2, "
264  "test.foo=INFO:h2; "
265  "stderr=handlerA: stream=stderr; "
266  "new=handlerB: newkey=newvalue,opt1=value1; "
267  "h2=handlerA: reuse_handler=1,abc=def,foo=xyz"),
268  db.getConfig());
269 
270  // Supplying partial options for a non-existent log handler should fail
272  db.updateConfig(parseLogConfig("; no_such_handler:foo=bar")),
273  std::invalid_argument,
274  R"(cannot update unknown log handler "no_such_handler")");
275 
276  // Explicitly clear the handlers for the "bar" category
277  // This should remove the "new" handler from the LoggerDB since bar was the
278  // only category referring to it.
279  db.updateConfig(parseLogConfig("bar=WARN:"));
280  EXPECT_EQ(LogLevel::INFO, db.getCategory("")->getLevel());
281  EXPECT_THAT(
282  db.getCategory("")->getHandlers(),
284  MatchLogHandler("handlerA", {{"stream", "stderr"}})));
285  EXPECT_EQ(LogLevel::WARN, db.getCategory("bar")->getLevel());
286  EXPECT_THAT(db.getCategory("bar")->getHandlers(), UnorderedElementsAre());
287  EXPECT_EQ(
288  parseLogConfig(".:=INFO:stderr, foo:=DBG2:, bar=WARN:, "
289  "test.foo=INFO:h2; "
290  "stderr=handlerA: stream=stderr; "
291  "h2=handlerA: reuse_handler=1,abc=def,foo=xyz"),
292  db.getConfig());
293 
294  // Now test resetConfig()
295  db.resetConfig(
296  parseLogConfig("bar=INFO:h2, test.abc=DBG3; "
297  "h2=handlerB: abc=xyz"));
298  EXPECT_EQ(
299  parseLogConfig(".:=INFO:, bar=INFO:h2, test.abc=DBG3:; "
300  "h2=handlerB: abc=xyz"),
301  db.getConfig());
302 }
#define EXPECT_THROW_RE(statement, exceptionType, pattern)
Definition: TestUtils.h:119
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogConfig parseLogConfig(StringPiece value)
internal::UnorderedElementsAreMatcher< ::testing::tuple<> > UnorderedElementsAre()
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_THAT(value, matcher)
void registerHandlerFactory(std::unique_ptr< LogHandlerFactory > factory, bool replaceExisting=false)
Definition: LoggerDB.cpp:572
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( ConfigUpdate  ,
getConfigAnonymousHandlers   
)

Definition at line 304 of file ConfigUpdateTest.cpp.

References folly::DBG1, folly::DBG2, EXPECT_EQ, folly::parseLogConfig(), and folly::LoggerDB::TESTING.

304  {
305  LoggerDB db{LoggerDB::TESTING};
307  std::make_unique<TestLogHandlerFactory>("handlerA"));
308  db.registerHandlerFactory(
309  std::make_unique<TestLogHandlerFactory>("handlerB"));
310  EXPECT_EQ(parseLogConfig(".:=INFO:"), db.getConfig());
311 
312  // Manually attach a handler to a category.
313  // It should be reported as "anonymousHandler1"
314  auto handlerFoo = std::make_shared<TestLogHandler>(
315  LogHandlerConfig{"foo", {{"abc", "xyz"}}});
316  db.setLevel("x.y.z", LogLevel::DBG2);
317  db.getCategory("x.y.z")->addHandler(handlerFoo);
318  EXPECT_EQ(
319  parseLogConfig(".:=INFO:, x.y.z=DBG2:anonymousHandler1; "
320  "anonymousHandler1=foo:abc=xyz"),
321  db.getConfig());
322 
323  // If we attach the same handler to another category it should still only be
324  // reported once.
325  db.setLevel("test.category", LogLevel::DBG1);
326  db.getCategory("test.category")->addHandler(handlerFoo);
327  EXPECT_EQ(
328  parseLogConfig(".:=INFO:, "
329  "x.y.z=DBG2:anonymousHandler1, "
330  "test.category=DBG1:anonymousHandler1; "
331  "anonymousHandler1=foo:abc=xyz"),
332  db.getConfig());
333 
334  // If we use updateConfig() to explicitly define a handler named
335  // "anonymousHandler1", the unnamed handler will be reported as
336  // "anonymousHandler2" instead now.
337  db.updateConfig(parseLogConfig(
338  "a.b.c=INFO:anonymousHandler1; anonymousHandler1=handlerA:key=value"));
339  EXPECT_EQ(
340  parseLogConfig(".:=INFO:, "
341  "a.b.c=INFO:anonymousHandler1, "
342  "x.y.z=DBG2:anonymousHandler2, "
343  "test.category=DBG1:anonymousHandler2; "
344  "anonymousHandler1=handlerA: key=value; "
345  "anonymousHandler2=foo: abc=xyz"),
346  db.getConfig());
347 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogConfig parseLogConfig(StringPiece value)
void registerHandlerFactory(std::unique_ptr< LogHandlerFactory > factory, bool replaceExisting=false)
Definition: LoggerDB.cpp:572
TEST ( ConfigUpdate  ,
getFullConfig   
)

Definition at line 349 of file ConfigUpdateTest.cpp.

References EXPECT_EQ, folly::parseLogConfig(), and folly::LoggerDB::TESTING.

349  {
350  LoggerDB db{LoggerDB::TESTING};
352  std::make_unique<TestLogHandlerFactory>("handlerA"));
353  db.registerHandlerFactory(
354  std::make_unique<TestLogHandlerFactory>("handlerB"));
355  EXPECT_EQ(parseLogConfig(".:=INFO:"), db.getConfig());
356 
357  db.getCategory("src.libfoo.foo.c");
358  db.getCategory("src.libfoo.foo.h");
359  db.getCategory("src.libfoo.bar.h");
360  db.getCategory("src.libfoo.bar.c");
361  db.getCategory("test.foo.test.c");
362 
363  db.updateConfig(
364  parseLogConfig(".=INFO:stdout,"
365  "src.libfoo=dbg5; "
366  "stdout=handlerA:stream=stdout"));
367  EXPECT_EQ(
368  parseLogConfig(".:=INFO:stdout,"
369  "src.libfoo=dbg5:; "
370  "stdout=handlerA:stream=stdout"),
371  db.getConfig());
372  EXPECT_EQ(
373  parseLogConfig(".:=INFO:stdout,"
374  "src=FATAL:, "
375  "src.libfoo=dbg5:, "
376  "src.libfoo.foo=FATAL:, "
377  "src.libfoo.foo.c=FATAL:, "
378  "src.libfoo.foo.h=FATAL:, "
379  "src.libfoo.bar=FATAL:, "
380  "src.libfoo.bar.c=FATAL:, "
381  "src.libfoo.bar.h=FATAL:, "
382  "test=FATAL:, "
383  "test.foo=FATAL:, "
384  "test.foo.test=FATAL:, "
385  "test.foo.test.c=FATAL:; "
386  "stdout=handlerA:stream=stdout"),
387  db.getFullConfig());
388 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogConfig parseLogConfig(StringPiece value)
void registerHandlerFactory(std::unique_ptr< LogHandlerFactory > factory, bool replaceExisting=false)
Definition: LoggerDB.cpp:572