views:

16

answers:

1

My project runs on Wicket+Spring+JPA/Hibernate. When I run it using the command:

mvn jetty:run

I'd like jetty to print logs I make in the code. I have for example the following DAO implemented:

@Repository(value = "isinDao")
public class IsinDaoJpa implements IsinDao {

    @PersistenceContext
    private EntityManager em;

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

    public Isin findById(Long id) {
        return em.find(Isin.class, id);
    }

    public List findAll() {
        Query query = em.createQuery("select e from Isin e");
        logger.info("DAO: All ISINs selected");
        return query.getResultList();
    }
}

How do I make Jetty print this information real-time into the command line window?

In pom.xml, I have the following dependencies:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.4.2</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
</dependency>
+1  A: 

Since you use SLF4-to-log4j bridge, you need to configure logging in log4j configuration.

To configure log4j logging with Spring you need to add the following to web.xml (before ContextLoaderListener):

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

Then configure log4j in /WEB-INF/log4j.xml (you may also use traditional log4j.properties):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
    <!-- Appenders -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p: %c\n\t%m%n" />
        </layout>
    </appender>

    <logger name="... your package ...">
        <level value="info" />
    </logger>
</log4j:configuration>

See also:

axtavt