首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Log4cxx属性文件变量替换

Log4cxx属性文件变量替换
EN

Stack Overflow用户
提问于 2020-03-25 16:31:38
回答 1查看 522关注 0票数 0

我有一个用于log4cxx配置的属性文件,其变量名为${ name }

属性文件示例:

代码语言:javascript
运行
复制
log4j.rootLogger=INFO, FILE

# FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=./${name}
log4j.appender.FILE.MaxFileSize=16MB
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %m%n

如何从cpp文件或cmake文件中设置变量${name}。我尝试使用以下代码,但它不起作用:

代码语言:javascript
运行
复制
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/properties.h>

using namespace log4cxx;

static LoggerPtr logger(Logger::getLogger(""));

int main() {
    PropertyConfigurator::configure("./assets/logger.properties");

#ifndef NDEBUG
    logger->setLevel(log4cxx::Level::getDebug());
#endif

    return EXIT_SUCCESS;
}

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-29 16:05:53

找到解决方案:使用来自塞滕夫stdlib。以下是工作解决方案:

代码语言:javascript
运行
复制
#include <stdlib.h> // Include the library
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/properties.h>

using namespace log4cxx;

static LoggerPtr logger(Logger::getLogger(""));

int main() {
    // Set the env variable
    // The identifier must be the same as the one in the log4cxx configuration file
    setenv("name", "NAME_OF_THE_CURRENT_LOG_FILE", true);

    PropertyConfigurator::configure("./assets/logger.properties");

#ifndef NDEBUG
    logger->setLevel(log4cxx::Level::getDebug());
#endif

    return EXIT_SUCCESS;
}

我希望它不仅对我有用:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60853131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档