Reputation: 31526
I am using a library in my code which has the following logback.xml
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date - %-5p %t %-25logger{0} %F:%L %m%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I have no control over this .xml file because the library author owns this file and I just use her library as a jar file.
Now when I use the library as a jar, I see lots of "INFO" statements in my output. I want to shut the output of the logger just from this library.
I don't want to globally switch off (or raise severity of logs) for my application. I just want to silence the logs from this library.
How can I do that?
Upvotes: 5
Views: 5566
Reputation: 847
You can specify the location of your logback.xml as a system property, then you can change it.
As the logback document says:
Specifying the location of the default configuration file as a system property
You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
Note that the file extension must be ".xml" or ".groovy". Other extensions are ignored. Explicitly registering a status listener may help debugging issues locating the configuration file.
UPDATE
To suppress logs from a specified package, you can define a logger in the logback.xml.
E.g. To suppress logs from "io.netty" package, add <logger name="io.netty" level="WARN"/> to your logback.xml.
Upvotes: 5
Reputation: 31526
I was able to solve the problem. I am listing my solution here for others.
So the problem is that we have multiple libraries and each has its own logger. we want the output from some and not from others. We can easily simulate this by the example below
package com.abhi
import org.slf4j.LoggerFactory
object Program extends App {
val f = new Foo()
val b = new Bar()
f.sayHello("Test1")
b.sayHello("Test2")
}
class Foo {
val logger = LoggerFactory.getLogger(classOf[Foo])
def sayHello(name: String) : String = {
logger.debug(s"++++++++++++ came inside Foo sayHello(${name}) +++++++++++++++++++++")
"Hello " + name
}
}
class Bar {
val logger = LoggerFactory.getLogger(classOf[Bar])
def sayHello(name: String) : String = {
logger.debug(s"++++++++++++ came inside Bar sayHello(${name}) +++++++++++++++++++++")
"Hello " + name
}
}
Now let's say we want to keep the output from Foo logger but don't want the output from Bar logger.
We will adjust our logback.xml like
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.abhi.Foo" level="TRACE">
<appender-ref ref="STDOUT" />
</logger>
<root level="off" />
</configuration>
Here we are setting a logger explicitly for foo and setting its level to trace and setting the root to off. All people who don't have specific loggers will go to root and will not be able to log anything.
Upvotes: 2