Search code examples
javaeclipsegoogle-app-enginejava.util.logging

Google Cloud Logging Handler Not Found


My google app engine local development server returns a classdef not found exception when I point the logger to google clouds logging handler, how do I fix this?

INFO: Dev App Server is now running
Can't load log handler "com.google.cloud.logging.LoggingHandler"
java.lang.ClassNotFoundException: com.google.cloud.logging.LoggingHandler
java.lang.ClassNotFoundException: com.google.cloud.logging.LoggingHandler
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.util.logging.LogManager$5.run(LogManager.java:965)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.util.logging.LogManager.loadLoggerHandlers(LogManager.java:958)
    at java.util.logging.LogManager.addLogger(LogManager.java:1165)
    at java.util.logging.LogManager.demandLogger(LogManager.java:556)
    at java.util.logging.Logger.demandLogger(Logger.java:455)
    at java.util.logging.Logger.getLogger(Logger.java:502)
    at coffee.weneed.chat.CoffeeChat.<clinit>(CoffeeChat.java:37)
    at coffee.weneed.chat.kik.KikServlet.doGet(KikServlet.java:18)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at ...

my logging.properties:

.level = INFO
io.grpc.netty.level=INFO
sun.net.level=INFO

coffee.weneed.chat.CoffeeChat.handlers=com.google.cloud.logging.LoggingHandler,java.util.logging.ConsoleHandler
com.google.cloud.logging.LoggingHandler.log=coffee_chat
com.google.cloud.logging.LoggingHandler.level=INFO

com.google.cloud.logging.LoggingHandler.flushLevel=SEVERE
com.google.cloud.logging.LoggingHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s

and my system properties:

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

I have included the cloud logging into the maven deps, the cloud maven plugin is installed. I just can't quite find out why it won't load the class.


Solution

  • You have to use code to install a custom handler in GAE. This is described as a feature request for GAE. It is also a known issue with the JDK as JDK-6878454: LogManager class loading inconsistent with Java EE best practices.

    The issue is that the LogManager uses the system class loader to locate handlers but your code is located in a web app class loader which will be a child class loader. The child class loader can locate classes in the parent but not the other way around. This is the cause of your java.lang.ClassNotFoundException.

    You can follow this example setting up a custom handler setup in GAE. This uses a ServletContextListener to gain access to the correct class loader.