In this tutorial I will explain how can we write log from one system to other system,
For this purpose I will use SocketAppender of Log4j.For more info regard SocketAppender you can go through this link
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/net/SocketAppender.html
For Demonstration I will use two log4j configuration file and two java class
one for client machine(localHost) and other for server or remote machine
========= client log configuration File (client.xml)=========
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="socket" class="org.apache.log4j.net.SocketAppender">
<param name="Port" value="1342"/>
<param name="RemoteHost" value="192.000.000.00"/>
<param name="ReconnectionDelay" value="5000"/>
<param name="LocationInfo" value="true"/>
</appender>
<root>
<param name="level" value="INFO"/>
<appender-ref ref="socket" />
</root>
</log4j:configuration>
class = The SocketAppender class
RemoteHost = Ip of that machine where you want to write your log
Port = port number of Remote machine
========= Server log configuration File (server.xml)=========
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="rolling" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="C:/Test/LogFile.log" />
<param name="datePattern" value="'.'yyyy-MM-dd-HH " />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n"/>
</layout>
</appender>
<root>
<param name="level" value="INFO"/>
<appender-ref ref="rolling" />
</root>
</log4j:configuration>
Class = you can use any log4j class as per your requirement I am using DailyRollingFileAppender.
========= Client Java class =========
package com.amit.blog.clientlogging;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.xml.DOMConfigurator;
/**
* This class use to logging on remote server on xml defined port.
*/
public class Client
{
private static Log logger = LogFactory.getLog(Client.class);
public static void main(String[] a)
{
try
{
DOMConfigurator.configure("client.xml");
for(int i=0;i<10;i++)
{
logger.info("writing on remote server !");
}
}
catch (Exception e)
{
logger.info("Failed to write on remote server");
}
}
}
========= Server Java class =========
package com.amit.blog.serverlogging;
import org.apache.log4j.net.SimpleSocketServer;
/**
* This class use to receive the log entries from client on port open by method.
*/
public class Server
{
public static void main(String[] args)
{
try
{
String defaultPort="1342"; //Need to mention one port to open for writing
/** This class is provided by log4j
* It takes two parameters, namely the port to start the
* server on and the server side log4j configuration file.
*/
String[] arguments = {defaultPort, "server.xml"};
SimpleSocketServer.main(arguments);
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
I think It will help you to manage your log.
For this purpose I will use SocketAppender of Log4j.For more info regard SocketAppender you can go through this link
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/net/SocketAppender.html
For Demonstration I will use two log4j configuration file and two java class
one for client machine(localHost) and other for server or remote machine
========= client log configuration File (client.xml)=========
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="socket" class="org.apache.log4j.net.SocketAppender">
<param name="Port" value="1342"/>
<param name="RemoteHost" value="192.000.000.00"/>
<param name="ReconnectionDelay" value="5000"/>
<param name="LocationInfo" value="true"/>
</appender>
<root>
<param name="level" value="INFO"/>
<appender-ref ref="socket" />
</root>
</log4j:configuration>
class = The SocketAppender class
RemoteHost = Ip of that machine where you want to write your log
Port = port number of Remote machine
========= Server log configuration File (server.xml)=========
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="rolling" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="C:/Test/LogFile.log" />
<param name="datePattern" value="'.'yyyy-MM-dd-HH " />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n"/>
</layout>
</appender>
<root>
<param name="level" value="INFO"/>
<appender-ref ref="rolling" />
</root>
</log4j:configuration>
Class = you can use any log4j class as per your requirement I am using DailyRollingFileAppender.
========= Client Java class =========
package com.amit.blog.clientlogging;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.xml.DOMConfigurator;
/**
* This class use to logging on remote server on xml defined port.
*/
public class Client
{
private static Log logger = LogFactory.getLog(Client.class);
public static void main(String[] a)
{
try
{
DOMConfigurator.configure("client.xml");
for(int i=0;i<10;i++)
{
logger.info("writing on remote server !");
}
}
catch (Exception e)
{
logger.info("Failed to write on remote server");
}
}
}
========= Server Java class =========
package com.amit.blog.serverlogging;
import org.apache.log4j.net.SimpleSocketServer;
/**
* This class use to receive the log entries from client on port open by method.
*/
public class Server
{
public static void main(String[] args)
{
try
{
String defaultPort="1342"; //Need to mention one port to open for writing
/** This class is provided by log4j
* It takes two parameters, namely the port to start the
* server on and the server side log4j configuration file.
*/
String[] arguments = {defaultPort, "server.xml"};
SimpleSocketServer.main(arguments);
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
I think It will help you to manage your log.
No comments:
Post a Comment