2010-10-18 97 views
1

我的項目在Wicket + Spring + JPA/Hibernate上運行。當我使用命令運行它:如何使Jetty打印日誌

mvn jetty:run

我想碼頭打印日誌,我在代碼中。例如,我已經實現了以下DAO:

@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(); 
    } 
} 

我該如何讓Jetty實時將這些信息打印到命令行窗口中?

在pom.xml中,我有以下的依存關係:

<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

由於您使用SLF4到log4j的橋,你需要在log4j的配置配置日誌。

配置Log4j日誌記錄使用Spring,你需要將以下添加到web.xml(前ContextLoaderListener):

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

然後在/WEB-INF/log4j.xml配置log4j(你也可以使用傳統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/"> 
    <!-- 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> 

另請參閱: