logger_manager_impl.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifndef __LOGGER_MANAGER_IMPL_H
  15. #define __LOGGER_MANAGER_IMPL_H
  16. #include <string>
  17. #include <log4cplus/appender.h>
  18. #include <log/logger_level.h>
  19. // Forward declaration to avoid need to include log4cplus header file here.
  20. namespace log4cplus {
  21. class Logger;
  22. class Appender;
  23. }
  24. namespace isc {
  25. namespace log {
  26. // Forward declarations
  27. class LoggerSpecification;
  28. struct OutputOption;
  29. /// \brief Logger Manager Implementation
  30. ///
  31. /// This is the implementation of the logger manager for the log4cplus
  32. /// underlying logger.
  33. ///
  34. /// As noted in logger_manager.h, the logger manager class exists to set up the
  35. /// logging given a set of specifications. This class handles the processing
  36. /// of those specifications.
  37. ///
  38. /// Note: the logging has been implemented using a "pimpl" idiom to conceal
  39. /// the underlying implementation (log4cplus) from the BIND 10 interface.
  40. /// This requires that there be an implementation class, even though in this
  41. /// case, all the implementation class methods can be declared static.
  42. class LoggerManagerImpl {
  43. public:
  44. /// \brief Constructor
  45. LoggerManagerImpl()
  46. {}
  47. /// \brief Initialize Processing
  48. ///
  49. /// This resets the hierachy of loggers back to their defaults. This means
  50. /// that all non-root loggers (if they exist) are set to NOT_SET, and the
  51. /// root logger reset to logging informational messages.
  52. static void processInit();
  53. /// \brief Process Specification
  54. ///
  55. /// Processes the specification for a single logger.
  56. ///
  57. /// \param spec Logging specification for this logger
  58. static void processSpecification(const LoggerSpecification& spec);
  59. /// \brief End Processing
  60. ///
  61. /// Terminates the processing of the logging specifications.
  62. static void processEnd()
  63. {}
  64. /// \brief Implementation-specific initialization
  65. ///
  66. /// Sets the basic configuration for logging (the root logger has INFO and
  67. /// more severe messages routed to stdout). Unless this function (or
  68. /// process() with a valid specification for all loggers that will log
  69. /// messages) is called before a message is logged, log4cplus will output
  70. /// a message to stderr noting that logging has not been initialized.
  71. ///
  72. /// It is assumed here that the name of the BIND 10 root logger can be
  73. /// obtained from the global function getRootLoggerName().
  74. ///
  75. /// \param severity Severity to be associated with this logger
  76. /// \param dbglevel Debug level associated with the root logger
  77. static void init(isc::log::Severity severity = isc::log::INFO,
  78. int dbglevel = 0);
  79. /// \brief Reset logging
  80. ///
  81. /// Resets to default configuration (root logger logging to the console
  82. /// with INFO severity).
  83. ///
  84. /// \param severity Severity to be associated with this logger
  85. /// \param dbglevel Debug level associated with the root logger
  86. static void reset(isc::log::Severity severity = isc::log::INFO,
  87. int dbglevel = 0);
  88. private:
  89. /// \brief Create console appender
  90. ///
  91. /// Creates an object that, when attached to a logger, will log to one
  92. /// of the output streams (stdout or stderr).
  93. ///
  94. /// \param logger Log4cplus logger to which the appender must be attached.
  95. /// \param opt Output options for this appender.
  96. static void createConsoleAppender(log4cplus::Logger& logger,
  97. const OutputOption& opt);
  98. /// \brief Create file appender
  99. ///
  100. /// Creates an object that, when attached to a logger, will log to a
  101. /// specified file. This also includes the ability to "roll" files when
  102. /// they reach a specified size.
  103. ///
  104. /// \param logger Log4cplus logger to which the appender must be attached.
  105. /// \param opt Output options for this appender.
  106. static void createFileAppender(log4cplus::Logger& logger,
  107. const OutputOption& opt);
  108. /// \brief Create syslog appender
  109. ///
  110. /// Creates an object that, when attached to a logger, will log to the
  111. /// syslog file.
  112. ///
  113. /// \param logger Log4cplus logger to which the appender must be attached.
  114. /// \param opt Output options for this appender.
  115. static void createSyslogAppender(log4cplus::Logger& logger,
  116. const OutputOption& opt);
  117. /// \brief Set default layout and severity for root logger
  118. ///
  119. /// Initializes the root logger to BIND 10 defaults - console output and
  120. /// the passed severity/debug level.
  121. ///
  122. /// \param severity Severity of messages that the logger should output.
  123. /// \param dbglevel Debug level if severity = DEBUG
  124. static void initRootLogger(isc::log::Severity severity = isc::log::INFO,
  125. int dbglevel = 0);
  126. /// \brief Set layout for console appender
  127. ///
  128. /// Sets the layout of the specified appender to one suitable for file
  129. /// or console output:
  130. ///
  131. /// YYYY-MM-DD HH:MM:SS.ssss SEVERITY [root.logger] message
  132. ///
  133. /// \param appender Appender for which this pattern is to be set.
  134. static void setConsoleAppenderLayout(log4cplus::SharedAppenderPtr& appender);
  135. /// \brief Set layout for syslog appender
  136. ///
  137. /// Sets the layout of the specified appender to one suitable for the
  138. /// syslog file:
  139. ///
  140. /// SEVERITY [root.logger] message
  141. ///
  142. /// \param appender Appender for which this pattern is to be set.
  143. static void setSyslogAppenderLayout(log4cplus::SharedAppenderPtr& appender);
  144. };
  145. } // namespace log
  146. } // namespace isc
  147. #endif // __LOGGER_MANAGER_IMPL_H