📄 propertyconfigurator.cpp
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <log4cxx/logstring.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/spi/loggerfactory.h>
#include <log4cxx/helpers/properties.h>
#include <log4cxx/helpers/loglog.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/logmanager.h>
#include <log4cxx/helpers/optionconverter.h>
#include <log4cxx/level.h>
#include <log4cxx/defaultloggerfactory.h>
#include <log4cxx/helpers/stringhelper.h>
#include <log4cxx/appender.h>
#include <log4cxx/logger.h>
#include <log4cxx/layout.h>
#include <log4cxx/config/propertysetter.h>
#include <log4cxx/spi/loggerrepository.h>
#include <log4cxx/helpers/stringtokenizer.h>
#include <log4cxx/helpers/synchronized.h>
#include <apr_file_io.h>
#include <apr_file_info.h>
#include <apr_pools.h>
#include <log4cxx/helpers/transcoder.h>
#include <log4cxx/helpers/fileinputstream.h>
using namespace log4cxx;
using namespace log4cxx::spi;
using namespace log4cxx::helpers;
using namespace log4cxx::config;
#if APR_HAS_THREADS
#include <log4cxx/helpers/filewatchdog.h>
class PropertyWatchdog : public FileWatchdog
{
public:
PropertyWatchdog(const File& filename) : FileWatchdog(filename)
{
}
/**
Call PropertyConfigurator#doConfigure(const String& configFileName,
const spi::LoggerRepositoryPtr& hierarchy) with the
<code>filename</code> to reconfigure log4cxx.
*/
void doOnChange()
{
PropertyConfigurator().doConfigure(file,
LogManager::getLoggerRepository());
}
};
#endif
IMPLEMENT_LOG4CXX_OBJECT(PropertyConfigurator)
PropertyConfigurator::PropertyConfigurator()
: registry(new std::map<LogString, AppenderPtr>()), loggerFactory(new DefaultLoggerFactory())
{
}
PropertyConfigurator::~PropertyConfigurator() {
delete registry;
}
void PropertyConfigurator::addRef() const {
ObjectImpl::addRef();
}
void PropertyConfigurator::releaseRef() const {
ObjectImpl::releaseRef();
}
void PropertyConfigurator::doConfigure(const File& configFileName,
spi::LoggerRepositoryPtr& hierarchy)
{
hierarchy->setConfigured(true);
Properties props;
try {
InputStreamPtr inputStream = new FileInputStream(configFileName);
props.load(inputStream);
} catch(const IOException& ie) {
LogLog::error(((LogString) LOG4CXX_STR("Could not read configuration file ["))
+ configFileName.getPath() + LOG4CXX_STR("]."));
return;
}
try {
doConfigure(props, hierarchy);
} catch(const std::exception& ex) {
LogLog::error(((LogString) LOG4CXX_STR("Could not parse configuration file ["))
+ configFileName.getPath() + LOG4CXX_STR("]."), ex);
}
}
void PropertyConfigurator::configure(const File& configFilename)
{
PropertyConfigurator().doConfigure(configFilename, LogManager::getLoggerRepository());
}
void PropertyConfigurator::configure(helpers::Properties& properties)
{
PropertyConfigurator().doConfigure(properties, LogManager::getLoggerRepository());
}
#if APR_HAS_THREADS
void PropertyConfigurator::configureAndWatch(const File& configFilename)
{
configureAndWatch(configFilename, FileWatchdog::DEFAULT_DELAY);
}
void PropertyConfigurator::configureAndWatch(
const File& configFilename, long delay)
{
PropertyWatchdog * pdog = new PropertyWatchdog(configFilename);
pdog->setDelay(delay);
pdog->start();
}
#endif
void PropertyConfigurator::doConfigure(helpers::Properties& properties,
spi::LoggerRepositoryPtr& hierarchy)
{
hierarchy->setConfigured(true);
static const LogString DEBUG_KEY(LOG4CXX_STR("log4j.debug"));
LogString value(properties.getProperty(DEBUG_KEY));
if (!value.empty())
{
LogLog::setInternalDebugging(OptionConverter::toBoolean(value, true));
}
static const LogString THRESHOLD_PREFIX(LOG4CXX_STR("log4j.threshold"));
LogString thresholdStr =
OptionConverter::findAndSubst(THRESHOLD_PREFIX, properties);
if (!thresholdStr.empty())
{
hierarchy->setThreshold(OptionConverter::toLevel(thresholdStr, Level::getAll()));
LogLog::debug(((LogString) LOG4CXX_STR("Hierarchy threshold set to ["))
+ hierarchy->getThreshold()->toString()
+ LOG4CXX_STR("]."));
}
configureRootLogger(properties, hierarchy);
configureLoggerFactory(properties);
parseCatsAndRenderers(properties, hierarchy);
LogLog::debug(LOG4CXX_STR("Finished configuring."));
// We don't want to hold references to appenders preventing their
// destruction.
registry->clear();
}
void PropertyConfigurator::configureLoggerFactory(helpers::Properties& props)
{
static const LogString LOGGER_FACTORY_KEY(LOG4CXX_STR("log4j.loggerFactory"));
LogString factoryClassName =
OptionConverter::findAndSubst(LOGGER_FACTORY_KEY, props);
if (!factoryClassName.empty())
{
LogString msg(LOG4CXX_STR("Setting logger factory to ["));
msg += factoryClassName;
msg += LOG4CXX_STR("].");
LogLog::debug(msg);
loggerFactory =
OptionConverter::instantiateByClassName(
factoryClassName, LoggerFactory::getStaticClass(), loggerFactory);
static const LogString FACTORY_PREFIX(LOG4CXX_STR("log4j.factory."));
Pool p;
PropertySetter::setProperties(loggerFactory, props, FACTORY_PREFIX, p);
}
}
void PropertyConfigurator::configureRootLogger(helpers::Properties& props,
spi::LoggerRepositoryPtr& hierarchy)
{
static const LogString ROOT_CATEGORY_PREFIX(LOG4CXX_STR("log4j.rootCategory"));
static const LogString ROOT_LOGGER_PREFIX(LOG4CXX_STR("log4j.rootLogger"));
LogString effectiveFrefix(ROOT_LOGGER_PREFIX);
LogString value = OptionConverter::findAndSubst(ROOT_LOGGER_PREFIX, props);
if (value.empty())
{
value = OptionConverter::findAndSubst(ROOT_CATEGORY_PREFIX, props);
effectiveFrefix = ROOT_CATEGORY_PREFIX;
}
if (value.empty())
{
LogLog::debug(LOG4CXX_STR("Could not find root logger information. Is this OK?"));
}
else
{
LoggerPtr root = hierarchy->getRootLogger();
synchronized sync(root->getMutex());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -