andPat
andPat

Reputation: 4503

Logback logging not working

I've a Spring MVC simple addressbook project build with maven. I want to use Logback for logging, but I'm completely new to it; first I changed my pom.xml this way:

<!-- Spring 3 dependencies -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
  <exclusions>
    <!--Exclude Commons Logging in favor of SLF4j--> 
    <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>   
<!-- Logging -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>${logback.version}</version>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-core</artifactId>
  <version>${logback.version}</version>
</dependency>

then I added this lines to my custom Exception DatabaseException:

private final static Logger logger = (Logger) LoggerFactory.getLogger(DatabaseException.class);

...

public DatabaseException(Throwable cause) {
    super(cause);
    logger.log(Level.WARNING, cause.getMessage());
}

so that when I rise in other classes this exception the log is able to print to stout some messages, BUT DOES'T WORK, when I run maven on Tomcat it prints only container log [talledContainer] INFO .... etc etc. seems like my logger doesn't exists

Upvotes: 0

Views: 7921

Answers (2)

Shinichi Kai
Shinichi Kai

Reputation: 4523

To your DatabaseException make it work, your DatabaseException should be like this:

package org.myorg.myapp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DatabaseException extends Exception {

  private final static Logger logger = LoggerFactory.getLogger(DatabaseException.class);

  public DatabaseException(Throwable cause) {
    super(cause);
    logger.warn(cause.getMessage());
  }
}

Also, you need to include jcl-over-slf4j dependency in your pom.xml.

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

I would recommend reading the reference documentation (Using SLF4J section). http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/overview.html

Hope this helps.

Upvotes: 2

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

You have not mentioned anything about logback.xml. You should create one under src/main/resources folder.

Upvotes: 1

Related Questions