Apache Log4cxx  Version 1.6.0
Loading...
Searching...
No Matches
com/foo/config4.cpp

This file is a simpler example of how to use the current module name to select the Log4cxx configuration file.

/*
* 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 "config.h"
#include "product_version.h" // Provides getVendorFolder() and getProductFolder()
#include <vector>
namespace com { namespace foo {
// Retrieve the \c name logger pointer.
// Configure Log4cxx on the first call.
auto getLogger(const std::string& name) -> LoggerPtr {
using namespace log4cxx;
static struct log4cxx_initializer {
log4cxx_initializer() {
auto vendorFolder = getVendorFolder();
auto productFolder = getProductFolder();
LOG4CXX_DECODE_CHAR(lsVendorFolder, vendorFolder);
LOG4CXX_DECODE_CHAR(lsProductFolder, productFolder);
// Allow expansion of ${CURRENT_VENDOR_FOLDER} and ${CURRENT_PRODUCT_FOLDER}
// when loading a configuration from a file
#ifndef WIN32
props.setProperty(LOG4CXX_STR("LocalAppData"), LOG4CXX_STR("/var/local"));
#endif
props.setProperty(LOG4CXX_STR("CURRENT_VENDOR_FOLDER"), lsVendorFolder);
props.setProperty(LOG4CXX_STR("CURRENT_PRODUCT_FOLDER"), lsProductFolder);
// Check every 5 seconds for configuration file changes
// Look for a configuration file in the current working directory
// and the same directory as the program
std::vector<LogString> paths
{ LOG4CXX_STR(".")
, LOG4CXX_STR("${PROGRAM_FILE_PATH.PARENT_PATH}")
};
std::vector<LogString> names
{ LOG4CXX_STR("${PROGRAM_FILE_PATH.STEM}.xml")
, LOG4CXX_STR("${PROGRAM_FILE_PATH.STEM}.properties")
};
auto selectedPath = LogString();
std::tie(status, selectedPath) = DefaultConfigurator::configureFromFile(paths, names);
BasicConfigurator::configure(); // Send events to the console
}
~log4cxx_initializer() {
}
} initialiser;
return name.empty()
}
} } // namespace com::foo
static void configure(const LayoutPtr &layout=LayoutPtr())
Add a ConsoleAppender to the root logger that formats output using layout.
static void setConfigurationWatchSeconds(int seconds)
Make seconds the time a background thread will delay before checking for a change to the configuratio...
static std::tuple< spi::ConfigurationStatus, LogString > configureFromFile(const std::vector< LogString > &directories, const std::vector< LogString > &filenames)
Call configure() passing the default repository after calling setConfigurationFileName() with a path ...
static LoggerPtr getRootLogger()
Retrieve the root logger from the LoggerRepository.
static LoggerPtr getLogger(const std::string &name)
Retrieve the name Logger instance from the LoggerRepository using the default factory to create it if...
static void shutdown()
Safely close and remove all appenders in all loggers including the root logger.
static helpers::Properties & properties()
The key value pairs used when expanding ${varname} instances in a configuration file.
#define LOG4CXX_DECODE_CHAR(var, src)
Create a log4cxx::LogString equivalent of src.
Definition log4cxx/helpers/transcoder.h:243
@ NotConfigured
Definition configurator.h:33
Definition configuration.h:25
std::basic_string< logchar > LogString
Definition logstring.h:60