You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rm...@apache.org on 2022/12/29 13:59:32 UTC

[logging-log4cxx] branch next_stable updated: LOGCXX-574 Provide a list of files/directories to look in for files (#173)

This is an automated email from the ASF dual-hosted git repository.

rmiddleton 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 96f50bda LOGCXX-574 Provide a list of files/directories to look in for files (#173)
96f50bda is described below

commit 96f50bdaab7d76d6bbe4fcc6fd6e041586e8ecc9
Author: Robert Middleton <rm...@users.noreply.github.com>
AuthorDate: Thu Dec 29 08:59:27 2022 -0500

    LOGCXX-574 Provide a list of files/directories to look in for files (#173)
---
 src/main/cpp/defaultconfigurator.cpp           | 37 ++++++++++++++++++++++++++
 src/main/include/log4cxx/defaultconfigurator.h | 30 +++++++++++++++++++++
 2 files changed, 67 insertions(+)

diff --git a/src/main/cpp/defaultconfigurator.cpp b/src/main/cpp/defaultconfigurator.cpp
index 0fdd99e7..a8bdddb7 100644
--- a/src/main/cpp/defaultconfigurator.cpp
+++ b/src/main/cpp/defaultconfigurator.cpp
@@ -22,6 +22,8 @@
 #include <log4cxx/helpers/loglog.h>
 #include <log4cxx/helpers/optionconverter.h>
 #include <log4cxx/helpers/stringhelper.h>
+#include <log4cxx/xml/domconfigurator.h>
+#include <log4cxx/propertyconfigurator.h>
 
 using namespace log4cxx;
 using namespace log4cxx::spi;
@@ -150,6 +152,41 @@ int DefaultConfigurator::getConfigurationWatchDelay()
 	return milliseconds;
 }
 
+log4cxx::spi::ConfigurationStatus DefaultConfigurator::tryLoadFile(const LogString& filename){
+	if(helpers::StringHelper::endsWith(filename, ".xml")){
+		return log4cxx::xml::DOMConfigurator::configure(filename);
+	}else if(helpers::StringHelper::endsWith(filename, ".properties")){
+		return log4cxx::PropertyConfigurator::configure(filename);
+	}
+
+	return log4cxx::spi::ConfigurationStatus::NotConfigured;
+}
+
+std::tuple<log4cxx::spi::ConfigurationStatus,LogString>
+DefaultConfigurator::configureFromFile(const std::vector<LogString>& directories, const std::vector<LogString>& filenames){
+	log4cxx::helpers::Pool pool;
+
+	for( LogString dir : directories ){
+		for( LogString fname : filenames ){
+			LogString canidate_str = dir + "/" + fname;
+			File candidate(canidate_str);
+
+			LogString debugMsg = LOG4CXX_STR("Checking file ");
+			debugMsg.append(canidate_str);
+			LogLog::debug(debugMsg);
+			if (candidate.exists(pool))
+			{
+				log4cxx::spi::ConfigurationStatus configStatus = tryLoadFile(canidate_str);
+				if( configStatus == log4cxx::spi::ConfigurationStatus::Configured ){
+					return {configStatus, canidate_str};
+				}
+				LogLog::debug("Unable to load file: trying next");
+			}
+		}
+	}
+
+	return {log4cxx::spi::ConfigurationStatus::NotConfigured, LogString()};
+}
 
 
 
diff --git a/src/main/include/log4cxx/defaultconfigurator.h b/src/main/include/log4cxx/defaultconfigurator.h
index 59561274..caceac20 100644
--- a/src/main/include/log4cxx/defaultconfigurator.h
+++ b/src/main/include/log4cxx/defaultconfigurator.h
@@ -20,6 +20,7 @@
 
 #include <log4cxx/spi/configurator.h>
 #include <log4cxx/spi/loggerrepository.h>
+#include <tuple>
 
 namespace log4cxx
 {
@@ -70,10 +71,39 @@ class LOG4CXX_EXPORT DefaultConfigurator
 		*/
 		static void setConfigurationWatchSeconds(int seconds);
 
+		/**
+		 * Configure Log4cxx from a file.  This method will attempt to load the configuration files in the
+		 * directories given.
+		 *
+		 * For example, if we want a configuration file named 'myapp-logging.xml' with the default location
+		 * for this file in /etc/myapp, but to have this overriden by a file in /usr/local/etc/myapp, we would
+		 * call this function as follows:
+		 *
+		 * configureFromFile( { "/usr/local/etc/myapp", "/etc/myapp" }, { "myapp-logging.xml" );
+		 *
+		 * This will then search for files in the following order:
+		 *
+		 * <pre>
+		 * /usr/local/etc/myapp/myapp-logging.xml
+		 * /etc/myapp/myapp-logging.xml
+		 * </pre>
+		 *
+		 * The status of configuring Log4cxx as well as the eventual filename used is returned.  If a file exists
+		 * but it is not able to be used to configure Log4cxx, the next file in the list will be tried until
+		 * a valid configuration file is found or the end of the list is reached.
+		 *
+		 * @param directories The directories to look in.
+		 * @param filenamse The names of the files to look for
+		 * @return The status of the configuration, and the filename loaded(if a file was found).
+		 */
+		static std::tuple<log4cxx::spi::ConfigurationStatus,LogString> configureFromFile(const std::vector<LogString>& directories,
+																						 const std::vector<LogString>& filenames);
+
 	private:
 		static const LogString getConfigurationFileName();
 		static const LogString getConfiguratorClass();
 		static int getConfigurationWatchDelay();
+		static log4cxx::spi::ConfigurationStatus tryLoadFile(const LogString& filename);
 
 };	 // class DefaultConfigurator
 }  // namespace log4cxx