Using Log4j in Oracle ADF application

As i talked about more of ADF logger in my previous Adf logger post.Now what if you want log4j in ADF application.

1) Add the log4j JAR file to the project library JAR file .

2) Then you need to create log4j.properties file in your application src directory.

Content of properties file-

#####################################################

# Set root logger level to INFO and its only appender to ConsoleOut.
log4j.rootLogger=INFO,ConsoleOut,F

# ConsoleOut is set to be a ConsoleAppender.
log4j.appender.ConsoleOut=org.apache.log4j.ConsoleAppender

# ConsoleOut uses PatternLayout.
log4j.appender.ConsoleOut.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleOut.layout.ConversionPattern=%-5p: [%d] %c{1} – %m%n

#####################################################

now you can use log4j as below –

package com.techartifact.log4jSample;

import org.apache.log4j.Logger;
import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.*;
import javax.servlet.http.*;



public class Log4JSample
extends HttpServlet
{
Logger log = Logger.getLogger(this.getClass().getSimpleName());
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

public void init(ServletConfig config)
 throws ServletException
{
 super.init(config);
 log.info("inside init() method of Log4JSample");
}

public void doGet(HttpServletRequest request,
                 HttpServletResponse response)
 throws ServletException, IOException
{
 log.info("inside doGet method of Log4JSample");
}
}

Happing logging in Techartifact with Vinay Kumar. 🙂

Insights of ADF logging in ADF Framework

Logging is an important in debugging and tracing the issue.In Adf framework we have ADF logger.
How to use it ADF logger in ADF application.Hopefully , after reading you will have a good idea on it.

Q) How to Turn On Diagnostic Logging

To turn on diagnostic logging, set the Java system property named
jbo.debugoutput to the value console. Additionally, the value ADFLogger lets
you route diagnostics through the standard Logger implementation, which can be
controlled in a standard way through the logging.xml file
Inside JDeveloper is to edit your project properties and in the Run/Debug page, select a run
configuration and click Edit. Then add the string -Djbo.debugoutput=console to
the Java Options field.

Q)How to locate the logging.xml

There are following two ways to get to the logging configuration screen.
1. Open the Application Server Navigator (Ctrl + Shift + G), right mouse click on the IntegratedWeblogicServer and select Configure Oracle Diagnostic Logging for “IntegratedWeblogicServer”.
2. From the Log window (usually docked underneath your editor) pull down the Actions menu and select Configure Oracle diagnostic Logging.

How to configure log level
we need to set the log Level to at least FINE. Let’s set it to the most detailed level FINEST – TRACE:32 or anything you want to set ,if using ODL Log Levels – for the oracle.jbo logger.

If you are using Integrated WebLogic Server in JDeveloper on the Windows platform,
you can find the logging.xml file in a location similar to:
C:\Documents and Settings\username\ApplicationData\JDeveloper\latest_system_folder\DefaultDomain\config\fmwconfig\servers\DefaultServer The log files for Integrated WebLogic Server are in a location similar to:
C:\Documents and Settings\username\ApplicationData\JDeveloper\latest_system_folder\DefaultDomain\servers\DefaultServer\logs

The log files for a standalone WebLogic Server instance are in a location similar to:
$domain_home/servers/your_servername/logs

In the Project Properties dialog, click the Run/Debug/Profile node and create a
new run configuration.. In the Run Configurations list, double-click the new run configuration to edit its
properties.4. In the Edit Run Configuration dialog, for Launch Settings, enter the following
Java options for the default virtual machine:
-Djbo.debugoutput=adflogger -Djbo.adflogger.level=FINE
Set the level=FINE for detailed diagnostic messages
With this the setup is completed. Now open the file (AM, bean or any java class) which belongs to above package and add log messages as shown in below sample file.

LogLevelMapping

You need to set logging in your java classes as below this.

private static final ADFLogger logger= ADFLogger.createADFLogger("Implementation class vinay.class");

createADFLogger() function will create the logger instance .
After initializing the logger, you can use logging within the method as

private static ADFLogger log= ADFLogger.createADFLogger(GammaAMImpl.class);  
For debug messages in try block/ normal debug messages , write  
if(log.isFine()){ log.fine(String classname,String methodname,String message); }  


One important thing ,you need to include the package directory of the implementation project in Logging.xml for adfLogger.

I have used a lot log4j in my jee application.Then why I should use ADF logger compare to log4j.I found view of some adf expert on internet .So sharing with you all. Not sure about all the points:-

Pros to use ADFLogger:

– You can control trace level in Oracle Fusion Middleware Console (Enterprise Manager) with a visual interface.
– You have support by Oracle if you are having problems with logging solution.
– You have an ADFLogger for JavaScript too.
– Have more trace level’s than log4j.

Cons to use ADFLogger:

– You need to associate ADF libraries to your PDK’s.

Pros to use log4j:

– Best known for most developers.
– Easy to set different log strategies and file sizes.
– Goog performance.
– Easy to extend and use Wrappers with it.

Cons to use log4j:

– You can’t control trace level with visual interface provided by Oracle. You need to change manually log4j.properties
– You haven’t Oracle Support for log4j.

It’s only my own opinion. To me is recommended to use ADFLogger if you are in ADF Application. If you are in a JSR-168 or another application use log4j instead of ADFLogger.

Implementing Log4J

User log4j and commons-logging.jar in the class path. These jar files contain the information for logging set up.Declare the following as a class

attribute in the class where you want to implement the logging, say SomeClass.java

private static Logger logger = Logger.getLogger

(com.mattiz.SomeClass.class);

You would have a sample log4j properties configuration file which you should put in your classpath.
Suppose you wanted the line “Test Debug” to go to your log

file use logger.error or logger.debug or logger.warn.
Viz. logger.error(“Test Debug”);

For warning/ debugging messages you could use

logger.warn(“This is a warning”);

or

logger.debug(“Variable value is”+var);

In the log4j configuration file you can set the level of logging.In the sample configuration file you will find words like ERROR,WARN or DEBUG.If you
set the level to WARN, then only warnings and errors will print, while debug won’t print. If you set the level to DEBUG level then debug, warn and error
will print.These may go to the console or to a separate log output file based on the settings in the log4j configuration file. The log4j.properties contains
settings to “rollover” files. If file becomes more than 1 MB then copying to another file and creating a new file is done automatically by log4j. With the
settings in the log4j.properties file you can control

what is being logged. By setting it to ERROR level, debug statements won’t be printed in the production system.For development use DEBUG level because you
want to see debug messages.

The hierarchy is

DEBUG < INFO < WARN < ERROR < FATAL

If set to WARN level, all warn, error and fatal messages will be printed but no info and debug messages will be printed.You can set logging levels for each
package. So if you are working on a certain package you can set the package’s logging level to DEBUG, and another package’s logging level to WARN in the
log4j properties file.

For example

log4j.category.com.mattiz.security = WARN

You need not use all the debugging levels; mostly people use

DEBUG and ERROR. You can put error level messages inside the catch block.Error logs are like this logger.error(“Exception critical”+ex.toString());You can do this for production systems.

Contents of a simple log4j.properties configuration file

# Print FATAL, ERROR and WARN messages – do not print

DEBUG and INFO messages

# the sequence is FATAL > ERROR > WARN > DEBUG > INFO

# since the level is set to WARN – message levels above it will

be printed

# while levels below it will not be printed

log4j.rootCategory=WARN, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# the conversion pattern will be used to format the timestamp

see below example

# 2004-05-13 17:15:13,318 [Servlet.Engine.Transports : 1]

DEBUG

# this will pre-pend all logging messages

log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-

5p %c{1} - %m%n

Contents of a more elaborate log4j.properties file

# the general level is set to WARN

# WARN, ERROR, FATAL will be printed

# In addition to printing to System Out, also print

to "RollingFile"

log4j.rootCategory=WARN, stdout, RollingFile

# the level for the com.mattiz.web package (and subpackages)

is set to DEBUG

# DEBUG, WARN, ERROR, FATAL will be printed for the web package

log4j.category.com.mattiz.web=DEBUG

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Print the date and time for systemOut

log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c{1} - %m%n

# Save log to Rolling File Also

log4j.appender.RollingFile=org.apache.log4j.RollingFileAppender

# Location of rolling file

log4j.appender.RollingFile.File=d:/mattiz/mattiz.log

# if the file becomes greater than 500KB then create a new file

and backup the old file

log4j.appender.RollingFile.MaxFileSize=500KB

# Keep 5 back up files

log4j.appender.RollingFile.MaxBackupIndex=5

log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout

#Print the date and time for RollingFile

log4j.appender.RollingFile.layout.ConversionPattern=%d [%t]

%-5p %c{1} - %m%n