Reputation: 29
I have a web application.I want to write log file of this web application in remote machine using Syslog Appender in log4j.The log file is written in the Syslog server.But the file name is Class.Log. I want to give proper log file name.How can I do this?
Upvotes: 1
Views: 1759
Reputation: 2231
I have a working example:
For log4j2 do not use the Syslog
appender. I had to use Socket
appender in this way (the PatternLayout
element simulates the traditional forwarding format for sending data in the wire, see: smtradfwd.c):
<Configuration status="error" strict="true" monitorInterval="30"
name="XMLConfigTest" packages="org.apache.logging.log4j.test">
<Properties>
<Property name="REMOTE_HOST">some.ip.address</Property>
<Property name="APP_NAME">rest-server</Property>
</Properties>
<Appenders>
<Socket name="REMOTE" host="${REMOTE_HOST}" port="514" protocol="TCP">
<PatternLayout pattern="<128>%d{MMM dd HH:mm:ss} ${hostName} ${APP_NAME}[${sys:PID}]: %-5level %c %M - %msg%xEx%n" />
</Socket>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="REMOTE"/>
</Root>
</Loggers>
</Configuration>
<128>
means facility LOCAL0
. See: rsyslog.h. In the wire, (and when using traditional forwarding format) every syslog message starts with <FACILITY_NUMBER>
.
In server side, rsyslog should be configured like this:
# provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
# Dynamic file
template (name="AppFile" type="string" string="/var/log/remote/%programname%.log")
if ( $fromhost-ip == 'the.remote.ip.address' ) then {
if ( $programname == 'rest-server' and $syslogfacility-text == 'local0' ) then {
action(type="omfile" Sync="on" FileCreateMode="0640" DynaFile="AppFile")
& stop
}
}
Upvotes: 1