How can I use log4j to log why RMI is failing - java

I am currently using log4j to log on both server and client side processes.
But I am having an rmi failure.
I would like to know if the problem is on the client side or the server side.
How can I use log4j to show me the rmi activity on both the client and server?
Note that I don't see any rmi logging.
My log4j file contains this:
log4j.rootLogger=DEBUG, file, Socket_Logger, CONSOLE
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=C:/log/RDS.log
log4j.appender.file.Append=true
log4j.appender.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %-5p (%t)[%c] %m%n
log4j.appender.file.Threshold=DEBUG
The server log4j log shows things like:
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
But the Client side log doesn't show anything

Log4J has nothing to do with it. RMI uses Java logging. What you need to do is enable the client-side logging you need, via the RMI system logLevel properties.

Related

Log4j2 Syslog appender is not writing the 1st message to syslog after the syslog service is restarted

We have configured our application to write some specific log messages to System's Syslog file using the Syslog appender of Log4j2. No issue in writing the Syslog to the file. But when the syslog service is restarted, the first log message is not written to the syslog. The subsequent messages are written.
Enabled debug logs of Log4j, no exception is seen while writing 1st message to syslog after the restart. But for the subsequent request, the following messages were captured in the Log4j2 log.
2022-01-27 18:07:40,120 ajp-nio-0.0.0.0-8009-exec-3 DEBUG Reconnecting localhost/127.0.0.1:514
2022-01-27 18:07:40,121 ajp-nio-0.0.0.0-8009-exec-3 DEBUG Creating socket localhost/127.0.0.1:514
2022-01-27 18:07:40,122 ajp-nio-0.0.0.0-8009-exec-3 DEBUG Closing SocketOutputStream java.net.SocketOutputStream#1a769d7
2022-01-27 18:07:40,122 ajp-nio-0.0.0.0-8009-exec-3 DEBUG Connection to localhost:514 reestablished: Socket[addr=localhost/127.0.0.1,port=514,localport=57852]
I took threaddump and checked whether the Reconnector thread is running but no such exists in the threaddump. I am clueless here, any help on finding the reason for missing the message would be helpful.
Environment details:
CentOS 7.9 + RSyslog Service,
Application deployed in Tomcat and running on Java 11,
Log4j2 version is 2.17.1
This is due to the way plain text TCP syslog works. Check out this post for further information.
This "bug" exists, since version 8.1901 and newer.
The only way you can fix this - as far as i know - is to send the messages over the RELP protocol. See omrelp module.

Configure log4j to send email

In log4j.properties I have
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-5p [correlation-ids:%X{X-CAAP-Correlation-ID},user:%X{X-CAAP-User}] [%t] (%C:%L) %X{camel.routeId}- %m%n
log4j.logger.com.lacapitale=INFO
log4j.logger.org.apache.cxf=INFO
log4j.logger.org.apache.camel=DEBUG
log4j.logger.org.springframework=INFO
log4j.appender.SMTP=org.apache.log4j.net.SMTPAppender
log4j.appender.SMTP.BufferSize=1
log4j.appender.SMTP.From=noreply#mycompany.com
log4j.appender.SMTP.SMTPHost=localhost
log4j.appender.SMTP.Subject=[{{appid}}] - Erreur technique
log4j.appender.SMTP.Threshold=INFO
log4j.appender.SMTP.To=me#mycompany.com
log4j.appender.SMTP.layout=org.apache.log4j.PatternLayout
log4j.appender.SMTP.layout.conversionPattern=%d{ISO8601} %-5p %m%n
log4j.rootLogger=INFO, CONSOLE
log4j.logger.email=INFO, SMTP
But, I never get any email when I log an info.
Is there anything wrong in my config?
Is it because I deployed the app in Eclipse embedded Tomcat? Does Tomcat always include a SMTP server?
Tomcat does not include an SMTP server. You will need to make sure the SMTPHost you have defined has an MTA installed, running, and accepting connections.

Change Log4J's Threshold dynamically through user input

I'm writing a simple console application which sends and receives data to/from a socket via user commands, such as "send hello" -> receives "hello" from socket. One of the available commands should be logLevel (ALL|WARN|DEBUG|etc.), which enables the user to set the log level of the Log4J logger which I'M using throughout the application.
Right now, I specified two Appenders in my properties file:
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/client.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# info and above messages are printed to stdout
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
My goal here is to send all log entries from any level to the logfile, but only the ones that match the specified log level currently set by the user to stdout. A sample workflow could look like this:
> INFO: current log level is INFO and above.
> send hello
> INFO: sending hello to server
> INFO: receiving "hello" from server
> logLevel WARN
> INFO: log level set to warn
> send hello
> # no info here, but its still sent to the logfile
My goal here is to send all log entries from any level to the logfile, but only the ones that match the specified log level currently set by the user to stdout.
So essentially you want to change the threshold on the stdout appender programmatically. This is easy enough to do, the wrinkle is that the threshold property is part of the AppenderSkeleton abstract class (which ConsoleAppender extends) rather than the Appender interface itself.
((AppenderSkeleton)Logger.getRootLogger().getAppender("stdout"))
.setThreshold(Level.toLevel(levelNameProvidedByUser));
If you really do want all log messages to go to the file then you should probably configure the root logger level to ALL rather than INFO in your log4j.properties, and rely on the threshold to filter stdout.
My requirement is to add specific logs to one of my log file. I have two log files. One for normal case and other I have to add only specific logs. Means I have to make threshold off by default. Once requires I have to make threshold INFO and add then logs to my log file and then again make threshold OFF.
This is my code of java
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getRootLogger();
Appender appender = logger.getAppender("logFileName");
((AppenderSkeleton)appender).setThreshold(Level.INFO);
logger.info(myString.toString());
((AppenderSkeleton)appender).setThreshold(Level.OFF);
So only myString is log to my logFileName file.

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.
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.
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.

email log file, generated by Java WebLogic Application Server

does this requirement conform to the J2EE Standards?
is there a easy way to implement this, log file gets generated by Log4J and in the end I will access the file system and email the whole file(s). can I access the file system?
Log4j has an email appender...
See: http://www.onjava.com/pub/a/onjava/2004/09/29/smtp-logging.html?page=2
(Also look on page 1)
with log4j, you can add an email appender to your configuration. You can declare the appender in your log4j.proeprties so:
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.To= #recepient's email address
log4j.appender.email.From= #the sender's email address
log4j.appender.email.SMTPHost= #location of your smtp server
log4j.appender.email.Threshold=FATAL #the lowest log level on which the email is generated
log4j.appender.email.BufferSize=512
log4j.appender.email.Subject= #subject line of the email sent out
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=-[%d] %-4L %-5p %c %x - %m%n #message format
Something to remember: this appender will send an email on every log message that meets the threshold requirement so having a high threshold is recommended so your inbox doesn't get flooded with messages that are non critical.

Categories

Resources