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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With