Skip to content

Instantly share code, notes, and snippets.

@jerieljan
Created May 15, 2012 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerieljan/2698282 to your computer and use it in GitHub Desktop.
Save jerieljan/2698282 to your computer and use it in GitHub Desktop.
Log4J on Java EE
//include:
//WebContent/WEB-INF/lib/log4j-1.2.16.jar
//src/Log4JServlet.java
import java.io.File;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;
public class Log4JServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* This initializes Log4J by retrieving the .properties file in WEB-INF
*/
public void init(ServletConfig config) throws ServletException {
String log4jLocation = config.getInitParameter("log4jprop-location");
ServletContext sc = config.getServletContext();
String webAppPath = sc.getRealPath("/");
String log4jPath = webAppPath + log4jLocation;
File log4jFile = new File(log4jPath);
System.out.println("Initializing Log4J: " + log4jPath);
if (log4jFile.exists()) {
PropertyConfigurator.configure(log4jPath);
} else {
BasicConfigurator.configure();
}
super.init(config);
}
}
//WebContent/WEB-INF/log4j.properties
# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, myConsoleAppender
# settings for the console appender
log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
# If %M slows down code (as it is listed as extremely slow), omit it.
log4j.appender.myConsoleAppender.layout.ConversionPattern=[%-5p] %c.%M %x >> %m%n
//web.xml ("Deployment Descriptor")
// EDIT DISPLAY NAME!
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>ShoppingAppProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Log4JServlet</servlet-name>
<servlet-class>servlets.Log4JServlet</servlet-class>
<init-param>
<param-name>log4jprop-location</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
//USAGE:
//private final static Logger LOG = Logger.getClass(<currentclassname>.class);
// then use this in your code:
// log.info(<message>);
// log.error(<message>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment