问题描述
我想附加主机名和日期为日志文件名称.SO日志文件名应该像 app_hostname.date.log . 注意:这应该在 Linux和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.我希望匹配模式. 预先感谢
推荐答案
首先从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");
更新:
如果您的应用程序是Web应用程序, 然后需要配置我们想要在tomcat-server开始之后和任何application运行之前的属性,
为此,创建一个类ApplicationConfiguration已实现了ServletContextListener接口,这有助于在此处运行之前先运行.
同样,
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文件,
<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文件,
<web-app ...> <listener> <listener-class> com.pck1.ApplicationConfiguration </listener-class> </listener> </web-app>
此配置需要应用于web.xml,因为应用程序 启动时,通过这种配置,它将像 上下文列表.
更新2:
<logger name="packageName.AAA" additivity="false" > <level value="INFO" /> <appender-ref ref="applog"/> </logger>
其他推荐答案
遵循log4j2 文档您可以执行环境变量查找,因此在类似Unix的系统中应该有效:
<Property name="MYHOST">${env:HOSTNAME}</Property> <Appenders> <File name="File1" fileName="${MYHOST}_file.log"> ... </File> </Appenders>
请注意,$ hostName并不总是默认情况下可用,您可能需要在外壳中明确导出它,请参阅此帖子.
其他推荐答案
我发现默认情况下${hostName}将适用于最新 log4j
类似的东西:
<File name="file" fileName="${baseDir}/${hostName}-file.log" append="true">
这是在此处记录的: /Manual/configuration.html#automaticconfiguration
在默认适当位置 e节:
默认地图预先填充了"主机名" 的值,这是当前系统的主机名称或IP地址
问题描述
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>
And how to add filter based on the log pattern, not as StringMatchFilter.I want pattern to be matched. Thanks in advance
推荐答案
Do this first from your java code then configure log4j into application,
NOTE : handle or catch required Exception while below code execute.
// 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");
UPDATED :
If your application is web-application, then need to configure property which we want here aftertomcat-server start and before any application run,
for that create one class ApplicationConfiguration which has ServletContextListener interface implemented which helps here to run first before any application runs.
do likewise,
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(); } } }
......
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>
please, update web.xml file accordingly,
<web-app ...> <listener> <listener-class> com.pck1.ApplicationConfiguration </listener-class> </listener> </web-app>
This configuration need to apply into web.xml because application when start, by this configuration it will follow it like Context-listener.
UPDATE 2 :
<logger name="packageName.AAA" additivity="false" > <level value="INFO" /> <appender-ref ref="applog"/> </logger>
其他推荐答案
Following the log4j2 documentation you can do environment variable lookups, so in Unix-like systems this should work:
<Property name="MYHOST">${env:HOSTNAME}</Property> <Appenders> <File name="File1" fileName="${MYHOST}_file.log"> ... </File> </Appenders>
Beware that $HOSTNAME is not always available by default and you might need to export it explicitly in the shell, see this post.
其他推荐答案
I found that just ${hostName} by default would work for latest log4j
Something like this:
<File name="file" fileName="${baseDir}/${hostName}-file.log" append="true">
This is documented under here: https://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration
In the Default Properites Section:
The default map is pre-populated with a value for "hostName" that is the current system's host name or IP address