LoggingConfigurator.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // LoggingConfigurator.h
  3. //
  4. // Library: Util
  5. // Package: Configuration
  6. // Module: LoggingConfigurator
  7. //
  8. // Definition of the LoggingConfigurator class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Util_LoggingConfigurator_INCLUDED
  16. #define Util_LoggingConfigurator_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #include "Poco/Formatter.h"
  19. #include "Poco/Channel.h"
  20. namespace Poco {
  21. namespace Util {
  22. class AbstractConfiguration;
  23. class Util_API LoggingConfigurator
  24. /// This utility class uses a configuration object to configure the
  25. /// logging subsystem of an application.
  26. ///
  27. /// The LoggingConfigurator sets up and connects formatters, channels
  28. /// and loggers. To accomplish its work, the LoggingConfigurator relies on the
  29. /// functionality provided by the LoggingFactory and LoggingRegistry classes.
  30. ///
  31. /// The LoggingConfigurator expects all configuration data to be under a root
  32. /// property named "logging".
  33. ///
  34. /// Configuring Formatters
  35. ///
  36. /// A formatter is configured using the "logging.formatters" property. Every
  37. /// formatter has an internal name, which is only used for referring to it
  38. /// during configuration time. This name becomes part of the property name.
  39. /// Every formatter has a mandatory "class" property, which specifies the actual
  40. /// class implementing the formatter. Any other properties are passed on to
  41. /// the formatter by calling its setProperty() method.
  42. ///
  43. /// A typical formatter definition looks as follows:
  44. /// logging.formatters.f1.class = PatternFormatter
  45. /// logging.formatters.f1.pattern = %s: [%p] %t
  46. /// logging.formatters.f1.times = UTC
  47. ///
  48. /// Configuring Channels
  49. ///
  50. /// A channel is configured using the "logging.channels" property. Like with
  51. /// Formatters, every channel has an internal name, which is used during
  52. /// configuration only. The name becomes part of the property name.
  53. /// Every channel has a mandatory "class" property, which specifies the actual
  54. /// class implementing the channel. Any other properties are passed on to
  55. /// the formatter by calling its setProperty() method.
  56. ///
  57. /// For convenience, the "formatter" property of a channel is treated
  58. /// specifically. The "formatter" property can either be used to refer to
  59. /// an already defined formatter, or it can be used to specify an "inline"
  60. /// formatter definition. In either case, when a "formatter" property is
  61. /// present, the channel is automatically "wrapped" in a FormattingChannel
  62. /// object.
  63. ///
  64. /// Similarly, a channel supports also a "pattern" property, which results
  65. /// in the automatic instantiation of a FormattingChannel object with a
  66. /// connected PatternFormatter.
  67. ///
  68. /// Examples:
  69. /// logging.channels.c1.class = ConsoleChannel
  70. /// logging.channels.c1.formatter = f1
  71. /// logging.channels.c2.class = FileChannel
  72. /// logging.channels.c2.path = ${system.tempDir}/sample.log
  73. /// logging.channels.c2.formatter.class = PatternFormatter
  74. /// logging.channels.c2.formatter.pattern = %s: [%p] %t
  75. /// logging.channels.c3.class = ConsoleChannel
  76. /// logging.channels.c3.pattern = %s: [%p] %t
  77. ///
  78. /// Configuring Loggers
  79. ///
  80. /// A logger is configured using the "logging.loggers" property. Like with
  81. /// channels and formatters, every logger has an internal name, which, however,
  82. /// is only used to ensure the uniqueness of the property names. Note that this
  83. /// name is different from the logger's full name, which is used to access
  84. /// the logger at runtime.
  85. /// Every logger except the root logger has a mandatory "name" property which
  86. /// is used to specify the logger's full name.
  87. /// Furthermore, a "channel" property is supported, which can either refer
  88. /// to a named channel, or which can contain an inline channel definition.
  89. ///
  90. /// Examples:
  91. /// logging.loggers.root.channel = c1
  92. /// logging.loggers.root.level = warning
  93. /// logging.loggers.l1.name = logger1
  94. /// logging.loggers.l1.channel.class = ConsoleChannel
  95. /// logging.loggers.l1.channel.pattern = %s: [%p] %t
  96. /// logging.loggers.l1.level = information
  97. {
  98. public:
  99. LoggingConfigurator();
  100. /// Creates the LoggingConfigurator.
  101. ~LoggingConfigurator();
  102. /// Destroys the LoggingConfigurator.
  103. void configure(AbstractConfiguration* pConfig);
  104. /// Configures the logging subsystem based on
  105. /// the given configuration.
  106. ///
  107. /// A ConfigurationView can be used to pass only
  108. /// a part of a larger configuration.
  109. private:
  110. void configureFormatters(AbstractConfiguration* pConfig);
  111. void configureChannels(AbstractConfiguration* pConfig);
  112. void configureLoggers(AbstractConfiguration* pConfig);
  113. Poco::Formatter* createFormatter(AbstractConfiguration* pConfig);
  114. Poco::Channel* createChannel(AbstractConfiguration* pConfig);
  115. void configureChannel(Channel* pChannel, AbstractConfiguration* pConfig);
  116. void configureLogger(AbstractConfiguration* pConfig);
  117. LoggingConfigurator(const LoggingConfigurator&);
  118. LoggingConfigurator& operator = (const LoggingConfigurator&);
  119. };
  120. } } // namespace Poco::Util
  121. #endif // Util_LoggingConfigurator_INCLUDED