You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by sw...@apache.org on 2022/12/19 05:55:15 UTC
[logging-log4cxx] branch next_stable updated: Allow an alternate layout when using BasicConfigurator (#166)
This is an automated email from the ASF dual-hosted git repository.
swebb2066 pushed a commit to branch next_stable
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
The following commit(s) were added to refs/heads/next_stable by this push:
new f251a144 Allow an alternate layout when using BasicConfigurator (#166)
f251a144 is described below
commit f251a144001fa1bdc0797d59205936c4652d259e
Author: Stephen Webb <st...@ieee.org>
AuthorDate: Mon Dec 19 16:55:10 2022 +1100
Allow an alternate layout when using BasicConfigurator (#166)
---
src/main/cpp/basicconfigurator.cpp | 13 ++++++++-----
src/main/include/log4cxx/basicconfigurator.h | 11 +++++++----
src/test/cpp/rolling/sizebasedrollingtest.cpp | 8 ++++----
src/test/cpp/rolling/timebasedrollingtest.cpp | 19 ++++++++-----------
4 files changed, 27 insertions(+), 24 deletions(-)
diff --git a/src/main/cpp/basicconfigurator.cpp b/src/main/cpp/basicconfigurator.cpp
index 3cf590b9..abe71fa4 100644
--- a/src/main/cpp/basicconfigurator.cpp
+++ b/src/main/cpp/basicconfigurator.cpp
@@ -23,14 +23,17 @@
using namespace log4cxx;
-void BasicConfigurator::configure()
+void BasicConfigurator::configure(const LayoutPtr& layoutArg)
{
LogManager::getLoggerRepository()->setConfigured(true);
- LoggerPtr root = Logger::getRootLogger();
- static const LogString TTCC_CONVERSION_PATTERN(LOG4CXX_STR("%r [%t] %p %c %x - %m%n"));
- LayoutPtr layout = std::make_shared<PatternLayout>(TTCC_CONVERSION_PATTERN);
+ auto layout = layoutArg;
+ if (!layout)
+ {
+ static const LogString TTCC_CONVERSION_PATTERN(LOG4CXX_STR("%r [%t] %p %c %x - %m%n"));
+ layout = std::make_shared<PatternLayout>(TTCC_CONVERSION_PATTERN);
+ }
auto appender = std::make_shared<ConsoleAppender>(layout);
- root->addAppender(appender);
+ Logger::getRootLogger()->addAppender(appender);
}
void BasicConfigurator::configure(const AppenderPtr& appender)
diff --git a/src/main/include/log4cxx/basicconfigurator.h b/src/main/include/log4cxx/basicconfigurator.h
index 282f66e5..59bf032d 100644
--- a/src/main/include/log4cxx/basicconfigurator.h
+++ b/src/main/include/log4cxx/basicconfigurator.h
@@ -41,10 +41,13 @@ class LOG4CXX_EXPORT BasicConfigurator
public:
/**
- Add a ConsoleAppender that uses PatternLayout
- using the PatternLayout#TTCC_CONVERSION_PATTERN and
- prints to <code>stdout</code> to the root logger.*/
- static void configure();
+ Add a ConsoleAppender to the root logger that formats output using \c layout.
+
+ If \c layout is not provided,
+ use a PatternLayout with <code>%%r [%%t] %%p %%c %%x - %%m%%n</code>
+ as the conversion pattern.
+ */
+ static void configure(const LayoutPtr& layout = LayoutPtr());
/**
Add <code>appender</code> to the root logger.
diff --git a/src/test/cpp/rolling/sizebasedrollingtest.cpp b/src/test/cpp/rolling/sizebasedrollingtest.cpp
index 3c1d4bec..ddb0f2d5 100644
--- a/src/test/cpp/rolling/sizebasedrollingtest.cpp
+++ b/src/test/cpp/rolling/sizebasedrollingtest.cpp
@@ -18,6 +18,7 @@
#include "../insertwide.h"
#include "../logunit.h"
#include <apr_time.h>
+#include <log4cxx/basicconfigurator.h>
#include <log4cxx/logmanager.h>
#include <log4cxx/xml/domconfigurator.h>
#include <log4cxx/patternlayout.h>
@@ -64,11 +65,10 @@ LOGUNIT_CLASS(SizeBasedRollingTest)
public:
void setUp()
{
- PatternLayoutPtr layout(new PatternLayout(LOG4CXX_STR("%d %level %c -%m%n")));
- AppenderPtr ca(new ConsoleAppender(layout));
- ca->setName(LOG4CXX_STR("CONSOLE"));
+ BasicConfigurator::configure(std::make_shared<PatternLayout> (
+ LOG4CXX_STR("%d %level %c -%m%n")
+ ));
root = Logger::getRootLogger();
- root->addAppender(ca);
logger = Logger::getLogger("org.apache.log4j.rolling.SizeBasedRollingTest");
}
diff --git a/src/test/cpp/rolling/timebasedrollingtest.cpp b/src/test/cpp/rolling/timebasedrollingtest.cpp
index 9225d458..5b1e0826 100644
--- a/src/test/cpp/rolling/timebasedrollingtest.cpp
+++ b/src/test/cpp/rolling/timebasedrollingtest.cpp
@@ -18,6 +18,7 @@
#include <vector>
#include <log4cxx/rolling/rollingfileappender.h>
+#include <log4cxx/basicconfigurator.h>
#include <log4cxx/logger.h>
#include <log4cxx/consoleappender.h>
#include <log4cxx/logmanager.h>
@@ -85,8 +86,8 @@ LOGUNIT_CLASS(TimeBasedRollingTest)
LOGUNIT_TEST_SUITE_END();
private:
- static LoggerPtr logger;
- log4cxx_time_t current_time;
+ LoggerPtr logger;
+ log4cxx_time_t current_time;
/**
* Build file names with timestamps.
@@ -370,14 +371,12 @@ public:
void setUp()
{
- LoggerPtr root(Logger::getRootLogger());
- root->addAppender(
- ConsoleAppenderPtr(new ConsoleAppender(
- PatternLayoutPtr(new PatternLayout(
- LOG4CXX_STR("%d{ABSOLUTE} [%t] %level %c{2}#%M:%L - %m%n"))))));
-
+ BasicConfigurator::configure(std::make_shared<PatternLayout> (
+ LOG4CXX_STR("%d{ABSOLUTE} [%t] %level %c{2}#%M:%L - %m%n")
+ ));
+ this->logger = LogManager::getLogger("org.apache.log4j.TimeBasedRollingTest");
this->setUpCurrTime();
- log4cxx::helpers::Date::setGetCurrentTimeFunction( std::bind( &TimeBasedRollingTest::currentTime, this ) );
+ helpers::Date::setGetCurrentTimeFunction( std::bind( &TimeBasedRollingTest::currentTime, this ) );
}
void tearDown()
@@ -683,6 +682,4 @@ public:
};
-LoggerPtr TimeBasedRollingTest::logger(Logger::getLogger("org.apache.log4j.TimeBasedRollingTest"));
-
LOGUNIT_TEST_SUITE_REGISTRATION(TimeBasedRollingTest);