Skip to content

Instantly share code, notes, and snippets.

@traviskaufman
Last active March 20, 2023 08:38
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save traviskaufman/d13b29807c0be730db45 to your computer and use it in GitHub Desktop.
Save traviskaufman/d13b29807c0be730db45 to your computer and use it in GitHub Desktop.
Logback: Disable all logging in unit tests

After scouring the internet and piece-mealing together the correct way to do this, here is a step-by-step, all-in-one-place guide to making logback STFU when running your unit tests.

Here's how to do it

Save the following as logback-test.xml under src/test/resources:

<configuration>
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>
  <root level="OFF">
    <appender-ref ref="CONSOLE"/>
  </root>
</configuration>

Bonus: Disabling logging when testking akka actors

If you're using akka-testkit, make sure you pass a config string that tells akka to turn off logging.

package whatever

import akka.actor.ActorSystem
import akka.testkit.{ TestKit, ImplicitSender }
import com.typesafe.config.ConfigFactory
import org.scalatest.WordSpecLike
import org.scalatest.Matchers
import org.scalatest.BeforeAndAfterAll

class MySpec(_system: ActorSystem) extends TestKit(_system) with ImplicitSender
  with WordSpecLike with Matchers with BeforeAndAfterAll {
 
  def this() = this(ActorSystem("MySpec", ConfigFactory.parseString("""
    akka.loggers = ["akka.testkit.TestEventListener"]
    akka.stdout-loglevel = "OFF"
    akka.loglevel = "OFF"
  """)))
  
  // stuff
}

At Refinery29 we've abstracted this into a base ActorSpec class we can extend our actor tests from

package whatever

import java.util.UUID

import akka.actor.ActorSystem
import akka.testkit.{ ImplicitSender, TestKit }
import com.typesafe.config.ConfigFactory

import org.scalatest.{ BeforeAndAfterAll, Matchers, OneInstancePerTest, WordSpecLike }

/**
 * Used as the base class when testing actors.
 * Provides automatic setup and teardown of ActorSystems, and bootstraps the ActorSystem
 * to suppress annoying logging.
 */
abstract class ActorSpec(_system: ActorSystem) extends TestKit(_system)
    with ImplicitSender with WordSpecLike with OneInstancePerTest with Matchers with BeforeAndAfterAll {

  def this() = this(ActorSystem(s"ActorSpec-${UUID.randomUUID}", ConfigFactory.parseString("""
    akka.loggers = ["akka.testkit.TestEventListener"]
    akka.stdout-loglevel = "OFF"
    akka.loglevel = "OFF"
  """)))

  override def afterAll {
    TestKit.shutdownActorSystem(system)
  }
}
@sumew
Copy link

sumew commented Dec 10, 2015

In my case I had to rename logback-test.xml to application-logger.xml before I got it to STFU :)

@erip
Copy link

erip commented Sep 1, 2016

Incredibly helpful! Thank you. 👍

@samudurand
Copy link

Great ! Was trying using SLF4J-nop but it doesn't seem to be enough when logback is on top of it

@lJoublanc
Copy link

Hmm ... I'm suffering the exact opposite problem in IntelliJ IDEA w/ the ScalaTest plugin. I've got the equivalent set-up using SLF4J and logback, butno matter what I put in the level parameter, I don't get any error messages except from the Test framework. I need to some application traces to see what's wrong!

@keweishang
Copy link

Great! It works perfectly! Thank you!

@apflieger
Copy link

Thanks

@ioleo
Copy link

ioleo commented Jul 12, 2018

Or simply add $project/src/test/resources/logback-test.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!-- only one line, shut up logback ! -->
<configuration />

@wjzz
Copy link

wjzz commented Apr 9, 2021

Thank you!

@duyng2512
Copy link

Thanks you sir, it work !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment