Alex Dow
Alex Dow

Reputation: 648

How to disable solr logging with SolrJ?

I am using SolrJ for the purposes of an embedded solr server for my unit tests.

The problem is that my maven output is littered with solr logs that I don't really want. For example, I am purposefully triggering a bad query as part of my test. My test passes, but it's very hard to see that because the screen is filled with Solr errors.

I'm not clear how to get around this with SolrJ. I'd like to do this as part of my POM file so that if a test fails, a dev can edit the POM to expose more information.

EDIT:

Solr is instantiated this way:

System.setProperty("solr.solr.home", "solr/")
def coreInitter = new CoreContainer.Initializer()
def core = coreInitter.initialize()
this.server = new EmbeddedSolrServer(core, "")

Upvotes: 2

Views: 1645

Answers (1)

Mark O'Connor
Mark O'Connor

Reputation: 78021

The Solr wiki explains that SLF4J is the logging framework used by versions > 1.4

You have not provided the POM dependencies of your project so I've made up my own:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myspotontheweb.demo</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
               <groupId>org.apache.solr</groupId>
               <artifactId>solr-solrj</artifactId>
               <version>3.6.1</version>
               <exclusions>
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
               </exclusions>
        </dependency>
        <dependency>
               <groupId>org.apache.solr</groupId>
               <artifactId>solr-core</artifactId>
               <version>3.6.1</version>
        </dependency>
    </dependencies>
</project>

This has the following dependency tree:

$ mvn dependency:tree
..
..
[INFO] com.myspotontheweb.demo:demo:jar:1.0-SNAPSHOT
[INFO] +- org.apache.solr:solr-solrj:jar:3.6.1:compile
[INFO] |  +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] |  +- commons-io:commons-io:jar:2.1:compile
[INFO] |  +- org.codehaus.woodstox:wstx-asl:jar:3.2.7:runtime
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.6.1:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO] \- org.apache.solr:solr-core:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-core:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-analyzers:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-highlighter:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-kuromoji:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-memory:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-misc:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-phonetic:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-queries:jar:3.6.1:compile
[INFO]    |  \- jakarta-regexp:jakarta-regexp:jar:1.4:compile
[INFO]    +- org.apache.lucene:lucene-spatial:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-spellchecker:jar:3.6.1:compile
[INFO]    +- org.apache.lucene:lucene-grouping:jar:3.6.1:compile
[INFO]    +- commons-codec:commons-codec:jar:1.6:compile
[INFO]    +- commons-fileupload:commons-fileupload:jar:1.2.1:compile
[INFO]    +- commons-lang:commons-lang:jar:2.6:compile
[INFO]    +- com.google.guava:guava:jar:r05:compile
[INFO]    \- javax.servlet:servlet-api:jar:2.4:compile

None of these is a logging implementation jar. You'll notice that "jcl-over-slf4j" has been added by SolrJ to ensure that commons logging API calls are redirected to SLF4J.

To completely disable SLF4J logging, add the special NOP dependency:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-nop</artifactId>
    <version>1.6.1</version>
    <scope>runtime</scope>
</dependency>

Note the scope declaration. Logging implementation jars are not needed for compiling your code, and can cause problems if another project were to use your project's POM.

Upvotes: 2

Related Questions