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 2023/09/14 06:19:25 UTC

[logging-log4cxx] branch master updated: Document how to revert to console output when configuration file is missing (#267)

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

swebb2066 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git


The following commit(s) were added to refs/heads/master by this push:
     new 29b2929c Document how to revert to console output when configuration file is missing (#267)
29b2929c is described below

commit 29b2929c852b53b0421476913ac71353503e2ffb
Author: Stephen Webb <st...@ieee.org>
AuthorDate: Thu Sep 14 16:19:19 2023 +1000

    Document how to revert to console output when configuration file is missing (#267)
---
 src/examples/cpp/com/foo/config-qt.cpp       | 7 ++++++-
 src/examples/cpp/com/foo/config2.cpp         | 4 +++-
 src/examples/cpp/com/foo/config3.cpp         | 5 ++++-
 src/main/include/log4cxx/basicconfigurator.h | 9 +++++----
 4 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/examples/cpp/com/foo/config-qt.cpp b/src/examples/cpp/com/foo/config-qt.cpp
index 64889087..68c5fdde 100644
--- a/src/examples/cpp/com/foo/config-qt.cpp
+++ b/src/examples/cpp/com/foo/config-qt.cpp
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 #include "config-qt.h"
+#include <log4cxx/basicconfigurator.h>
 #include <log4cxx/logmanager.h>
 #include <log4cxx-qt/configuration.h>
 #include <log4cxx/helpers/loglog.h>
@@ -51,7 +52,11 @@ void ConfigureLogging() {
 #if defined(_DEBUG)
 	log4cxx::helpers::LogLog::setInternalDebugging(true);
 #endif
-	log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names);
+	auto status       = log4cxx::spi::ConfigurationStatus::NotConfigured;
+	auto selectedPath = QString();
+	std::tie(status, selectedPath) = log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names);
+	if (status == log4cxx::spi::ConfigurationStatus::NotConfigured)
+		log4cxx::BasicConfigurator::configure(); // Send events to the console
 }
 
 // Retrieve the \c name logger pointer.
diff --git a/src/examples/cpp/com/foo/config2.cpp b/src/examples/cpp/com/foo/config2.cpp
index 2c25f8af..8228e374 100644
--- a/src/examples/cpp/com/foo/config2.cpp
+++ b/src/examples/cpp/com/foo/config2.cpp
@@ -1,4 +1,5 @@
 #include "com/foo/config.h"
+#include <log4cxx/basicconfigurator.h>
 #include <log4cxx/propertyconfigurator.h>
 #include <log4cxx/logmanager.h>
 
@@ -7,7 +8,8 @@ namespace com { namespace foo {
 auto getLogger(const std::string& name) -> LoggerPtr {
 	static struct log4cxx_initializer {
 		log4cxx_initializer() {
-			log4cxx::PropertyConfigurator::configure("MyApp.properties");
+			if (log4cxx::PropertyConfigurator::configure("MyApp.properties") == log4cxx::spi::ConfigurationStatus::NotConfigured)
+				log4cxx::BasicConfigurator::configure(); // Send events to the console
 		}
 		~log4cxx_initializer() {
 			log4cxx::LogManager::shutdown();
diff --git a/src/examples/cpp/com/foo/config3.cpp b/src/examples/cpp/com/foo/config3.cpp
index d38ae43c..34974051 100644
--- a/src/examples/cpp/com/foo/config3.cpp
+++ b/src/examples/cpp/com/foo/config3.cpp
@@ -17,6 +17,7 @@
 #include "config.h"
 #include <log4cxx/logmanager.h>
 #include <log4cxx/logstring.h>
+#include <log4cxx/basicconfigurator.h>
 #include <log4cxx/defaultconfigurator.h>
 #include <log4cxx/helpers/pool.h>
 #include <log4cxx/file.h>
@@ -136,8 +137,10 @@ void SelectConfigurationFile() {
 			}
 		}
 		if (extension[i]) // Found a configuration file?
-			break;
+			return;
 	}
+	// Configuration file not found - send events to the console
+	BasicConfigurator::configure();
 }
 
 } // namespace
diff --git a/src/main/include/log4cxx/basicconfigurator.h b/src/main/include/log4cxx/basicconfigurator.h
index 59bf032d..edecfade 100644
--- a/src/main/include/log4cxx/basicconfigurator.h
+++ b/src/main/include/log4cxx/basicconfigurator.h
@@ -29,10 +29,11 @@ class Appender;
 typedef std::shared_ptr<Appender> AppenderPtr;
 
 /**
-Use this class to quickly configure the package.
-<p>For file based configuration see
-PropertyConfigurator. For XML based configuration see
-DOMConfigurator.
+Use BasicConfigurator (static) methods to configure Log4cxx
+when not using a configuration file.
+
+For <code>key=value</code> format configuration see PropertyConfigurator.
+For XML format configuration see xml::DOMConfigurator.
 */
 class LOG4CXX_EXPORT BasicConfigurator
 {