userDSSR
userDSSR

Reputation: 657

Configure Log4j2 in Google App Engine

I looked on the web and could not fing an easy to follow guide to enable Log4j2 for Google App Engine. Being new to this feature.. I found 3 main files that are probably relevent.

1) appengine-web.xml 2) log4j.properties 3) log4j2.xml

My Log4j.properties is shown below: (this has been included in Web-Inf)

log4j.appender.toFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.toFile.File=./log/logfile.log
log4j.appender.toFile.DatePattern='.'
log4j.appender.toFile.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=% %-5p [%t] - %c.%M - %m%n

I added the following to the appengine-web.xml

  <!-- Configure apache Log4J Logging -->
  <system-properties>
  <property name="org.apache.logging.log4j.config.file" value="WEB-    INF/log4j.properties"/>
  </system-properties>

While runng the Java application, I get the error.

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

My intentions were to log the entries into a file. Any pointers on where I can find an example to follow specifically for Google App Engine.

Upvotes: 1

Views: 1627

Answers (2)

konqi
konqi

Reputation: 5227

The reason why there is no good tutorial for log4j2 on GAE is because you cannot use log4j2 on GAE with a file as output. The GAE file system is read-only. You will not be able to log into a file on vanilla app engine. You might be able to do this in a managed vm.

App Engine uses java.util.logging which can be configured in the logging.properties file in your WEB-INF folder.

If you want to make your logging mechanism more reusable i recommend you take a look at slf4j. You can add the maven dependency like this (if you use maven):

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>RELEASE</version>
    </dependency>

If you add a bit of lombok to your mix you can annotate your classes with @Slf4j and start using the logger with log.info()/.warn()/.error().

I find this to be the best solution for logging on app engine with Java so far.

One more thing to think about: If you could write logs to a file in App Engine, how would you make your logs available to yourself but not the public? Wouldn't you end up with having to write a ui for accessing your log files? And if you do... why not just use the logging facilities App Engine provides?

Upvotes: 2

rgoers
rgoers

Reputation: 9151

Log4j2 does not use log4j.properties. That file is for Log4j 1.x. The latest version of log4j2 does support configuration with a properties file but the syntax is different than log4j1. It also supports XML, JSON and YAML.

As far as Google App Engine, I am afraid I have never tried that.

Upvotes: 0

Related Questions