How do I disable logging for a specific dependency

2019-04-23 23:45发布

问题:

I have the following build.sbt file:

version := "0.1"

scalaVersion := "2.10.0-RC1"

scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")

resolvers ++= Seq(
  "sonatype releases" at "https://oss.sonatype.org/content/repositories/releases/",
  "sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/",
  "typesafe repo" at "http://repo.typesafe.com/typesafe/releases/",
  "spray repo" at "http://repo.spray.io/"
)

libraryDependencies ++= Seq(
  "io.spray"           %   "spray-can"         % "1.1-M4.2"
  ,"io.spray"          %   "spray-routing"     % "1.1-M4.2"
  ,"io.spray"          %   "spray-testkit"     % "1.1-M4.2"
  ,"io.spray"          %%  "spray-json"        % "1.2.2"     cross CrossVersion.full
  ,"com.typesafe.akka" %%  "akka-actor"        % "2.1.0-RC1" cross CrossVersion.full
  ,"org.specs2"        %%  "specs2" % "1.12.2" % "test"      cross CrossVersion.full
  ,"com.typesafe"      %   "slick_2.10.0-RC1"  % "0.11.2"
  ,"com.h2database"    %   "h2"                % "1.3.166"
  ,"org.xerial"        %   "sqlite-jdbc"       % "3.6.20"
  ,"org.slf4j"         %   "slf4j-api"         % "1.6.4"
  ,"ch.qos.logback"    %   "logback-classic"   % "1.0.7"
  ,"org.specs2"        %   "specs2_2.10.0-RC1" % "1.12.2"    % "test"
  ,"junit"             %   "junit"             % "4.8.1"     % "test"
)

How do I enable DEBUG level reporting for my own (the current) project, but disable it for another. In this case I don't want to see the Slick library's debug output, but still want to see debug logging for my own project.

回答1:

In your logback.xml add an entry like this:

<logger name="com.typesafe.slick" level="INFO"/>

This means, that when a logger is obtained by any class of the namespace com.typesafe.slick it will have INFO set as log level.

edit: Here's the link to the documentation.



回答2:

To see what name the logger of you dependency have you can use the standard logging appender in your logback.xml. So if you have the following:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{1000} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

You will get something like as a log message:

12:53:09 [AmexReporting-akka.stream.default-blocking-io-dispatcher-6] INFO net.schmizz.sshj.connection.channel.direct.SessionChannel - Will request sftp subsystem

Then you can add a logger between the appender and root level configuration:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{1000} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="net.schmizz.sshj" level="OFF"/>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Where the logger name is a part or the full package name that you can see in the log output, depending on how granular you want your log settings to be.