Last active
February 3, 2026 20:24
-
-
Save dacr/162a4c30476e5f319f50d6c74fe70f95 to your computer and use it in GitHub Desktop.
ZIO learning - using zio logging with log4j2 / published by https://github.com/dacr/code-examples-manager #3532fec8-5527-403e-a06f-f1a8f9a57dec/9200f13068be700663725f80e4427498d543589d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : ZIO learning - using zio logging with log4j2 | |
| // keywords : scala, zio, learning, logging, pure-functional, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 3532fec8-5527-403e-a06f-f1a8f9a57dec | |
| // created-on : 2022-06-30T22:08:34+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "dev.zio::zio:2.0.13" | |
| //> using dep "dev.zio::zio-logging:2.1.12" | |
| //> using dep "dev.zio::zio-logging-slf4j:2.1.12" | |
| //> using dep "org.apache.logging.log4j:log4j-api:2.20.0" | |
| //> using dep "org.apache.logging.log4j:log4j-core:2.20.0" | |
| //> using dep "org.apache.logging.log4j:log4j-slf4j-impl:2.20.0" | |
| // --------------------- | |
| import zio.* | |
| import zio.logging.* | |
| import zio.logging.backend.SLF4J | |
| // ---------------------------------------- | |
| // configure log4j programmatically | |
| import org.slf4j.{Logger, LoggerFactory} | |
| import org.apache.logging.log4j.{LogManager,Level} | |
| import org.apache.logging.log4j.core.LoggerContext | |
| val context = LogManager.getContext(false).asInstanceOf[LoggerContext] | |
| val config = context.getConfiguration() | |
| val rootConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME) | |
| rootConfig.setLevel(Level.DEBUG) | |
| context.updateLoggers() | |
| // ---------------------------------------- | |
| object Encapsulated extends ZIOAppDefault { | |
| def run = | |
| ZIO | |
| .logInfo("Hello world") | |
| .provide(removeDefaultLoggers, SLF4J.slf4j(format = LogFormat.colored)) | |
| } | |
| Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment