开发者

Using log4j on Google App Engine

I need to use log4j in my application, but I don't know how I can get properties loaded. Deafult properties files says that I should put log4j.properties to /WEB-INF/classes/ folder, but in eclipse I cannot see that folder and I cannot create it, because it already exists. And I cannot add any files to that folder either.

Here is error that I get:

log4j:WARN No appenders could be found for logger (DataNucleus.ClassLoading).
log4j:WARN Please initialize the log4j system properly.
log4j:W开发者_如何转开发ARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

So how can I get web application to load log4j properties?


Put the log4j.properties file into a source directory of your project, e.g. /src. Eclipse will copy it into your target build directory.

I recomend using SLF4J with Log4J, and the SpringSource Tool Suite (STS) for your project.


Here is how to get log4j working using Eclipse with the Google plugin.

Modify appengine-web.xml as follows:

<system-properties> 
  <property name="java.util.logging.config.file" value="WEB-INF/classes/log4j.properties"/> 
</system-properties> 

You can add the following code to your servlet:

import org.apache.log4j.Logger; 
... 
Logger logger = Logger.getLogger("com.foo"); 
logger.debug("Yay2!"); 

Put the log4j.properties file in the src/ directory with the following content:

log4j.rootLogger=DEBUG, stdout 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.Target=System.out 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{1}:%M:%L - %m%n 

You can do a Project > Clean then allow it to automatically build, The build copies the log4j.properties file to /war/WEB-INF/classes/. You'll see the log displayed when you Run As > Web Application and request a URL.

I know that you're not using Maven but I will add instructions below in case anyone else needs them. These instructions will work with com.google.appengine.archetypes:guestbook-archetype.

Add the following to pom.xml:

<dependency> 
<groupId>log4j</groupId> 
<artifactId>log4j</artifactId> 
<version>1.2.16</version> 
</dependency> 

Add the following code to guestbook.jsp:

<%@ page import="org.apache.log4j.Logger" %> 
... 
<% 
Logger logger = Logger.getLogger("com.foo"); 
logger.debug("Yay2!"); 
%> 

Create src/main/webapp/WEB-INF/classes/log4j.properties with the same content as above.

Then run:

mvn clean 
mvn verify 
mvn appengine:devserver 

You will see log output in our console after calling http://localhost:8080/.


I have to put the log4j.properties and then configured in web.xml:

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> 
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

That way it runs before the classpath loading and it works.

Although it doesn't send the JUL stuff to log4j, so you will need a separated configuration to do that.


Ignore everything you see in the internet for the keywords spring + log4j + appengine.

The solution that worked for me and didn't created ambiguity was to leave JUL be and configure log4j with spring separately in this way:

public class CustomXmlWebApplicationContext extends XmlWebApplicationContext {
@Override
protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
    super.initBeanDefinitionReader(beanDefinitionReader);

    try {
        Resource res = beanDefinitionReader.getResourceLoader().getResource("classpath:log4j.properties");
        Properties props = new Properties();
        props.load(res.getInputStream());
        PropertyConfigurator.configure(props);
    }
    catch(Throwable e) {
        e.printStackTrace();
    }
}

}

Then just put your log4j.properties in the root of your source folder.


There is a good article on Logging in the Google AppEngine for Java (GAE/J) with Slf4j, Log4j and JUL.

In \src\main\webapp\WEB-INF\appengine-web.xml you need to have

appengine-web.xml

<!-- Configure java.util.logging -->
<system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/java-util-logging.properties"/>
</system-properties>

to tell GAE where java.util.logging (JUL) is configured.

In \src\main\webapp\WEB-INF\java-util-logging.properties you need

java-util-logging.properties

.level = ALL

or another of the JUL level names, as you like (i.e. 'TRACE' does not work).

In \src\main\resources\log4j.properties file you will have

log4j.properties

log4j.rootLogger=ALL, stdout
# or a lower log level such as DEBUG, INFO or WARN. 

# Define the destination and format of our logging
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p: %m  at %C.(%F:%L) on %d{ISO8601}%n

You need to add log4j to your CLASSPATH. I use Gradle for dependency management, so here is my build script:

build.gradle

configurations {
    all*.exclude group: "commons-logging", module: "commons-logging"
}

dependencies {
    // Logging
    compile 'org.slf4j:slf4j-api:1.7.+'
    runtime 'org.slf4j:slf4j-jdk14:1.7.+'
    runtime ('log4j:log4j:1.2.17') {
        exclude group: "com.sun.jdmk", module: "jmxtools"
        exclude group: "com.sun.jmx", module: "jmxri"
        exclude group: "javax.mail", module: "mail"
        exclude group: "javax.jms", module: "jms"
    }    
}

If you are using Spring WebMVC, in order to provide a Log4J configuration file when starting up your application add the following listener to your Deployment descriptor.

web.xml

<!-- The definition of the Log4j Configuration -->
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜