13#include <log4cplus/logger.h>
14#include <log4cplus/configurator.h>
15#include <log4cplus/hierarchy.h>
16#include <log4cplus/consoleappender.h>
17#include <log4cplus/fileappender.h>
18#include <log4cplus/syslogappender.h>
19#include <log4cplus/helpers/loglog.h>
20#include <log4cplus/version.h>
34#include <boost/lexical_cast.hpp>
37using boost::lexical_cast;
49 storeBufferAppenders();
51 log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
58 flushBufferAppenders();
65 string const& name(spec.getName());
68 log4cplus::Logger logger = log4cplus::Logger::getInstance(
expandLoggerName(name));
72 Level(spec.getSeverity(), spec.getDbglevel())));
75 logger.setAdditivity(spec.getAdditive());
78 logger.removeAllAppenders();
80 if (name == root_logger_name) {
86 if (spec.optionCount() > 0) {
88 appenderFactory(logger, spec);
93 appenderFactory(logger, root_spec_);
98LoggerManagerImpl::appenderFactory(log4cplus::Logger& logger,
101 switch (i.destination) {
103 createConsoleAppender(logger, i);
107 createFileAppender(logger, i);
111 createSyslogAppender(logger, i);
120 "Unknown logging destination, code = " << i.destination);
127LoggerManagerImpl::createConsoleAppender(log4cplus::Logger& logger,
128 const OutputOption& opt)
130 log4cplus::SharedAppenderPtr console(
131 new log4cplus::ConsoleAppender(
134 setAppenderLayout(console, (opt.pattern.empty() ?
136 logger.addAppender(console);
147LoggerManagerImpl::createFileAppender(log4cplus::Logger& logger,
148 const OutputOption& opt)
151 const std::ios::openmode mode = std::ios::app;
153 log4cplus::SharedAppenderPtr fileapp;
154 if (opt.maxsize == 0) {
155 fileapp = log4cplus::SharedAppenderPtr(
new log4cplus::FileAppender(
156 opt.filename, mode, opt.flush));
158 log4cplus::helpers::Properties properties;
159 properties.setProperty(
"File", opt.filename);
163 uint64_t maxsize(opt.maxsize);
165 while (std::numeric_limits<int32_t>::max() < maxsize && i < 2) {
169 std::array<std::string, 3>
const suffixes({
"",
"KB",
"MB"});
170 std::string
const max_file_size(to_string(maxsize) + suffixes[i]);
174 if (std::numeric_limits<int32_t>::max() < maxsize) {
175 isc_throw(BadValue,
"expected maxsize < "
176 << std::numeric_limits<int32_t>::max()
177 <<
"MB, but instead got " << max_file_size);
180 properties.setProperty(
"MaxFileSize", max_file_size);
181 properties.setProperty(
"MaxBackupIndex",
182 lexical_cast<string>(opt.maxver));
183 properties.setProperty(
"ImmediateFlush", opt.flush ?
"true" :
"false");
184 properties.setProperty(
"UseLockFile",
"true");
185 fileapp = log4cplus::SharedAppenderPtr(
186 new log4cplus::RollingFileAppender(properties));
189 setAppenderLayout(fileapp, (opt.pattern.empty() ?
191 logger.addAppender(fileapp);
195LoggerManagerImpl::createBufferAppender(log4cplus::Logger& logger) {
196 log4cplus::SharedAppenderPtr bufferapp(
new internal::BufferAppender());
197 bufferapp->setName(
"buffer");
198 logger.addAppender(bufferapp);
201 logger.setLogLevel(log4cplus::TRACE_LOG_LEVEL);
206LoggerManagerImpl::createSyslogAppender(log4cplus::Logger& logger,
207 const OutputOption& opt)
209 log4cplus::helpers::Properties properties;
211 properties.setProperty(
"facility", opt.facility);
212 log4cplus::SharedAppenderPtr syslogapp(
213 new log4cplus::SysLogAppender(properties));
214 setAppenderLayout(syslogapp, (opt.pattern.empty() ?
216 logger.addAppender(syslogapp);
229 log4cplus::BasicConfigurator config;
235 initRootLogger(severity, dbglevel, buffer);
245 initRootLogger(severity, dbglevel);
250 int dbglevel,
bool buffer)
252 log4cplus::Logger::getDefaultHierarchy().resetConfiguration();
258 log4cplus::helpers::LogLog::getLogLog()->setQuietMode(
true);
263 log4cplus::Logger::getRoot().setLogLevel(log4cplus::OFF_LOG_LEVEL);
267 log4cplus::Logger kea_root = log4cplus::Logger::getInstance(
270 Level(severity, dbglevel)));
273 createBufferAppender(kea_root);
276 createConsoleAppender(kea_root, opt);
281void LoggerManagerImpl::setAppenderLayout(
282 log4cplus::SharedAppenderPtr& appender,
287#
if LOG4CPLUS_VERSION < LOG4CPLUS_MAKE_VERSION(2, 0, 0)
288 auto_ptr<log4cplus::Layout>
290 unique_ptr<log4cplus::Layout>
292 (
new log4cplus::PatternLayout(pattern)));
295void LoggerManagerImpl::storeBufferAppenders() {
297 log4cplus::LoggerList loggers = log4cplus::Logger::getCurrentLoggers();
298 for (
auto& it : loggers) {
299 log4cplus::SharedAppenderPtr buffer_appender = it.getAppender(
"buffer");
300 if (buffer_appender) {
301 buffer_appender_store_.push_back(buffer_appender);
306void LoggerManagerImpl::flushBufferAppenders() {
307 std::vector<log4cplus::SharedAppenderPtr>
copy;
308 buffer_appender_store_.swap(copy);
310 for (
auto const& it :
copy) {
311 internal::BufferAppender* app =
dynamic_cast<internal::BufferAppender*
>(it.get());
static log4cplus::LogLevel convertFromBindLevel(const isc::log::Level &level)
Convert Kea level to log4cplus logging level.
static void init()
Initialize extended logging levels.
void processSpecification(const LoggerSpecification &spec)
Process Specification.
void processEnd()
End Processing.
static void init(isc::log::Severity severity=isc::log::INFO, int dbglevel=0, bool buffer=false)
Implementation-specific initialization.
static void reset(isc::log::Severity severity=isc::log::INFO, int dbglevel=0)
Reset logging.
void processInit()
Initialize Processing.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
Logging initialization functions.
isc::log::Logger logger("asiodns")
Use the ASIO logger.
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
const std::string & getRootLoggerName()
Get root logger name.
std::string expandLoggerName(const std::string &name)
Expand logger name.
Defines the logger used by the top-level component of kea-lfc.
static const std::string DEFAULT_FILE_PATTERN
Default layout pattern for file logs.
static const std::string DEFAULT_SYSLOG_PATTERN
Default layout pattern for syslog logs.
static const std::string DEFAULT_CONSOLE_PATTERN
Default layout pattern for console logs.