Log file not produced until WebLogic is restarted - java

I'm working on the development of an application that is deployed on WebLogic 10.3. It is packaged as an EAR and includes one module. The application works fine by itself but I am faced with issue related to logging.
I am using Log4j. The library is included in the EAR file and log4j.xml is placed under JAR module. So the config location is the following:
A.ear/B.jar/log4j.xml
Log4j config is following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CA" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MMM-yyyy-HH:mm:ss} %p %C{1} - %m%n" />
</layout>
</appender>
<appender name="DRFA" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file"
value="servers/AdminServer/logs/EJB.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'-'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MMM-yyyy-HH:mm:ss} %p %C{1} - %m%n" />
</layout>
</appender>
<logger name="com.companyname.ejb" additivity="false">
<level value="DEBUG" />
<appender-ref ref="DRFA" />
<appender-ref ref="CA" />
</logger>
<logger name="com.companyname.results" additivity="false">
<level value="DEBUG" />
<appender-ref ref="DRFA" />
<appender-ref ref="CA" />
</logger>
<logger name="com.companyname.marketdata" additivity="false">
<level value="DEBUG" />
<appender-ref ref="DRFA" />
<appender-ref ref="CA" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="CA" />
</root>
When I build and deploy EAR (using Maven and customized WebLogic plugin) and call application no log file appears. But if I restart WebLogic everything is fine.
WebLogic is running under Windows 7 in domain mode with single node.
I'd like to know if there is some way to make the log appear without weblogic restart (since it can cause issues on production environment)?
Update: Also I'd like to know what is the reason of such behavior (i.e. why the log file is not created right after application deployment)? Is this an issue with Weblogic, log4j or with their coupling? I've tried to find the answer in the Oracle documentation, but no luck for now.

Some notes:
In prod environment, you probably want to have your log configuration outside app packages, so you can change log levels without redeployment.
You should plan for production to be able to handle restarts. We usually have hot and cold servers, so load can be balanced and servers restarted when doing the deployment.
About the issue, if you want to, you could specify a servlet that is run on startup of app and configures your log4j. Something like:
web.xml
<servlet>
<servlet-name>SomeServlet</servlet-name>
<servlet-class>YourServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
Servlet
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.xml.DOMConfigurator;
public class YourServlet extends HttpServlet
{
#Override
public void init(final ServletConfig config)
throws ServletException
{
final java.net.URL url = Thread.currentThread().getContextClassLoader()
.getResource("Log4j.xml");
DOMConfigurator.configure(url);
}
}
There's also an example on the web about using a servlet context listener.
Edit. as to why this happens, weblogic logging mechanism is initiated by default on startup with these kind of settings:
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dlog4j.configuration=file:<path>/log4j.properties
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dweblogic.log.Log4jLoggingEnabled=true
so if you just redeploy your app without restarting the server, these settings will not be used -> logging will not get initiated.

I didn't found any quick configuration solution, so code change was required.
I've added Weblogic Application Lifecycle listener as suggested here. Its postStart() method initializes log4j explicitly through DOMConfigurator and now logs appear right after application deployment.
The are few alternatives how configuration can be initialized. One of them was mentioned in eis' post and another here. But I've choose listener in order to keep single module in EAR and in order to avoid issues with singleton EJB on cluster environment (i.e. I am not sure if Weblogic will create singleton on each node or just one instance per cluster).
Also in order to prevent environment changes for local and dev environment, I am using internal log4j.xml (i.e. placed within ear file) there.
For stage and prod - external config file is specified (in Maven profile).
External file is monitored by log4j so changes will be automatically applied without any redeployments or restarts.

Related

How to set size and amount of log files (stdout, stderr) in tomcat?

I recently deployed a java app on my production server. In which I have installed Tomcat version 8.5 and I have been having problems since the log files stderr and stdout grow in size exaggeratedly and therefore I lower the performance of the server or sometimes it is necessary to restart it since it does not respond. I would like to know how to configure these files so that they create only a certain amount with a specific size. I have tried configuring it through log4j it has given me error when integrating this same a tomcat. I have also configured certain parameters (for example: java.util.logging.FileHandler.limit = 1024) for logging in which the size and quantity are assigned but these do not apply to the configuration and therefore I continue to throw the server .
In a production environment, a good practice is to use a process that works with small log files, and that creates a new one if the critical size has gone.
With log4j you can use a RollingFileAppender. See the minimal configuration :
<appender name="rolling.file.appender" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="50MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="${catalina.base}/logs/myApplication.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value="INFO" />
<appender-ref ref="rolling.file.appender" />
</root>
Documentation: https://tomcat.apache.org/tomcat-8.0-doc/logging.html

Connection to database in a managed thread, Debug values doesn't appear in Tomcat console or logs

I am facing this trouble for a long time now without accessing the debug logs of the managed Threads in my Spring boot application when run on Tomcat. All the logs appear when run on the Eclipse/STS.
In Tomcat logs, I can only see the main Tread Logs.
I am connection to a database through JDBC and this is happening in a separate thread. I tried to follow the log configuration documentation but none of them helps to get the debug logs of these threads. So I do not actually see the exact problem of what is causing the connection to fail.
Here is what I tried so far:
I tried with the following logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p
%c{1}:%L - %m%n</pattern> </encoder> </appender> <logger name="com.biscoind"
additivity="false" level="TRACE"> <appender-ref ref="stdout" /> </logger>
<root level="debug"> <appender-ref ref="stdout" /> </root> -->
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework.web" level="DEBUG" />
</configuration>
When that did not resolve the issue I removed this file and see if by default, if it logs all the treads. But it did not.
So, I added the following configurations to the application.properties
logging.level.org.springframework.web:TRACE
logging.level.org.hibernate:ERROR
Then It seemed to me that this is only logging out the above namespaces, I again added
debug=true
logging.level.org.springframework.web:DEBUG
logging.level.org.hibernate:DEBUG
Tried and it did not work.
I added my namespaces also and tried as follwing,
debug=true
logging.level.com.mydomain:DEBUG
logging.level.org.springframework.web:DEBUG
logging.level.org.hibernate:DEBUG
That did not work also, I am now confused on the what should I do with the config relative to logging to make the logs to appear for the tread executions.
Irrespective of the treads, because of the property spring.jpa.show-sql=true it logs the queries that are made.
It was not a problem with the threads at all. The application was working correctly in the development environment. The problem was in the deployment environment.
It turned out to be a Java version miss-match with the jar files and the JVM version. The jars were build using Java 8 and it was running on Java 7 JVM.
When the JMV was changed to Java-8. It worked fine. So Next time I will be more careful with the version mismatch.

Log4j log append to file name dependent on day - config

I'm fairly new to log4j and I would like to set up automatic logging of anything output to the console, like errors, or info messages for a web app running on my local server.
How would I set up my xml and/or properties file to do this? What I really am looking for is that each day a new log file be created in a directory (Ex: /mylogs/app-log-01-08-2014.log)
I've begun like this:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- general application log -->
<appender name="BarLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="my-changing-file-name.log" /> ->>> how does this change
<param name="Threshold" value="INFO" /> ->>> should INFO be Console here?
</appender>
<logger name="what-goes-here?">
<appender-ref ref="something-here"/>
</logger>
<root>
<level value="INFO"/>
</root>
Also, where in a web project does the xml file go? WEB-INF?
Any help is appreciated.
You are looking for what is known as "daily rolling log files", configuration is shown here. As for the web project, see this answer.
On a side node, you might want to learn about Logback as alternative.

Accessing Detailed logs in Tomcat

while executing a small application in Eclipse, I get an Http Status 500 error from the Tomcat server that I am running through Eclipse itself...
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
org.apache.struts2.impl.StrutsActionProxy.getErrorMessage (StrutsActionProxy.java:69)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:500)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.8 logs.
This message from the server says that error was caused by a "NullPointerException". The last line says that "full stack trace is available in Apache Tomcat/7.0.8 logs.
I have checked the logs created in my workspace ( ${catalina.base} is set to a folder in my workspace) but the logs don't provide any details about the "NullPointerException". The only thing present in the logs is...
0:0:0:0:0:0:0:1 - - [03/May/2012:15:19:16 +0530] "GET /KurniawanChap02Struts/ HTTP/1.1" 500 1789
I have also tried increasing the "logging levels" but even that doesn't help.
What should I do to access the detailed logs of the server ?
I had some similar errors when I was using different versions of Apache Commons. For getting more details about these errors I changed the default loggind of Tomcat to log4j. You can see the detailed instructions in Apache Tomcat 7 (7.0.27) - Logging in Tomcat
This is xml example for log4j that I was using:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p (%c.java:%L).%M - %m%n" />
</layout>
</appender>
<!-- CATALINA -->
<logger name="org.apache.catalina.session">
<level value="INFO" />
</logger>
<!-- TOMCAT -->
</logger>
<logger name="org.apache.jasper.compiler">
<level value="INFO" />
</logger>
<!-- COMMONS -->
<logger name="org.apache.commons.digester">
<level value="INFO" />
</logger>
<root>
<priority value="TRACE" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
I hope that this can hep you.
I had the same problem. I removed the unused project that was referenced in properties->Java Build Path->Projects. It was showing that xyz(project Name) is missing.
I removed xyz.
Sometimes you can check in properties->Java Build Path->libraries whether any jar is missing. If so, you can add that jar from the classpath.

What is the best way to use log4j in the web application?

We are starting a Spring MVC based web application. We will be using tomcat as the web server.
I need to configure log4j in the application and log to the application specific file and not to the tomcat log files.
e.g. tomcat has its own log files like localhost.log etc. I want something like myAppName.log in the tomcats log folder.
The logging configuration will go in lo4j.xml or log4j.properties in the application war file.
Plus I dont want to hard code the output log file in the web application.
But I am not sure how to do this.
Please help me. As well correct me if I am wrong somewhere.
Do like this, initialize the logger with following code,
Logger log = Logger.getLogger(this.getClass());
log the information like follows,
log.debug("My message");
and place the log4j.xml in your class path. content like follows,
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="infoLogsFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="MyApplication.log"/>
<param name="Threshold" value="DEBUG"/>
<param name="MaxFileSize" value="100MB"/>
<param name="ImmediateFlush" value="TRUE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<root>
<priority value ="DEBUG" />
<appender-ref ref="infoLogsFile"/>
</root>
</log4j:configuration>
Do not forget to add the required jars like log4jXXX.jars and apache common logging jars. with this you will be able to see all log messages in MyApplication.log file creates in bin folder of your tomcat.
Try this:
Put the log4j jar as part of the web application
Do not put a configuration as part of your web application
Create your log4j.xml wherever you like
When you start tomcat provide this argument
-Dlog4j.configuration=file:///.../log4j.xml

Categories

Resources