Skip to content

Instantly share code, notes, and snippets.

@ianlintner
Created October 31, 2022 21:48
Show Gist options
  • Save ianlintner/9c5fea895618225dbf19f8f3e678a015 to your computer and use it in GitHub Desktop.
Save ianlintner/9c5fea895618225dbf19f8f3e678a015 to your computer and use it in GitHub Desktop.
Google Cloud Logs for Scala ZIO2 Apps on Kubernetes

How to add Google Cloud Logging for Kubernetes to a Scala ZIO2 Application

This is based on the google logback appender docs: https://cloud.google.com/logging/docs/setup/java

  • Add the google-cloud-logging-logback to your sbt file see example.
    • If you don't already have the slf4j and zio logging libs add those as well.
  • Add the logback.xml appender below this has been scoped for k8s_container.
  • If you haven't use the slf4j appender ZLayer in your main application see the example for a ZIOAppDefault app below.
val googleCloudLoggingLogback = "0.127.8-alpha"
//...
Seq(
//...
/**
* "dev.zio" %% "zio-logging" % zioLoggingVersion,
* "dev.zio" %% "zio-logging-slf4j" % zioLoggingVersion,
* "ch.qos.logback" % "logback-classic" % logbackClassicVersion,
*/
"com.google.cloud" % "google-cloud-logging-logback" % googleCloudLoggingLogback //, ...
)
<configuration>
<appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
<log>my-app-name.log</log>
<resourceType>k8s_container</resourceType>
<flushLevel>WARN</flushLevel>
<redirectToStdout>true</redirectToStdout>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
<root level="INFO">
<appender-ref ref="CLOUD" />
</root>
</configuration>
object MyApp extends ZIOAppDefault {
// This is the line that gets the slf4j loggers to work with zio2 logging.
override val bootstrap: ZLayer[Any, Nothing, Unit] = Runtime.removeDefaultLoggers ++ SLF4J.slf4j
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment