A Log4J example class
package com.techpage.log4jdemo;
import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
/**
* A simple Java Log4j example class.
*/
public class Log4JExample
{
// our log4j category reference
static final Category log = Category.getInstance(Log4JDemo.class);
static final String LOG_PROPERTIES_FILE = "lib/Log4J.properties";
public static void main(String[] args)
{
// call our constructor
new Log4JExample();
// Log4J is now loaded; try it
log.info("leaving the main method of Log4JDemo");
}
public Log4JExample()
{
initializeLogger();
log.info( "Log4JExample - leaving the constructor ..." );
}
private void initializeLogger()
{
Properties logProperties = new Properties();
try
{
// load our log4j properties / configuration file
logProperties.load(new FileInputStream(LOG_PROPERTIES_FILE));
PropertyConfigurator.configure(logProperties);
log.info("Logging initialized.");
}
catch(IOException e)
{
throw new RuntimeException("Unable to load logging property " + LOG_PROPERTIES_FILE);
}
}
}
main
method, which first calls the class constructor. The constructor then calls theinitializeLogger
method. This method actually does the work of loading the Log4J properties (configuration) file. It then calls the configure
method of the PropertyConfigurator
class.info
method of the Log4j log
object several times. Notice that I could have also called other methods like logger.warn()
, log.debug()
, log.error()
, orlog.fatal()
, but to keep it simple I'm just showinglog.info()
.The Log4J properties file
Log4J.properties
, and for the purpose of this demonstration I'm keeping it in a sub-directory of my project named lib
. Here are the contents:
#
# our log4j properties / configuration file
#
# STDOUT appender
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d %p [%t] %C{1} - %m\n
# use the STDOUT appender. set the level to INFO.
log4j.category.com.techpage.log4jdemo.Log4JDemo=INFO, STDOUT
Ajax to JSR 168 Portlets
-
Good article on *Best Practices for Applying AJAX to JSR 168 Portlets
http://developers.sun.com/portalserver/reference/techart/ajax-portlets.html*
0 Comments:
Post a Comment