将进程ID添加到log4cxx中的日志文件名 [英] Add process id to log file name in log4cxx
问题描述
在log4net中,我可以轻松地将进程ID设置为从config轻松记录文件名
In log4net i can easily set process id to to log file name from config easily
<appender name="LogFileAppender"
type="log4net.Appender.RollingFileAppender,log4net">
<file type="log4net.Util.PatternString" value="Log[%processid]" />
- 我可以对配置文件中的log4cxx做同样的事情吗?
- 如果是,怎么办?
推荐答案
根据log4cxx文档,当前可以执行此操作 您需要至少声明一个MappedDiagnostic上下文.
According to the log4cxx documentation in order to currently do this you need to declare at least one MappedDiagnostic context.
未测试的部分摘要如下所示
An untested partial snippet of doing so is shown below
#include <sys/types.h>
#include <log4cxx/mdc.h>
#include <iostream>
#include <sstream>
int main (int argc, char **argv)
{
//at the start of your program
pid_t pid = getpid();
pid_t tid = gettid();
std::string pidstring;
std::string tidstring;
std::stringstream buffer;
buffer << pid << std::endl;
pidstring = buffer.str();
buffer.str(std::string());
buffer << tid << std::endl;
tidstring = buffer.str();
buffer.str(std::string());
MDC::put( "pid", pidstring);
MDC::put( "tid", tidstring);
// do actual stuff here
return 0;
}
在进一步检查log4cxx源之后,我意识到该文件没有采用ConversionPattern,但FileNamePattern却具有.我相信只有在使用TimeBasedRollingPolicy或FixedWindowRollingPolicy时,才能使用FileNamePattern.
After further inspection of the log4cxx source, I realised that file doesn't take a ConversionPattern but FileNamePattern does. I believe you can only use FileNamePattern when you have use a TimeBasedRollingPolicy or FixedWindowRollingPolicy.
现在您可以将processid添加到日志中 通过在XML配置文件的appender标签中添加以下参数.
Now you can add the processid to the log by adding the following parameter in your appender tags in the XML configuration file.
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="MyApplication-%d{yyyy-MM-dd}- %X{pid}.log"/>
<param name="activeFileName" value="MyApplication.log"/>
</rollingPolicy>
<param name="file" value="appxDailyLog.log"/>
或者您可以通过在XML配置文件中的附加程序标记内指定以下布局标记来将其包括在模式布局中.
or you can include it in the pattern layout by specifying the following layout tags also inside your appender tags in the XML configuration file.
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%X{pid} %X{tid} %d{yyyy-MM-dd HH:mm:ss,SSS}"/>
</layout>
没有一种简单的方法可以让配置文件像您在log4net中所熟悉的那样,将每个进程将自己的进程附加到自己的日志中.
There is no simple way from the configuration file only to have each process append its own process to its own log like you are familiar with in log4net.
有几个log4cxx邮件列表线程提到了动态日志重命名,但是它们都涉及C ++代码中的许多更改,并且它们无法满足您的要求.
There were several log4cxx mailing list threads that mention dynamic log renaming, but all of them involved numerous changes in the C++ code and they don't do what you request.
他们使用的方法涉及到<param name="file" value="${logfilename}"/>
,其中$ logfilename是由其设置的环境变量
The method they used involves having <param name="file" value="${logfilename}"/>
where $logfilename is an environment variable that gets set by
std::string filename ="MyApp-";
filename.append(pidstring);
logger = Logger::getLogger("Nameoflogger");
setenv("logfile.name", "MyApp.log", 1);
每次您想要更改日志名称时,都在C ++代码中调用类似于上面的代码片段.
calling something like the above snippet in the C++ code, each time you want the log name to change.
其他方法可能会涉及log4cxx的补丁程序,因为它目前不具有您所需的功能.
Other methods would involve patches to log4cxx as it currently doesn't have the functionality you need.
参考
这篇关于将进程ID添加到log4cxx中的日志文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!