logger_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <iostream>
  15. #include <string>
  16. #include <gtest/gtest.h>
  17. #include <log/root_logger_name.h>
  18. #include <log/logger.h>
  19. #include <log/messagedef.h>
  20. using namespace isc;
  21. using namespace isc::log;
  22. using namespace std;
  23. namespace isc {
  24. namespace log {
  25. /// \brief Test Logger
  26. ///
  27. /// This logger is a subclass of the logger class under test, but makes
  28. /// protected methods public (for testing)
  29. class TestLogger : public Logger {
  30. public:
  31. /// \brief constructor
  32. TestLogger(const string& name) : Logger(name, true)
  33. {}
  34. static void reset() {
  35. Logger::reset();
  36. }
  37. };
  38. } // namespace log
  39. } // namespace isc
  40. class LoggerTest : public ::testing::Test {
  41. protected:
  42. LoggerTest()
  43. {
  44. }
  45. ~LoggerTest() {
  46. TestLogger::reset();
  47. }
  48. };
  49. // Checks that the logger is named correctly.
  50. TEST_F(LoggerTest, Name) {
  51. // Create a logger
  52. setRootLoggerName("test1");
  53. Logger logger("alpha");
  54. // ... and check the name
  55. EXPECT_EQ(string("test1.alpha"), logger.getName());
  56. }
  57. // This test attempts to get two instances of a logger with the same name
  58. // and checks that they are in fact the same logger.
  59. TEST_F(LoggerTest, GetLogger) {
  60. // Set the root logger name (not strictly needed, but this will be the
  61. // case in the program(.
  62. setRootLoggerName("test2");
  63. const string name1 = "alpha";
  64. const string name2 = "beta";
  65. // Instantiate two loggers that should be the same
  66. TestLogger logger1(name1);
  67. TestLogger logger2(name1);
  68. // And check they equal
  69. EXPECT_TRUE(logger1 == logger2);
  70. // Instantiate another logger with another name and check that it
  71. // is different to the previously instantiated ones.
  72. TestLogger logger3(name2);
  73. EXPECT_FALSE(logger1 == logger3);
  74. }
  75. // Check that the logger levels are get set properly.
  76. TEST_F(LoggerTest, Severity) {
  77. // Create a logger
  78. setRootLoggerName("test3");
  79. TestLogger logger("alpha");
  80. // Now check the levels
  81. logger.setSeverity(isc::log::NONE);
  82. EXPECT_EQ(isc::log::NONE, logger.getSeverity());
  83. logger.setSeverity(isc::log::FATAL);
  84. EXPECT_EQ(isc::log::FATAL, logger.getSeverity());
  85. logger.setSeverity(isc::log::ERROR);
  86. EXPECT_EQ(isc::log::ERROR, logger.getSeverity());
  87. logger.setSeverity(isc::log::WARN);
  88. EXPECT_EQ(isc::log::WARN, logger.getSeverity());
  89. logger.setSeverity(isc::log::INFO);
  90. EXPECT_EQ(isc::log::INFO, logger.getSeverity());
  91. logger.setSeverity(isc::log::DEBUG);
  92. EXPECT_EQ(isc::log::DEBUG, logger.getSeverity());
  93. logger.setSeverity(isc::log::DEFAULT);
  94. EXPECT_EQ(isc::log::DEFAULT, logger.getSeverity());
  95. }
  96. // Check that the debug level is set correctly.
  97. TEST_F(LoggerTest, DebugLevels) {
  98. // Create a logger
  99. setRootLoggerName("test4");
  100. TestLogger logger("alpha");
  101. // Debug level should be 0 if not at debug severity
  102. logger.setSeverity(isc::log::NONE, 20);
  103. EXPECT_EQ(0, logger.getDebugLevel());
  104. logger.setSeverity(isc::log::INFO, 42);
  105. EXPECT_EQ(0, logger.getDebugLevel());
  106. // Should be the value set if the severity is set to DEBUG though.
  107. logger.setSeverity(isc::log::DEBUG, 32);
  108. EXPECT_EQ(32, logger.getDebugLevel());
  109. logger.setSeverity(isc::log::DEBUG, 97);
  110. EXPECT_EQ(97, logger.getDebugLevel());
  111. // Try the limits
  112. logger.setSeverity(isc::log::DEBUG, -1);
  113. EXPECT_EQ(0, logger.getDebugLevel());
  114. logger.setSeverity(isc::log::DEBUG, 0);
  115. EXPECT_EQ(0, logger.getDebugLevel());
  116. logger.setSeverity(isc::log::DEBUG, 1);
  117. EXPECT_EQ(1, logger.getDebugLevel());
  118. logger.setSeverity(isc::log::DEBUG, 98);
  119. EXPECT_EQ(98, logger.getDebugLevel());
  120. logger.setSeverity(isc::log::DEBUG, 99);
  121. EXPECT_EQ(99, logger.getDebugLevel());
  122. logger.setSeverity(isc::log::DEBUG, 100);
  123. EXPECT_EQ(99, logger.getDebugLevel());
  124. }
  125. // Check that changing the parent and child severity does not affect the
  126. // other.
  127. TEST_F(LoggerTest, SeverityInheritance) {
  128. // Create to loggers. We cheat here as we know that the underlying
  129. // implementation (in this case log4cxx) will set a parent-child
  130. // relationship if the loggers are named <parent> and <parent>.<child>.
  131. setRootLoggerName("test5");
  132. TestLogger parent("alpha");
  133. TestLogger child("alpha.beta");
  134. // By default, newly created loggers should have a level of DEFAULT
  135. // (i.e. default to parent)
  136. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  137. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  138. // Set the severity of the child to something other than the default -
  139. // check it changes and that of the parent does not.
  140. child.setSeverity(isc::log::INFO);
  141. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  142. EXPECT_EQ(isc::log::INFO, child.getSeverity());
  143. // Reset the child severity and set that of the parent
  144. child.setSeverity(isc::log::DEFAULT);
  145. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  146. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  147. parent.setSeverity(isc::log::WARN);
  148. EXPECT_EQ(isc::log::WARN, parent.getSeverity());
  149. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  150. }
  151. // Check that severity is inherited.
  152. TEST_F(LoggerTest, EffectiveSeverityInheritance) {
  153. // Create to loggers. We cheat here as we know that the underlying
  154. // implementation (in this case log4cxx) will set a parent-child
  155. // relationship if the loggers are named <parent> and <parent>.<child>.
  156. setRootLoggerName("test6");
  157. Logger parent("test6");
  158. Logger child("test6.beta");
  159. // By default, newly created loggers should have a level of DEFAULT
  160. // (i.e. default to parent) and the root should have a default severity
  161. // of INFO. However, the latter is only enforced when created by the
  162. // RootLogger class, so explicitly set it for the parent for now.
  163. parent.setSeverity(isc::log::INFO);
  164. EXPECT_EQ(isc::log::INFO, parent.getEffectiveSeverity());
  165. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  166. EXPECT_EQ(isc::log::INFO, child.getEffectiveSeverity());
  167. // Set the severity of the child to something other than the default -
  168. // check it changes and that of the parent does not.
  169. child.setSeverity(isc::log::FATAL);
  170. EXPECT_EQ(isc::log::INFO, parent.getEffectiveSeverity());
  171. EXPECT_EQ(isc::log::FATAL, child.getEffectiveSeverity());
  172. // Reset the child severity and check again.
  173. child.setSeverity(isc::log::DEFAULT);
  174. EXPECT_EQ(isc::log::INFO, parent.getEffectiveSeverity());
  175. EXPECT_EQ(isc::log::INFO, child.getEffectiveSeverity());
  176. // Change the parwnt's severity and check it is reflects in the child.
  177. parent.setSeverity(isc::log::WARN);
  178. EXPECT_EQ(isc::log::WARN, parent.getEffectiveSeverity());
  179. EXPECT_EQ(isc::log::WARN, child.getEffectiveSeverity());
  180. }
  181. // Test the isXxxxEnabled methods.
  182. TEST_F(LoggerTest, IsXxxEnabled) {
  183. setRootLoggerName("test7");
  184. Logger logger("test7");
  185. logger.setSeverity(isc::log::INFO);
  186. EXPECT_FALSE(logger.isDebugEnabled());
  187. EXPECT_TRUE(logger.isInfoEnabled());
  188. EXPECT_TRUE(logger.isWarnEnabled());
  189. EXPECT_TRUE(logger.isErrorEnabled());
  190. EXPECT_TRUE(logger.isFatalEnabled());
  191. logger.setSeverity(isc::log::WARN);
  192. EXPECT_FALSE(logger.isDebugEnabled());
  193. EXPECT_FALSE(logger.isInfoEnabled());
  194. EXPECT_TRUE(logger.isWarnEnabled());
  195. EXPECT_TRUE(logger.isErrorEnabled());
  196. EXPECT_TRUE(logger.isFatalEnabled());
  197. logger.setSeverity(isc::log::ERROR);
  198. EXPECT_FALSE(logger.isDebugEnabled());
  199. EXPECT_FALSE(logger.isInfoEnabled());
  200. EXPECT_FALSE(logger.isWarnEnabled());
  201. EXPECT_TRUE(logger.isErrorEnabled());
  202. EXPECT_TRUE(logger.isFatalEnabled());
  203. logger.setSeverity(isc::log::FATAL);
  204. EXPECT_FALSE(logger.isDebugEnabled());
  205. EXPECT_FALSE(logger.isInfoEnabled());
  206. EXPECT_FALSE(logger.isWarnEnabled());
  207. EXPECT_FALSE(logger.isErrorEnabled());
  208. EXPECT_TRUE(logger.isFatalEnabled());
  209. // Check various debug levels
  210. logger.setSeverity(isc::log::DEBUG);
  211. EXPECT_TRUE(logger.isDebugEnabled());
  212. EXPECT_TRUE(logger.isInfoEnabled());
  213. EXPECT_TRUE(logger.isWarnEnabled());
  214. EXPECT_TRUE(logger.isErrorEnabled());
  215. EXPECT_TRUE(logger.isFatalEnabled());
  216. logger.setSeverity(isc::log::DEBUG, 45);
  217. EXPECT_TRUE(logger.isDebugEnabled());
  218. EXPECT_TRUE(logger.isInfoEnabled());
  219. EXPECT_TRUE(logger.isWarnEnabled());
  220. EXPECT_TRUE(logger.isErrorEnabled());
  221. EXPECT_TRUE(logger.isFatalEnabled());
  222. // Create a child logger with no severity set, and check that it reflects
  223. // the severity of the parent logger.
  224. Logger child("test7.child");
  225. logger.setSeverity(isc::log::FATAL);
  226. EXPECT_FALSE(child.isDebugEnabled());
  227. EXPECT_FALSE(child.isInfoEnabled());
  228. EXPECT_FALSE(child.isWarnEnabled());
  229. EXPECT_FALSE(child.isErrorEnabled());
  230. EXPECT_TRUE(child.isFatalEnabled());
  231. logger.setSeverity(isc::log::INFO);
  232. EXPECT_FALSE(child.isDebugEnabled());
  233. EXPECT_TRUE(child.isInfoEnabled());
  234. EXPECT_TRUE(child.isWarnEnabled());
  235. EXPECT_TRUE(child.isErrorEnabled());
  236. EXPECT_TRUE(child.isFatalEnabled());
  237. }
  238. // Within the Debug level there are 100 debug levels. Test that we know
  239. // when to issue a debug message.
  240. TEST_F(LoggerTest, IsDebugEnabledLevel) {
  241. setRootLoggerName("test8");
  242. Logger logger("test8");
  243. int MID_LEVEL = (MIN_DEBUG_LEVEL + MAX_DEBUG_LEVEL) / 2;
  244. logger.setSeverity(isc::log::DEBUG);
  245. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  246. EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL));
  247. EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  248. logger.setSeverity(isc::log::DEBUG, MIN_DEBUG_LEVEL);
  249. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  250. EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL));
  251. EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  252. logger.setSeverity(isc::log::DEBUG, MID_LEVEL);
  253. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  254. EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL - 1));
  255. EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL));
  256. EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL + 1));
  257. EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  258. logger.setSeverity(isc::log::DEBUG, MAX_DEBUG_LEVEL);
  259. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  260. EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL));
  261. EXPECT_TRUE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  262. }