You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-dev@logging.apache.org by ca...@apache.org on 2004/10/22 06:46:34 UTC
cvs commit: logging-log4cxx/src logmanager.cpp
carnold 2004/10/21 21:46:34
Modified: . build.xml
docs introduction.dox
include/log4cxx logmanager.h
src logmanager.cpp
Log:
LOGCXX-26: Default configuration is broken
Revision Changes Path
1.2 +34 -1 logging-log4cxx/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/logging-log4cxx/build.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- build.xml 14 Oct 2004 17:46:44 -0000 1.1
+++ build.xml 22 Oct 2004 04:46:34 -0000 1.2
@@ -300,7 +300,7 @@
value="${build.dir}:${cppunit.bin.dir}:${env.LD_LIBRARY_PATH}"/>
</exec>
- <!-- testcase3 checks default configuration so must be run in isolation -->
+ <!-- test use of log4j.configuration -->
<property name="defaultInit3.properties"
location="${tests.dir}/input/defaultInit3.properties"/>
<exec executable="${log4cxx-testcase3.exe}" dir="${tests.dir}"
@@ -310,6 +310,39 @@
<env key="log4j.configuration"
value="${defaultInit3.properties}"/>
</exec>
+
+ <!-- test of LOG4CXX_CONFIGURATION -->
+ <exec executable="${log4cxx-testcase3.exe}" dir="${tests.dir}"
+ failonerror="true">
+ <env key="LD_LIBRARY_PATH"
+ value="${build.dir}:${cppunit.bin.dir}:${env.LD_LIBRARY_PATH}"/>
+ <env key="LOG4CXX_CONFIGURATION"
+ value="${defaultInit3.properties}"/>
+ </exec>
+
+ <!-- test of log4j.properties -->
+ <mkdir dir="${build.dir}/output"/>
+ <delete file="${build.dir}/log4*.properties"/>
+ <delete file="${build.dir}/log4*.xml"/>
+ <copy tofile="${build.dir}/log4j.properties"
+ file="${defaultInit3.properties}" overwrite="true"/>
+ <exec executable="${log4cxx-testcase3.exe}" dir="${build.dir}"
+ failonerror="true">
+ <env key="LD_LIBRARY_PATH"
+ value="${build.dir}:${cppunit.bin.dir}:${env.LD_LIBRARY_PATH}"/>
+ </exec>
+
+ <!-- modify log4j.properties so if it has precedence the test fails -->
+ <replace file="${build.dir}/log4j.properties" token="D3" value="D4"/>
+ <copy tofile="${build.dir}/log4cxx.properties"
+ file="${defaultInit3.properties}"/>
+ <exec executable="${log4cxx-testcase3.exe}" dir="${build.dir}"
+ failonerror="true">
+ <env key="LD_LIBRARY_PATH"
+ value="${build.dir}:${cppunit.bin.dir}:${env.LD_LIBRARY_PATH}"/>
+ </exec>
+
+
</target>
1.8 +40 -45 logging-log4cxx/docs/introduction.dox
Index: introduction.dox
===================================================================
RCS file: /home/cvs/logging-log4cxx/docs/introduction.dox,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- introduction.dox 21 Oct 2004 20:44:30 -0000 1.7
+++ introduction.dox 22 Oct 2004 04:46:34 -0000 1.8
@@ -1,9 +1,9 @@
-/**
+/**
@page Introduction Short introduction to log4cxx
This document is largely inspired of the
<a href=http://logging.apache.org/log4j/docs/manual.html>Short introduction to
-log4j</a> by <i>Ceki G�lc�</i>
+log4j</a> by <i>Ceki G�lc�</i>
@section Contents
- @ref Abstract
@@ -26,15 +26,15 @@
with arbitrary granularity. It is fully configurable at runtime using external
configuration files. Best of all, log4cxx has a gentle learning curve. Beware:
judging from user feedback, it is also quite addictive.
-
+
@section IntroductionIntroduction Introduction
The log4cxx framework is based on log4j, see http://logging.apache.org/log4j
for more information on log4cxx.
-The package is distributed under the
+The package is distributed under the
<a href=http://logging.apache.org/log4j/LICENSE.txt>Apache Software License
-</a>, a fully-fledged open source license certified by the
+</a>, a fully-fledged open source license certified by the
<a href=http://www.opensource.org/> open source</a> initiative. The latest
log4cxx version, including full-source code, class files and documentation can
be found at http://logging.apache.org/log4cxx .
@@ -99,7 +99,7 @@
in two ways:
-# it always exists,
--# it cannot be retrieved by name.
+-# it cannot be retrieved by name.
Invoking the class static log4cxx::Logger::getRootLogger method retrieves it.
All other loggers are instantiated and retrieved with the class static
@@ -110,26 +110,26 @@
@code
namespace log4cxx {
typedef std::string String;
-
+
class Logger {
public:
// Creation & retrieval methods:
static LoggerPtr getRootLogger();
static LoggerPtr getLogger(const String& name);
-
+
// printing methods:
void debug(const String& message, const char* file = 0, int line = -1);
void info(const String& message, const char* file = 0, int line = -1);
void warn(const String& message, const char* file = 0, int line = -1);
void error(const String& message, const char* file = 0, int line = -1);
void fatal(const String& message, const char* file = 0, int line = -1);
-
+
// generic printing method:
void log(const LevelPtr& l, const String& message,
const char* file = 0, int line = -1);
};
}
-
+
//
// Use these instead of calling Logger methods directly.
//
@@ -140,7 +140,7 @@
#define LOG4CXX_FATAL(logger, msg) ...
@endcode
-Loggers may be assigned levels. The set of possible levels, that is {@link
+Loggers may be assigned levels. The set of possible levels, that is {@link
log4cxx::Level::DEBUG DEBUG}, {@link log4cxx::Level::INFO INFO}, {@link
log4cxx::Level::WARN WARN}, {@link log4cxx::Level::ERROR ERROR} and {@link
log4cxx::Level::FATAL FATAL} are defined in the log4cxx::Level class. Although
@@ -167,7 +167,7 @@
always has an assigned level.
Below are four tables with various assigned level values and the resulting
-inherited levels according to the above rule.
+inherited levels according to the above rule.
<table border="1" >
<caption align=bottom>Example 1</caption>
@@ -264,7 +264,7 @@
This rule is at the heart of log4cxx. It assumes that levels are ordered. For
the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.
-Here is an example of this rule.
+Here is an example of this rule.
@code
// get a logger instance named "com.foo"
@@ -336,11 +336,11 @@
on their logger is only part of the picture. Log4cxx allows logging
requests to print to multiple destinations. In log4cxx speak, an output
destination is called an <em>appender</em>. Currently, appenders exist
-for the {@link log4cxx::ConsoleAppender console}, {@link
-log4cxx::FileAppender files}, GUI components, {@link
+for the {@link log4cxx::ConsoleAppender console}, {@link
+log4cxx::FileAppender files}, GUI components, {@link
log4cxx::net::SocketAppender remote socket} servers, {@link
log4cxx::nt::NTEventLogAppender NT Event Loggers}, and remote UNIX {@link
-log4cxx::net::SyslogAppender Syslog} daemons.
+log4cxx::net::SyslogAppender Syslog} daemons.
It is also possible to log {@link log4cxx::AsyncAppender asynchronously}.
More than one appender can be attached to a logger.
@@ -357,7 +357,7 @@
<em>C</em>, then enabled logging requests for <em>C</em> and
<em>C</em>'s children will print on a file <em>and</em> on the
console. It is possible to override this default behavior so that
-appender accumulation is no longer additive by {@link
+appender accumulation is no longer additive by {@link
log4cxx::Logger::setAdditivity setting
the additivity flag} to <code>false</code>.
@@ -543,7 +543,7 @@
The invocation of the {@link log4cxx::BasicConfigurator::configure
BasicConfigurator::configure} method creates a rather simple log4cxx setup.
-This method is hardwired to add to the root logger {@link
+This method is hardwired to add to the root logger {@link
log4cxx::ConsoleAppender ConsoleAppender}. The output will be formatted using a
{@link log4cxx::PatternLayout PatternLayout} set
to the pattern "\%-4r [\%t] \%-5p \%c \%x - \%m\%n".
@@ -724,35 +724,30 @@
environment. In particular, there are no default log4cxx
appenders. Under certain well-defined circumstances however, the
initialization of the logger hierarchy will attempt to
-automatically configure log4cxx.
+automatically configure log4cxx.
The exact default initialization algorithm is defined as follows:
--# Set the <code>configurationOptionStr</code> string variable to the value of
-the <b>log4j.configuration</b> environment variable. <em>The preferred
-way to specify the default initialization file is through the
-<b>log4j.configuration</b> environment variable.</em> In case the environment
-variable <b>log4j.configuration</b> is not defined, then set the
-string variable <code>configurationOptionStr</code> to its default value
-"log4j.properties".
-@n @n
--# Attempt to convert the <code>configurationOptionStr</code> variable to a
-valid file name.
+-# Set the <code>configurationOptionStr</code> string variable
+to the value of the <b>LOG4CXX_CONFIGURATION</b> environment variable if set,
+otherwise the value of the <b>log4j.configuration</b> environment variable if set,
+otherwise the first of the following file names which exist in the current
+working directory, "log4cxx.properties", "log4j.properties",
+"log4cxx.xml" and "log4j.xml". If <code>configurationOptionStr</code>
+has not been set, then disable logging.
@n @n
--# If no file could be found, abort default
-initialization. Otherwise, configure log4cxx from the file name.
-@n @n
-The {@link log4cxx::PropertyConfigurator PropertyConfigurator}
-will be used to parse the file to configure log4cxx unless the file name ends
+Unless a custom configurator is specified using the
+<b>LOG4CXX_CONFIGURATOR_CLASS</b> or <b>log4j.configuratorClass</b>
+environment variable, the {@link log4cxx::PropertyConfigurator PropertyConfigurator}
+will be used to configure log4cxx unless the file name ends
with the ".xml" extension, in which case the {@link
log4cxx::xml::DOMConfigurator DOMConfigurator}
-will be used. You can optionaly specify a custom configurator. The
-value of the <b>log4j.configuratorClass</b> environment variable is taken
-as the fully qualified class name of your custom configurator. The
-custom configurator you specify <em>must</em> implement the {@link
-log4cxx::spi::Configurator Configurator} interface.
+will be used. If a custom configurator is specified,
+the environment variable should contain a fully qualified class name
+of a class that implements the {@link
+log4cxx::spi::Configurator Configurator} interface.
-@section NDC Nested Diagnostic Contexts
+@section NDC Nested Diagnostic Contexts
Most real-world systems have to deal with multiple clients
simultaneously. In a typical multithreaded implementation of such a
@@ -842,7 +837,7 @@
parameter construction.
@n @n
For example, for some logger <code>logger</code>, writing,
-@code
+@code
logger->debug("The user named [" + strName + "] is logged", __FILE__, __LINE__);
@endcode
incurs the cost of constructing the message parameter, i.e.
@@ -853,15 +848,15 @@
depends on the size of the parameters involved.
@n @n
To avoid the parameter construction cost write:
-@code
+@code
if(logger->isDebugEnabled()
{
- logger->forcedLog(Level::DEBUG,
+ logger->forcedLog(Level::DEBUG,
"The user named [" + strName + "] is logged", __FILE__, __LINE__);
}
-@endcode
+@endcode
or better yet:
-@code
+@code
LOG4CXX_DEBUG(logger, "The user named [" + strName + "] is logged");
@endcode
1.13 +2 -0 logging-log4cxx/include/log4cxx/logmanager.h
Index: logmanager.h
===================================================================
RCS file: /home/cvs/logging-log4cxx/include/log4cxx/logmanager.h,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- logmanager.h 21 Oct 2004 04:53:43 -0000 1.12
+++ logmanager.h 22 Oct 2004 04:46:34 -0000 1.13
@@ -44,6 +44,8 @@
private:
static void * guard;
static spi::RepositorySelectorPtr& getRepositorySelector();
+ static const String getConfigurationFileName();
+ static const String LogManager::getConfiguratorClass();
public:
/**
1.10 +43 -15 logging-log4cxx/src/logmanager.cpp
Index: logmanager.cpp
===================================================================
RCS file: /home/cvs/logging-log4cxx/src/logmanager.cpp,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- logmanager.cpp 21 Oct 2004 04:53:44 -0000 1.9
+++ logmanager.cpp 22 Oct 2004 04:46:34 -0000 1.10
@@ -27,10 +27,6 @@
#include <log4cxx/helpers/loglog.h>
#include <sys/stat.h>
-#define DEFAULT_CONFIGURATION_FILE _T("log4j.properties")
-#define DEFAULT_XML_CONFIGURATION_FILE _T("log4j.xml")
-#define DEFAULT_CONFIGURATION_KEY _T("log4j.configuration")
-#define CONFIGURATOR_CLASS_KEY _T("log4j.configuratorClass")
using namespace log4cxx;
using namespace log4cxx::spi;
@@ -65,6 +61,31 @@
LogManager::getRepositorySelector() = selector;
}
+const String LogManager::getConfiguratorClass() {
+
+ static const String LOG4J_CONFIGURATOR_CLASS_KEY("log4j.configuratorClass");
+ static const String LOG4CXX_CONFIGURATOR_CLASS_KEY("LOG4CXX_CONFIGURATOR_CLASS");
+ // Use automatic configration to configure the default hierarchy
+ const String log4jConfiguratorClassName(
+ OptionConverter::getSystemProperty(LOG4J_CONFIGURATOR_CLASS_KEY,_T("")));
+ const String configuratorClassName(
+ OptionConverter::getSystemProperty(LOG4CXX_CONFIGURATOR_CLASS_KEY,
+ log4jConfiguratorClassName));
+ return configuratorClassName;
+}
+
+const String LogManager::getConfigurationFileName() {
+ static const String LOG4CXX_DEFAULT_CONFIGURATION_KEY("LOG4CXX_CONFIGURATION");
+ static const String LOG4J_DEFAULT_CONFIGURATION_KEY("log4j.configuration");
+ const String log4jConfigurationOptionStr(
+ OptionConverter::getSystemProperty(LOG4J_DEFAULT_CONFIGURATION_KEY,_T("")));
+ const String configurationOptionStr(
+ OptionConverter::getSystemProperty(LOG4CXX_DEFAULT_CONFIGURATION_KEY,
+ log4jConfigurationOptionStr));
+ return configurationOptionStr;
+}
+
+
LoggerRepositoryPtr& LogManager::getLoggerRepository()
{
if (getRepositorySelector() == 0)
@@ -74,22 +95,29 @@
new Hierarchy(
new RootCategory(Level::getDebug())));
- // Use automatic configration to configure the default hierarchy
- String configuratorClassName =
- OptionConverter::getSystemProperty(CONFIGURATOR_CLASS_KEY,_T(""));
- String configurationOptionStr =
- OptionConverter::getSystemProperty(DEFAULT_CONFIGURATION_KEY,_T(""));
+ const String configuratorClassName(getConfiguratorClass());
+
+ String configurationOptionStr(getConfigurationFileName());
struct stat buff;
- USES_CONVERSION;
if (configurationOptionStr.empty())
{
- configurationOptionStr = DEFAULT_XML_CONFIGURATION_FILE;
- if (stat(T2A(configurationOptionStr.c_str()), &buff) == -1)
- {
- configurationOptionStr = DEFAULT_CONFIGURATION_FILE;
- }
+ configurationOptionStr = "log4cxx.properties";
+ const char* configFilenames[] = {
+ "log4cxx.properties",
+ "log4j.properties",
+ "log4cxx.xml",
+ "log4j.xml",
+ NULL };
+ for (const char** configFile = configFilenames;
+ *configFile != NULL;
+ configFile++) {
+ if (stat(*configFile, &buff) == 0) {
+ configurationOptionStr = *configFile;
+ break;
+ }
+ }
}
if (stat(T2A(configurationOptionStr.c_str()), &buff) == 0)