如何将主机名附加到log4j.xml中的日志文件 [英] How to append hostname to log file in log4j.xml
问题描述
我要在日志文件名后附加主机名和日期.因此日志文件名应类似于 app_hostname.date.log . 注意:这应该同时在 linux和Windows中运行.
I want to append hostname and date to log file name.So log file Name should be like app_hostname.date.log. Note: This should run in both linux and windows.
<appender name="applog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${path}/app.log" />
<param name="MaxFileSize" value="1MB" />
<param name="DatePattern" value=".dd-MM-yyyy" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%-5p] %m%n"/>
</layout>
</appender>
以及如何基于日志模式(而不是StringMatchFilter
)添加过滤器.我希望模式匹配.
预先感谢
And how to add filter based on the log pattern, not as StringMatchFilter
.I want pattern to be matched.
Thanks in advance
推荐答案
首先从Java代码执行此操作 然后 配置log4j 到应用程序中,
注意:在执行以下代码时处理或捕获所需的异常.
// step-1 : set hostName into System's property, which will use by log4j
System.setProperty("hostName", InetAddress.getLocalHost().getHostName());
//step - 2 : set currentDate into System's property, which will use by log4j
System.setProperty("currentDate", new SimpleDateFormat("dd-MMM-yyyy").format(new Date()));
//step - 3 : now configure/load log4j into Application , if it's not still loaded earlier then.
org.apache.log4j.Logger LOG = Logger.getLogger(YourJavaClassName.class); // ALERT : before this step above 2-step must needs to be execute, otherwise file-name won't appear as you required.
//LOG.debug("anything whatever programmer what to log");
已更新:
如果您的应用程序是网络应用程序,
然后需要配置在tomcat-server
开始之后和任何application
运行之前我们想要的属性
If your application is web-application,
then need to configure property which we want here aftertomcat-server
start and before any application
run,
为此创建一个类ApplicationConfiguration
,该类已实现ServletContextListener
接口,该接口有助于在任何应用程序运行之前先运行.
for that create one class ApplicationConfiguration
which has ServletContextListener
interface implemented which helps here to run first before any application runs.
也这样做,
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ApplicationConfiguration implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
try {
// step-1 : set hostName into System's property, which will use by log4j
System.setProperty("hostName", InetAddress.getLocalHost().getHostName());
//step - 2 : set currentDate into System's property, which will use by log4j
System.setProperty("currentDate", new SimpleDateFormat("dd-MMM-yyyy").format(new Date()));
} catch (UnknownHostException e) {
System.out.println("Error Message : " + e.getMessage());
//e.printStackTrace();
}
}
}
......
同样设置您的log4j.xml文件,
Set your log4j.xml file likewise,
<appender name="applog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${path}/app_${hostName}.${currentDate}.log" />
<param name="MaxFileSize" value="1MB" />
<param name="DatePattern" value=".dd-MM-yyyy" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd-MM-yyyy HH:mm:ss}] [%-5p] %m%n"/>
</layout>
</appender>
请相应地更新web.xml文件,
please, update web.xml file accordingly,
<web-app ...>
<listener>
<listener-class>
com.pck1.ApplicationConfiguration
</listener-class>
</listener>
</web-app>
此配置需要应用到
web.xml
,因为应用 启动时,通过此配置,它将像 上下文侦听器.
This configuration need to apply into
web.xml
because application when start, by this configuration it will follow it like Context-listener.
更新2:
<logger name="packageName.AAA" additivity="false" >
<level value="INFO" />
<appender-ref ref="applog"/>
</logger>
这篇关于如何将主机名附加到log4j.xml中的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!