Since recent runtimes, AppEngine
is evolving with a behaviour that is more and more converging with a container based approach, more "opened" as new other products (like Cloud Run for example).
This is changing a little the way we're developing with GAE
, specific legacy libraries aren't available (SearchAPI...), and it is changing also how logs are managed.
We can reproduce this "nested log feature" with new java11
runtime, but we need to manage it ourself.
As official docs mentioned:
In the Logs Viewer, log entries correlated by the same trace can be
viewed in a "parent-child" format.
It means, if we retrieve the trace
identifier received inside X-Cloud-Trace-Context
HTTP header of our request, we can then use it to add a new LogEntry
by passing it as the trace
identifier attribute.
This can be done by using Stackdriver Logging Client libraries
With Spring GCP
Fortunately, Spring Cloud GCP is there to make our lives easier.
You can find a sample project which implements it. Be careful, it's a AppEngine Flexible
example, but it will work fine with Standard
runtime.
It uses Logback.
From a working Spring Boot project on GAE Java11
, steps to follow are :
- Add
spring-cloud-gcp-starter-logging
dependency :
<!-- Starter for Stackriver Logging -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-logging</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
- Add a
logback-spring.xml
inside src/main/resources
folder :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml" />
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="INFO">
<!-- If running in GCP, remove the CONSOLE appender otherwise logs will be duplicated. -->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="STACKDRIVER" />
</root>
</configuration>
- Enable Spring GCP logging feature, inside
src/main/resources/application.properties
:
spring.cloud.gcp.logging.enabled=true
- And use LOGGER inside your code:
@SpringBootApplication
@RestController
public class DemoApplication {
private static final Log LOGGER = LogFactory.getLog(DemoApplication.class);
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping()
public SomeData get() {
LOGGER.info("My info message");
LOGGER.warn("My warning message");
LOGGER.error("My error message");
return new SomeData("Hello from Spring boot !");
}
}
Result will be in Stackdriver Logging
viewer, for appengine.googleapis.com/request_log
:
runtime: java11 instance_class: F4 inbound_services: - warmup # Explicitly set the memory limit and maximum heap size for the Spring Boot app env_variables: JAVA_TOOL_OPTIONS: "-XX:MaxRAM=512m -XX:ActiveProcessorCount=2 -Xmx128m"
– Fradin