1

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.

userDSSR
  • 627
  • 1
  • 8
  • 17
  • Independent from where your log4j2 configuration is, you simply cannot log into a file on App Engine. See: http://stackoverflow.com/questions/2693081/does-google-app-engine-allow-creation-of-files-and-folders-on-the-server . I let my answer reflect that and suggested you use a logging facade for reusability (if that is a concern of yours). Sadly i got downvoted for that. – konqi Nov 06 '15 at 13:56

2 Answers2

2

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?

konqi
  • 5,137
  • 3
  • 34
  • 52
  • Why would using slf4j be any different than log4j2? Anything you can do in Slf4j should be doable with Log4j2. – rgoers Nov 05 '15 at 14:25
  • 1
    @rgoers I'm having difficulty understanding your statement. slf4j is **NOT** a logger itself but a facade to utilize all sorts of logging facilities like java.util.logging or log4j or log4j2. You might be able to configure log4j2 to log into java.util.logging. That seems pointless though as it would be one logging facility logging into another. What good would that do? With slf4j one can reuse the same class with the same calls to logging independent of what the actual logging facility is or how it is configured. – konqi Nov 05 '15 at 15:51
  • SLF4J is a logging API. It must be bound to a logging implementation. Log4j2 also has an API and an implementation. Yes, the point of SLF4J is to be an abstraction for logging, but that in no way solves the problem of not being able to log to a file. Logging via java.util.logging is just as likely to have the same problem. So I have to ask again, how is using SLF4J going to solve the problem asked here? – rgoers Dec 26 '15 at 11:22
  • What I should be asking is, what is java.util.logging doing in the app engine that cannot be done in another logging framework? – rgoers Dec 26 '15 at 11:29
  • 1
    @rgoers It's really simple. You **can** **not** **ever** log to a file in app engine because the file system is read only. Therefor that problem cannot be solved (well unless you would use managed VMs and mount cloud storage via fuse, which would be a special kind of stupidity). I love log4j and log4j2 very much but it is not a good candidate for logging in app engine, because, as i've said before: The only supported logger in app engine is `java.util.logging`. I'm just wrapping slf4j around that to make my classes usable in non-app-engine code (in cases i can use log4j2). – konqi Dec 26 '15 at 11:38
  • You still didn't answer the question. Where does j.u.l write the log events to? Knowing that I would ask why Log4j can't write them there as well. – rgoers Dec 27 '15 at 13:51
  • 1
    @rgoers App Engine has an own implementation of `java.util.logging` and the logs end up in the console of your project, the mechanism is unknown. In managed VMs you can log to certain files (see: https://cloud.google.com/appengine/articles/logging), everywhere else you can use the Logs Api (see: https://cloud.google.com/appengine/docs/java/logs/). One could write an appender for the API but it's likely poor in performance in comparison to the officially supported logging method. If you wish more details about how Google implements `jul` i recommend you contact Google's technical support. – konqi Dec 27 '15 at 15:35
0

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.

rgoers
  • 8,696
  • 1
  • 22
  • 24