2

When logging errors to stackdriver, every message is logged as INFO, even when using log.error or log.warn, etc., but the payload is correct.

enter image description here

I'd like to be able to filter by severity and get email on error.

I'm using Spring Boot and Logback. The app has been deployed on a Kubernetes Cluster on GCP.

Here is my logback-spring.xml

<configuration>
    <include resource="org/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss, UTC} %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>

    <springProfile name="prod,qa">

        <root level="WARN">
            <appender-ref ref="STACKDRIVER" />
        </root>
    </springProfile>

</configuration>

And here is the dep added in Maven

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-gcp-starter-logging</artifactId>
</dependency>

Spring Boot version: 2.1.3.RELEASE
Spring Cloud version: Greenwich.RELEASE

What is wrong with this config? Is there any other solution?

EDIT: Just realized that the STACKDRIVER appender above is not the one logging to Stackdriver, but STDOUT is enough (maybe bc it's a Kubernetes cluster?), but the issue persists

CCC
  • 2,427
  • 5
  • 35
  • 54
  • Do you need logback for logging separately? You can keep that in application.yml as you are using spring boot. Check this one https://www.mkyong.com/spring-boot/spring-boot-slf4j-logging-example/ – Arun Apr 22 '19 at 21:57

2 Answers2

3

The Stackdriver logging agent configuration for Kubernetes defaults to INFO for any logs written to the container's stdout and ERROR for logs written to stderr. If you want finer-grained control over severity, you can configure Spring to log as single-line JSON (e.g., via JsonLayout1) and let the logging agent pick up the severity from the JSON object (see https://cloud.google.com/logging/docs/agent/configuration#process-payload).

1By default, JsonLayout will use "level" for the log level, while the Stackdriver logging agent recognizes "severity", so you may have to override addCustomDataToJsonMap.

See also GKE & Stackdriver: Java logback logging format?

Igor Peshansky
  • 667
  • 4
  • 11
  • I extended JsonLayout and have overridden addCustomDataToJsonMap, adding "severity" to the map, then changed the xml using that layout for ConsoleAppender, but even though I can see it locally, I can't see it in StackDriver – CCC May 06 '19 at 03:38
  • That other SO answer did it though :) – CCC May 06 '19 at 04:24
1

Directly using google cloud logging logback appender takes the severity from the log level on each case:

In Maven:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-logging-logback</artifactId>
    <version>0.116.0-alpha</version>
</dependency>

In logback.xml:

<appender name="Cloud" class="com.google.cloud.logging.logback.LoggingAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>INFO</level>
            </filter>
            <log>YOUR_LOG_NAME</log>
            <resourceType>container</resourceType>
            <flushLevel>INFO</flushLevel>
</appender>

...
    <logger name="org.springframework" level="WARN" additivity="true">
        <appender-ref ref="Cloud"/>
    </logger>

It is not the spring cloud component but solves the problem.