如何让Jetty打印日志

发布于 2024-09-27 22:41:25 字数 1168 浏览 8 评论 0原文

我的项目运行在 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();
    }
}

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

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光无声 2024-10-04 22:41:25

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

要使用 Spring 配置 log4j 日志记录,您需要将以下内容添加到 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>

另请参阅:

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/">
    <!-- 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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文