tostao
tostao

Reputation: 3048

How configure slf4j for simple maven application to display logs properly?

I have simple web application (src is here). I try configure logger. I have logback.xml file, which looks like:

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

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
    </encoder>
  </appender>
  <root level="DEBUG">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

and in pom.xml dependencies:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.5</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.5</version>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>log4j-over-slf4j</artifactId>
  <version>1.7.5</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-nop</artifactId>
  <version>1.7.5</version>
</dependency>

In Index.java Controller, for test is:

LOG.info("LOG Info - Index called!");
LOG.warn("LOG Warn -Index called!");
LOG.trace("LOG Trace -Index called!");
LOG.error("LOG Error -Index called!");
LOG.debug("LOG Debug -Index called!");
System.out.println("System out -Index called!");

when I start app: mvn tomcat7:run and go to page: index page (/simple-spring-mvc) on console is displayed only: System out -Index called!.

Logger looks like:

private static final Logger LOG = LoggerFactory.getLogger(Index.class);

Please explain me, how configure it properly or what I did wrong.

Upvotes: 3

Views: 2398

Answers (1)

Eugen
Eugen

Reputation: 8783

First, if you're using logback, you need to define it as maven dependency:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.11</version>
</dependency>

Then, I notice you're defining slf4j-nop - that's the root cause of your problems - you can go ahead and remove that.

Hope this helps.

Upvotes: 1

Related Questions