Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append hostname to log file in log4j.xml

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

like image 533
Prasanna Kumar H A Avatar asked Mar 23 '16 06:03

Prasanna Kumar H A


People also ask

What is logger name in log4j xml?

In log4j a logger is associated with a package or sometimes with a particular class. Package/class of a logger is defined by the attribute "name". A logger logs messages in its package and also in all the child packages and their classes.


1 Answers

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.

like image 60
Jose Duarte Avatar answered Oct 01 '22 01:10

Jose Duarte