Google App Engine flexible environment automatically pipes stdout and stderr to Stackdriver (Google Cloud Logging). But this only supports plain text log message without any
It's quite possible to do structured logging in StackDriver logging. In fact, each individual log entry uses the LogEntry structure. As you can see it includes severity and metadata.
According to the App Engine documentation, the following logs are provided out of the box:
Request logs record requests sent to all App Engine apps. The request log is provided by default and you cannot opt out of receiving it.
App logs record activity by software within the App Engine app. The log is provided by default and you cannot opt out of receiving it.
Runtime logs are provided from the flexible environment using a preinstalled Logging agent.
You could use the Logback appender + Implement LoggingEnhacer
public class LogEnhancer implements LoggingEnhancer {
@Override
public void enhanceLogEntry(LogEntry.Builder logEntry) {
// add Labels
logEntry.addLabel("project", "conversational-services");
// Transform textPayload to JSONPayload
ObjectMapper mapper = new ObjectMapper();
Builder structBuilder = Struct.newBuilder();
String textPayload = logEntry.build().getPayload().getData().toString();
try {
// Validate JSON Payload
mapper.readTree(textPayload);
JsonFormat.parser().merge(textPayload, structBuilder);
logEntry.setPayload(JsonPayload.of(structBuilder.build()));
} catch (InvalidProtocolBufferException e) {
// Error reporting an error! FML
System.err.println(e.getMessage());
} catch (IOException e) {
// Do nothing (there is not a JSON Payload)
}
}
}
This class add labels and transform a JSON String in a JSONPayload
You need to specify the LoggingEnhacer on the logback.xml file
<!DOCTYPE configuration>
<configuration>
<appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<log>application.log</log>
<resourceType>gae_app</resourceType>
<!-- References to the LoggingEnhacer class -->
<enhancer>[path_for_your_logging_enhancer_class]</enhancer>
<flushLevel>WARN</flushLevel>
</appender>
<root level="info">
<appender-ref ref="CLOUD" />
</root>
</configuration>