logger_manager_impl.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Kea 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. /// \brief Initialize Processing
  47. ///
  48. /// This resets the hierarchy of loggers back to their defaults. This means
  49. /// that all non-root loggers (if they exist) are set to NOT_SET, and the
  50. /// root logger reset to logging informational messages.
  51. void processInit();
  52. /// \brief Process Specification
  53. ///
  54. /// Processes the specification for a single logger.
  55. ///
  56. /// \param spec Logging specification for this logger
  57. static void processSpecification(const LoggerSpecification& spec);
  58. /// \brief End Processing
  59. ///
  60. /// Terminates the processing of the logging specifications.
  61. void processEnd();
  62. /// \brief Implementation-specific initialization
  63. ///
  64. /// Sets the basic configuration for logging (the root logger has INFO and
  65. /// more severe messages routed to stdout). Unless this function (or
  66. /// process() with a valid specification for all loggers that will log
  67. /// messages) is called before a message is logged, log4cplus will output
  68. /// a message to stderr noting that logging has not been initialized.
  69. ///
  70. /// It is assumed here that the name of the Kea root logger can be
  71. /// obtained from the global function getRootLoggerName().
  72. ///
  73. /// \param severity Severity to be associated with this logger
  74. /// \param dbglevel Debug level associated with the root logger
  75. /// \param buffer If true, all log messages will be buffered until one of
  76. /// the \c process() methods is called. If false, initial logging
  77. /// shall go to the default output (i.e. stdout)
  78. static void init(isc::log::Severity severity = isc::log::INFO,
  79. int dbglevel = 0, bool buffer = false);
  80. /// \brief Reset logging
  81. ///
  82. /// Resets to default configuration (root logger logging to the console
  83. /// with INFO severity).
  84. ///
  85. /// \param severity Severity to be associated with this logger
  86. /// \param dbglevel Debug level associated with the root logger
  87. static void reset(isc::log::Severity severity = isc::log::INFO,
  88. int dbglevel = 0);
  89. private:
  90. /// \brief Create console appender
  91. ///
  92. /// Creates an object that, when attached to a logger, will log to one
  93. /// of the output streams (stdout or stderr).
  94. ///
  95. /// \param logger Log4cplus logger to which the appender must be attached.
  96. /// \param opt Output options for this appender.
  97. static void createConsoleAppender(log4cplus::Logger& logger,
  98. const OutputOption& opt);
  99. /// \brief Create file appender
  100. ///
  101. /// Creates an object that, when attached to a logger, will log to a
  102. /// specified file. This also includes the ability to "roll" files when
  103. /// they reach a specified size.
  104. ///
  105. /// \param logger Log4cplus logger to which the appender must be attached.
  106. /// \param opt Output options for this appender.
  107. static void createFileAppender(log4cplus::Logger& logger,
  108. const OutputOption& opt);
  109. /// \brief Create syslog appender
  110. ///
  111. /// Creates an object that, when attached to a logger, will log to the
  112. /// syslog file.
  113. ///
  114. /// \param logger Log4cplus logger to which the appender must be attached.
  115. /// \param opt Output options for this appender.
  116. static void createSyslogAppender(log4cplus::Logger& logger,
  117. const OutputOption& opt);
  118. /// \brief Create buffered appender
  119. ///
  120. /// Appends an object to the logger that will store the log events sent
  121. /// to the logger. These log messages are replayed to the logger in
  122. /// processEnd().
  123. ///
  124. /// \param logger Log4cplus logger to which the appender must be attached.
  125. static void createBufferAppender(log4cplus::Logger& logger);
  126. /// \brief Set default layout and severity for root logger
  127. ///
  128. /// Initializes the root logger to Kea defaults - console or buffered
  129. /// output and the passed severity/debug level.
  130. ///
  131. /// \param severity Severity of messages that the logger should output.
  132. /// \param dbglevel Debug level if severity = DEBUG
  133. /// \param buffer If true, all log messages will be buffered until one of
  134. /// the \c process() methods is called. If false, initial logging
  135. /// shall go to the default output (i.e. stdout)
  136. static void initRootLogger(isc::log::Severity severity = isc::log::INFO,
  137. int dbglevel = 0, bool buffer = false);
  138. /// \brief Set layout for console appender
  139. ///
  140. /// Sets the layout of the specified appender to one suitable for file
  141. /// or console output:
  142. ///
  143. /// YYYY-MM-DD HH:MM:SS.ssss SEVERITY [root.logger] message
  144. ///
  145. /// \param appender Appender for which this pattern is to be set.
  146. static void setConsoleAppenderLayout(log4cplus::SharedAppenderPtr& appender);
  147. /// \brief Set layout for syslog appender
  148. ///
  149. /// Sets the layout of the specified appender to one suitable for the
  150. /// syslog file:
  151. ///
  152. /// SEVERITY [root.logger] message
  153. ///
  154. /// \param appender Appender for which this pattern is to be set.
  155. static void setSyslogAppenderLayout(log4cplus::SharedAppenderPtr& appender);
  156. /// \brief Store all buffer appenders
  157. ///
  158. /// When processing a new specification, this method can be used
  159. /// to keep a list of the buffer appenders; the caller can then
  160. /// process the specification, and call \c flushBufferAppenders()
  161. /// to flush and clear the list
  162. void storeBufferAppenders();
  163. /// \brief Flush the stored buffer appenders
  164. ///
  165. /// This flushes the list of buffer appenders stored in
  166. /// \c storeBufferAppenders(), and clears it
  167. void flushBufferAppenders();
  168. /// Only used between processInit() and processEnd(), to temporarily
  169. /// store the buffer appenders in order to flush them after
  170. /// processSpecification() calls have been completed
  171. std::vector<log4cplus::SharedAppenderPtr> buffer_appender_store_;
  172. };
  173. } // namespace log
  174. } // namespace isc
  175. #endif // LOGGER_MANAGER_IMPL_H