Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Direct Logs to Remote System With Log4j

DZone's Guide to

Direct Logs to Remote System With Log4j

If an application is running on a remote system, you need the logs on your local machine. Learn how to do direct logs to whichever machine you desire.

· Performance Zone
Free Resource

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

Logging is one of the most important aspects of developing programs. Logs provide us with information about the behavior of programs, whether the behavior is expected or not.

Log4j is a very popular package for logging purpose written in Java. (Note: This article assumes that you are familiar with Log4j and already using Log4j.)

Sometimes, we need the logs on different machines. Let's consider that the application is running on a remote system and we need the logs on our local machine.

Hence, the following question arises.

How Can I Direct Logs to a Desirable Machine?

There are some simple steps to do this:

Change the log4j.properties files on the sender side. Use SocketAppender to direct logs to another machine. See the following log4j.properties files:

log4j.rootLogger=DEBUG, server
log4j.appender.server=org.apache.log4j.net.SocketAppender
log4j.appender.server.Port=4712
log4j.appender.server.RemoteHost=loghost
log4j.appender.server.ReconnectionDelay=10000

Modify the log4j-reciever.properties files on the reciever side. See the following file:

log4j.rootLogger=DEBUG, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logfile.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d] [%t] [%m]%n

Run the following command on the reciever side:

java -classpath log4j-path.jar org.apache.log4j.net.SimpleSocketServer 4712 log4j-reciever.properties

After this setup, run your program/application, and you will get logs at your desirable machine.

Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.

Topics:
log4j ,logging ,performance ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}