This will create a centralized logging
mechanism by using log4j
This will create daily one log-file
Step 1: -
Create a file called log4j-server.properties and add the below content
Step 2:-
Copy log4j-1.2.7.jar into your folder or where log4j-server.properties
Step 3:-
Go to cmd prompt and run with the following command.
Ex:-
D:\Log4j-server\server> java -classpath log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer 4712 log4j-Server.properties
or
D:\Log4j-server> java -classpath .\Server\log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer 4712 .\Server\log4j-Server.properties
Step 4:-
Create a client log4j.properties and add the below content
Step 5:-
Your Java program.
This will create daily one log-file
Step 1: -
Create a file called log4j-server.properties and add the below content
log4j.rootLogger=INFO, R
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %-5p - %m%n
log4j.appender.R.File=C:/logs/examples
log4j.appender.R.DatePattern='-'yyyyMMdd'.log'
Step 2:-
Copy log4j-1.2.7.jar into your folder or where log4j-server.properties
Step 3:-
Go to cmd prompt and run with the following command.
Ex:-
D:\Log4j-server\server> java -classpath log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer 4712 log4j-Server.properties
or
D:\Log4j-server> java -classpath .\Server\log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer 4712 .\Server\log4j-Server.properties
Step 4:-
Create a client log4j.properties and add the below content
log4j.rootLogger=INFO, R
log4j.appender.R=org.apache.log4j.net.SocketAppender
log4j.appender.R.Port=4712
log4j.appender.R.RemoteHost=localhost
log4j.appender.R.ReconnectionDelay=10000
Step 5:-
Your Java program.
public class LogTest
{
private static org.apache.log4j.Logger log = Logger.getLogger ( LogTest.class );
public static void main ( String [] args ) throws Exception
{
log.info ( "First Info Message!" );
log.trace ( "First Trace Message!" );
log.debug ( "First Debug Message!" );
log.warn ( "First Warn Message!" );
log.error ( "First Error Message!" );
log.fatal ( "First Fatal Message!" );
}
}
OUTPUT :-
2013-09-12 00:40:04,395 [main] INFO - Listening on port 4712
2013-09-12 00:40:04,400 [main] INFO - Waiting to accept a new client.
2013-09-12 00:40:04,751 [main] INFO - Connected to client at /192.168.7.9
2013-09-12 00:40:04,751 [main] INFO - Starting new socket node.
2013-09-12 00:40:04,760 [main] INFO - Waiting to accept a new client.
2013-09-12 00:40:07,736 [main] INFO - Second Info Message!
2013-09-12 00:40:07,737 [main] WARN - Second Warn Message!
2013-09-12 00:40:07,738 [main] ERROR - Second Error Message!
2013-09-12 00:40:07,740 [main] FATAL - Second Fatal Message!
2013-09-12 00:40:12,754 [main] INFO - Second Info Message!
2013-09-12 00:40:12,755 [main] WARN - Second Warn Message!
2013-09-12 00:40:12,757 [main] ERROR - Second Error Message!
2013-09-12 00:40:12,758 [main] FATAL - Second Fatal Message!
2013-09-12 00:40:13,402 [main] INFO - Connected to client at /192.168.7.9
2013-09-12 00:40:13,402 [main] INFO - Starting new socket node.
2013-09-12 00:40:13,403 [main] INFO - Waiting to accept a new client.
2013-09-12 00:40:13,405 [main] INFO - First Info Message!
2013-09-12 00:40:13,414 [main] WARN - First Warn Message!
No comments:
Post a Comment