Using log4j in Google App Engine

I need to use log4j in my application, but I do not know how I can load properties. The Deafult property files say that I have to put log4j.properties in / WEB -INF / classes / folder, but in eclipse I don’t see this folder and I can’t create it because it already exists. And I can not add files to this folder.

Here is the error I get:

log4j:WARN No appenders could be found for logger (DataNucleus.ClassLoading). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 

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

+4
source share
5 answers

Put the log4j.properties file in the source directory of your project, for example. / Src. Eclipse will copy it to the target assembly directory.

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

+6
source

Here's how to get log4j 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!"); 

Place the log4j.properties file in the src / directory with the following contents:

 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 run the project> Clear, and then let it build automatically, the assembly copies the log4j.properties file to / war / WEB -INF / classes /. You will see a log displayed at startup as> Web Application and requesting a URL.

I know that you are not using Maven, but I will add the instructions below if 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 contents as above.

Then run:

 mvn clean mvn verify mvn appengine:devserver 

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

+4
source

I need to put log4j.properties and then configure 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> 

Thus, it starts before loading the class and works.

Although it does not send JUL stuff to log4j, you will need a separate configuration for this.

+1
source

Ignore everything you see on the Internet for the keywords spring + log4j + appengine.

The solution, which worked for me and did not create ambiguity, was to leave JUL and configure log4j with spring as follows:

 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 the source folder.

0
source

There is a good article on Logging in to Google AppEngine for Java (GAE / J) using 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 (ie. "TRACE" does not work).

In the file \src\main\resources\log4j.properties 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 to manage dependencies, 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 use Spring WebMVC to provide the Log4J configuration file at application startup, add the following listener to the 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
source

All Articles