In software development, logging is an indispensible part of the job. When developing java programs, Log4j (Apache framework) is probably the most commonly used framework.
But when we are writing Java programs, using the Oracle ADF framework, there is another option : ADF Logger. This logger is integrated in the Weblogic enterprise manager, and gives you the flexibility to adjust your log-levels at runtime. This blog post briefly demonstrates how the ADF logging works, using a servlet that logs at all levels.
The first example is run on a remote Weblogic server. At the end of the post the same example is run within the integrated Weblogic server of JDeveloper.
First we start with viewing the loggers defined in Oracle Enterprise Manager (OEM) before our servlet is deployed :
When we select ‘Log Configuration’ we get the Log Configuration screen, where some loggers are already defined and their loglevels can be configured :
We’ll deploy a servlet that uses the ADF Logger, change the loglevels at runtime, and check the logging to see what happens.
So fire up your JDeveloper and select a new Fusion Web Application (ADF) or download the JDeveloper project from https://www.dropbox.com/s/mwkjdw8k265iadx/LoggerApp.zip
Then create a servlet like the one below, which is a simple servlet called ‘ExecuteLogger’ that logs a message on all loglevels :
package be.iadvise.loggerapp;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import javax.servlet.*;
import javax.servlet.http.*;
import oracle.adf.share.logging.ADFLogger;
public class ExecuteLogger extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
private static ADFLogger _log = ADFLogger.createADFLogger(ExecuteLogger.class);
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Executing logging with ADFLogging and System.out.println</title></head>");
Calendar cal = Calendar.getInstance();
String loggingId = Long.toString(cal.getTimeInMillis());
out.println("<h1>Executing logging with ADFLogging and System.out.println</h1>");
out.println("<h2>Used Logging id = "+loggingId+"</h2>");
out.println("<p>The logging id is different for each request.</br>Use the logging id to search the log messages in the logfiles or EM for this request.");
out.println("<p>Logger Name in EM = "+_log.getName());
out.println("<p>****************** START LOGGING ******************");
out.println("<table >");
// FINEST
_log.finest("Loglevel finest : This is a logmessage with the ADFLogger. Logging id = "+loggingId);
System.out.println("Loglevel finest : This is a logmessage with the System.out.println. Logging id = "+loggingId);
out.println("<tr><td>FINEST</td><td>Loglevel finest : logMessage written to ADFLogger and System.out.println. Logging id = "+loggingId+"</td></tr>");
// FINER
_log.finer("Loglevel finer : This is a logmessage with the ADFLogger. Logging id = "+loggingId);
System.out.println("Loglevel finer : This is a logmessage with the System.out.println. Logging id = "+loggingId);
out.println("<tr><td>FINER</td><td>Loglevel finer : logMessage written to ADFLogger and System.out.println. Logging id = "+loggingId+"</td></tr>");
// FINE
_log.fine("Loglevel fine : This is a logmessage with the ADFLogger. Logging id = "+loggingId);
System.out.println("Loglevel fine : This is a logmessage with the System.out.println. Logging id = "+loggingId);
out.println("<tr><td>FINE</td><td>Loglevel fine : logMessage written to ADFLogger and System.out.println. Logging id = "+loggingId+"</td></tr>");
// CONFIG
_log.config("Loglevel config : This is a logmessage with the ADFLogger. Logging id = "+loggingId);
System.out.println("Loglevel config : This is a logmessage with the System.out.println. Logging id = "+loggingId);
out.println("<tr><td>CONFIG</td><td>Loglevel config : logMessage written to ADFLogger and System.out.println. Logging id = "+loggingId+"</td></tr>");
// INFO
_log.info("Loglevel info : This is a logmessage with the ADFLogger. Logging id = "+loggingId);
System.out.println("Loglevel info : This is a logmessage with the System.out.println. Logging id = "+loggingId);
out.println("<tr><td>INFO</td><td>Loglevel info : logMessage written to ADFLogger and System.out.println. Logging id = "+loggingId+"</td></tr>");
// WARNING
_log.warning("Loglevel warning : This is a logmessage with the ADFLogger. Logging id = "+loggingId);
System.out.println("Loglevel warning : This is a logmessage with the System.out.println. Logging id = "+loggingId);
out.println("<tr><td>WARNING</td><td>Loglevel warning : logMessage written to ADFLogger and System.out.println. Logging id = "+loggingId+"</td></tr>");
// SEVERE
_log.severe("Loglevel severe : This is a logmessage with the ADFLogger. Logging id = "+loggingId);
System.out.println("Loglevel severe : This is a logmessage with the System.out.println. Logging id = "+loggingId);
out.println("<tr><td>SEVERE</td><td>Loglevel severe : logMessage written to ADFLogger and System.out.println. Logging id = "+loggingId+"</td></tr>");
out.println("</table>");
out.println("<p>****************** END LOGGING ******************");
out.println("<body>");
out.println("</body></html>");
out.close();
}
}
Then, map this servlet in your web.xml as follows :
<servlet> <servlet-name>ExecuteLogging</servlet-name> <servlet-class>be.iadvise.loggerapp.ExecuteLogger</servlet-class> </servlet> <servlet-mapping> <servlet-name>ExecuteLogging</servlet-name> <url-pattern>/executeLogging</url-pattern> </servlet-mapping>
The line
private static ADFLogger _log = ADFLogger.createADFLogger(ExecuteLogger.class);
will create an entry in the ADF logging panel of OEM for the class ‘be.iadvise.loggerapp.ExecuteLogger’ during the first execution of our servlet.
So generate the ear file (in JDeveloper : Application -> Deploy -> your application) , and deploy to an ear , and deploy the ear to your remote Weblogic.
Then, execute the servlet by entering following url in a browser http://server:port/appname/executeLogger
We will receive the following output where the logging id is a unique id for every request (this will help us find the log info logged for every run) :
and in our log configuration screen, the logger for our servlet is added automatically with level WARNING :
When we look to the logging itself, by selecting the following :
We see that our servlet has logged 2 lines : level WARNING and ERROR, as the logger was default created with level “warning”.
(We look in the log files for the id generated by the servlet to get our 2 lines)
Now let’s update our log level to TRACE (finest), press apply, confirm the update, execute the servlet again , and see what happens to our logging :
confirm the update :
execute the servlet again (it generates a new logging id)
look for the generated logging :
As we have set the loglevel to trace (finest), which is the lowest level, we see all the generated loglines.
Remark : When we undeploy the application, the logger will remain visible in the log configuration screen until the managed server is restarted.
Using the integrated Weblogic server in JDeveloper
This screen shot shows you how get to the log configuration and logging screen in JDeveloper :
After executing the servlet locally, the Oracle Diagnostic Logging will show you the following :
and the analyze log :
then we change the log level :
and execute the servlet again and watch the output :
That’s it !!!
The big advantage of ADFLogging is the update of the loglevel at runtime, so whenever something seems to be going wrong, just set the loglevel to e.g. TRACE and check the logfiles. When the problem is solved reset it to WARNING or ERROR. And this without having to restart your application.
Also, being able to view and search the logfiles using the EM increases the userfriendlyness of this system. You don’t have to access the files directly on the system anymore.















Don’t forget about the log analyzer. It’s Ik the pull down menu on the log window of JDeveloper an cam give a meat overview of what happened during a single request.
Use ADFLogger.begin and .end in your code to time each time consuming step in your code and the analyzer will show where time is spent during a request
Pingback: Using ADF Logging in a non-ADF project | iAdvise blog