Logging Done Right on AppEngine for Java
Posted on : 06-04-2011 | By : Sam Edwards | In : App Engine, Frameworks
Tags: builds, Java, java util logging, log4j, maven, slf4j
0
I have found that using slf4j is the best bet when logging with App Engine for Java. You log against their API and then you provide a binding to any of the supported logging frameworks at runtime. In the case of App Engine, java.util.logging is used primarily, however is isn’t as flexible as log4j so in order to get around this, I use a slf4j and a log4j binding during development and a jdk (java.util.logging) binding for releases/deploys. This means that all my logs will be accessible through the App Engine administrative panel.
Here is the <profiles> configuration of our Maven pom.xml file in order to setup a defult “development” profile with log4j and a “release” profile for deploys to App Engine.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<slf4j.version>1.6.1</slf4j.version>
<log4j.version>1.2.16</log4j.version>
</properties>
...
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<!-- LOG4J logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>release</id>
<dependencies>
<!-- Java Util Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
logging.properties
This should go at /WEB-INF/logging.properties or wherever is specified in your appengine-web.xml file in this line:
<!-- Configure java.util.logging --> <system-properties> <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" /> </system-properties>
.level = WARNING com.YOURPACKAGENAME.level=ALL
log4j.properties
This should go in the root of your source directory. So in a maven project in /src/main/java/log4j.properties
# console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p: %m at %C.(%F:%L) on %d{ISO8601}%n
#Get Stuff I want
log4j.category.com.YOURPACKAGENAME=ALL, console

