0

I'm trying to do some Logging modification on My Standard AppEngine Java 11 app using a logging.properties file, My app is a Jetty web-server which I run by adding the following entry-point to my app.yaml file

runtime: java11
entrypoint: 'java -cp "*" com.jettymain.Main webapp.war'

Now Google documentation here suggests that in order to use logging.properties. The path to the configuration file must be provided to your application as a system property: -Djava.util.logging.config.file=/path/to/logging.properties

I have tried setting that in the code, first thing in the com.jettymain.Main class which starts the Jetty embedded web-server by doing the following

System.setProperty("java.util.logging.config.file", "WEB-INF/logging.properties")

But that didn't work, modifying the entry-point in app.yaml did make the web-server load those configurations but it was failing to load the Google cloud logging handler class com.google.cloud.logging.LoggingHandler, which I need to write those logs to Google Stackdriver, I have the Google cloud logging dependency added to both my app and the web-server but that didn't help.

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-logging</artifactId>
  <version>3.0.1</version>
</dependency>

modified entry-pint

runtime: java11
entrypoint: 'java -cp "*" com.jettymain.Main webapp.war -Djava.util.logging.config.file=WEB-INF/logging.properties'

logging.properties file, it is the sample file which can be found here plus few extra things

# To use this configuration, add to system properties : -Djava.util.logging.config.file="/path/to/file"
.level = INFO

# it is recommended that io.grpc and sun.net logging level is kept at INFO level,
# as both these packages are used by Cloud internals and can result in verbose / initialization problems.
io.grpc.netty.level=INFO
sun.net.level=INFO

handlers=com.google.cloud.logging.LoggingHandler
# default : java.log
com.google.cloud.logging.LoggingHandler.log=custom_log

# default : INFO
com.google.cloud.logging.LoggingHandler.level=FINE

# default : ERROR
com.google.cloud.logging.LoggingHandler.flushLevel=EMERGENCY

# default : auto-detected, fallback "global"
com.google.cloud.logging.LoggingHandler.resourceType=container

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

# Level for all logs coming from GWT client (can't filter by specific classes on client)
com.google.gwt.logging.server.RemoteLoggingServiceUtil.level = WARNING
com.beoui.geocell.level=WARNING
montss
  • 454
  • 5
  • 18
  • I assume you are using java.util.logging handler for setting up Cloud Logging for Java and not the Cloud logging library for Java directly? – Priyashree Bhadra Aug 26 '21 at 06:39

2 Answers2

1

It might be an old question, but this answer still might help to someone having same issue.

I've managed to add custom logging.properties using Gradle for Java 11 Standard Environment:

  1. First of all, you have to add dependency to the Cloud logging as it's no longer provided by the environment:

    dependencies {
      runtimeOnly("com.google.cloud:google-cloud-logging:3.11.3")
      // your other dependencies
    }
    
  2. Then you need to specify a folder containing your logging.properties file to be uploaded by Gradle com.google.cloud.tools.appengine plugin:

    configure<AppEngineAppYamlExtension> {
      stage {
        setArtifact("build/libs/server.jar")
        // can be some other folder of your choice where logging.properties is located
        setExtraFilesDirectories("src/main/resources")
      }
      deploy {
        version = "GCLOUD_CONFIG"
        projectId = "GCLOUD_CONFIG"
      }
    }
    
  3. Then specify an entrypoint in app.yaml. The trick here is that properties file can be found in /workspace/ directory of the GAE container:

    runtime: java11
    entrypoint: 'java -jar -Djava.util.logging.config.file=/workspace/logging.properties server.jar'
    
  4. Deploy application using

    ./gradlew appengineDeploy
    

My logging.properties file:

.level = INFO

# it is recommended that io.grpc and sun.net logging level is kept at INFO level,
# as both these packages are used by Cloud internals and can result in verbose / initialization problems.
io.grpc.netty.level=INFO
sun.net.level=INFO

handlers=com.google.cloud.logging.LoggingHandler
com.google.cloud.logging.LoggingHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s
Meos
  • 66
  • 5
  • Thank you, since then I have managed to supply the logging.properties file to my app, switched to the Legacy java 11 runtime and provided it to the app through appengine-web.xml file as a system property same as we used to do with gen 1 My problem now is that when I start the app I get a problem similar to what is described here https://stackoverflow.com/questions/55778891/google-cloud-logging-handler-not-found – montss Sep 26 '22 at 14:17
  • @montss I believe it's related to the fact that google cloud logging dependency have to be provided with the application itself, at least from my tests – Meos Sep 27 '22 at 13:14
  • Yes the Logging Handler class should be available to the system class loader during the app creation, which I still don't have a sleek way to do yet – montss Sep 28 '22 at 12:04
  • 1
    @montss The only why I was able to install a handler in was to use a [ServletContextListener to install the handler](https://stackoverflow.com/questions/4716711/google-app-engine-configure-default-logger-to-send-email). – jmehrens Sep 30 '22 at 01:32
-1

Get the System.setProperty into any method. It can be the main method also. I think the problem is "the logging.properties must go in WEB-INF/classes directory" as per this link and not WEB-INF/ directory as mentioned in your code.

System.setProperty("java.util.logging.config.file", "WEB-INF/classes/logging.properties")
Priyashree Bhadra
  • 3,182
  • 6
  • 23
  • Not sure where to execute this command, in Appengine Standard environment app.yaml entrypoint seems the only place to do similar thing, and I tried that as I mentioned but it didn't work. – montss Aug 30 '21 at 08:48
  • Execute the command I posted in the answer in your terminal if you are a Mac user or in Command Line if you are a Windows User. You can use Cloud Shell too. Specify your MainClass and provide the correct path. – Priyashree Bhadra Aug 30 '21 at 08:57
  • I want to run my app on a Deployed standard Appengine environment in the could, so I don't have access to any terminal, I don't have any problem with the app or the logs locally. – montss Aug 30 '21 at 09:38
  • Okay I get it now. As mentioned in the Google docs "The path to the configuration file must be provided to your application as a system property: -Djava.util.logging.config.file=/path/to/logging.properties". You are translating this path/to/ logging.properties to be " WEB-INF/logging.properties". Why do you think it should be set as WEB-INF/logging.properties? – Priyashree Bhadra Aug 30 '21 at 15:58
  • Because I have my logging.properties file in that directory. The question is how to do the "The path to the configuration file must be provided to your application as a system property". – montss Aug 31 '21 at 12:02
  • Just get the System.setProperty into any method. It can be the main method also. I think the problem is "the logging.properties must go in WEB-INF/classes directory" as per this [link](https://stackoverflow.com/a/14361928/15803365) and not WEB-INF/ directory as mentioned in your code. – Priyashree Bhadra Aug 31 '21 at 12:38
  • @montss Did you try setting System.setProperty in your main method like this System.setProperty("java.util.logging.config.file", "WEB-INF/classes/logging.properties")? Please let me know if this resolved your issue. – Priyashree Bhadra Sep 01 '21 at 14:39