Meenakshi
Meenakshi

Reputation: 468

Connect remote server for logging using 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.

Upvotes: 4

Views: 23551

Answers (2)

Abhilash
Abhilash

Reputation: 792

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.

Upvotes: 5

artbristol
artbristol

Reputation: 32407

Port 22 and 8080 are usually used by SSH and HTTP respectively. The SocketAppender is expecting to talk to a SocketNode using its own TCP-based protocol. So you need to start one up, on a different port of your choice.

Note that when you try to log to a remote server, you'll need to have that port open on the firewall.

Upvotes: 3

Related Questions