如何制作Jetty打印日志

时间:2010-10-18 13:30:42

标签: java maven-2 logging jetty slf4j

我的项目在Wicket + Spring + JPA / Hibernate上运行。当我使用命令运行它时:

  

mvn jetty:run

我想让jetty打印我在代码中创建的日志。例如,我实现了以下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 个答案:

答案 0 :(得分:1)

由于您使用SLF4-to-log4j网桥,因此需要在log4j配置中配置日志记录。

要使用Spring配置log4j日志记录,您需要将以下内容添加到web.xmlContextLoaderListener之前):

<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>

另见: