Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect remote server for logging using Log4J

Tags:

java

log4j

I want To Connect to the remote server to write my log files using Log4J Socket Appender My log.properties file is as below

log4j.rootLogger=DEBUG, BLAH

# to connect to the remote server
log4j.appender.BLAH=org.apache.log4j.net.SocketAppender

# set set that layout to be SimpleLayout
log4j.appender.BLAH.layout=org.apache.log4j.SimpleLayout

#log4j.appender.BLAH.File= admin.log

log4j.appender.BLAH.Port= xx
log4j.appender.BLAH.RemoteHost= <remoteIp>

I tried connecting localhost too with port number 22 and 8080

I am making a mistake some where in the connection . I get the following error

log4j:ERROR Could not connect to remote log4j server at [localhost]. We will try again later.

or Give me any suggestions to write the log files in a remote server machine.

like image 471
Meenakshi Avatar asked Feb 20 '23 12:02

Meenakshi


1 Answers

You should have a server running which listens to a given port. The log4j should connect to this server for logging.

Type the following in command prompt to start the listener

Java org.apache.log4j.net.SimpleSocketServer 4712 PATH_TO_THE_FILE\log4jServer.properties

Eg

java org.apache.log4j.net.SimpleSocketServer 4712 C:\log4j-server.properties

log4j-server.properties may contain something like this.

> log4j-server.properties will contain normal configuration of log4j.
> log4j.rootLogger=debug, stdout
> log4j.appender.stdout=org.apache.log4j.ConsoleAppender
> log4j.appender.stdout.Target=System.out
> log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
> log4j.appender.stdout.layout.ConversionPattern=%t %-5p %c{2} - %m%n

In the client side, your log4j config will look like this

log4j.rootLogger=DEBUG, BLAH  
# to connect to the remote server 
log4j.appender.BLAH=org.apache.log4j.net.SocketAppender  
# set set that layout to be SimpleLayout 
log4j.appender.BLAH.layout=org.apache.log4j.SimpleLayout  
#log4j.appender.BLAH.File= admin.log  
log4j.appender.BLAH.Port= 4712 
log4j.appender.BLAH.RemoteHost=10.225.226.58

Replace the IP and Port(without conflicting the standard ports) as per your configuration. Hope this would help.

like image 82
Abhilash Avatar answered Feb 23 '23 02:02

Abhilash