简体   繁体   English

在servlet 3.0中使用Log4j2

[英]using Log4j2 in servlet 3.0

I am using Log4j2 for logging purpose in my web application. 我在我的Web应用程序中使用Log4j2进行日志记录。 If I create any simple java class and call my logger , it works perfectly fine and print the logs in a file. 如果我创建任何简单的java类并调用我的记录器,它可以很好地工作并将日志打印在一个文件中。 However if I do the same in servlet class, it is not working. 但是,如果我在servlet类中执行相同操作,则无法正常工作。 As specified in the documentation I have not configured anything related to log4j2 in web.xml. 正如文档中所指定的,我没有在web.xml中配置与log4j2相关的任何内容。

Code for Web.xml: Web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns="http://xmlns.jcp.org/xml/ns/javaee"     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SampleWebApp</display-name>
<welcome-file-list>
<welcome-file>Welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>

Logging code in Servlet class: 在Servlet类中记录代码:

public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger log=LogManager.getLogger(MyServlet.class);

I have not explicitly placed log4j2.xml file in WEB-INF folder ,since I have added this file in classpath , its automatically added in WEB-INF/classes after I build the project. 我没有明确地将log4j2.xml文件放在WEB-INF文件夹中,因为我在classpath中添加了这个文件,在构建项目后它自动添加到WEB-INF / classes中。

Am I missing some configurations? 我错过了一些配置吗?

Line of Code : 代码行:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        log.error("i am inside servlet");
        /*response.getWriter().append("Served at: ").append(request.getContextPath());*/
        System.out.println("GET called");

    }

log4j2.xml : log4j2.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration>
      <Appenders>
        <Console name="Console">
          <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <File name="MyFile" filename="C:/Users/jasleen_kathuria/Documents/logs/info.log">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
      </Appenders>
<Loggers>
<logger name="MyServlet" level="TRACE">
        <AppenderRef ref="MyFile"/>
    </logger>
</Loggers>

You should say at least how you build/deploy your web-app, on which servlet container, and how you add things to classpath. 您应该至少说明如何构建/部署Web应用程序,在哪个servlet容器上以及如何向classpath 添加内容

All the same, the main problem is probably the fact that you are not adding log4j2 jar in WEB-INF/lib and or it's configuration in WEB-INF/classes. 同样,主要问题可能是你没有在WEB-INF / lib中添加log4j2 jar,或者在WEB-INF / classes中添加它的配置。

When you call your simple java class , you are relying to a different classpath from your web-app. 当您调用简单的java类时,您依赖于Web应用程序中的不同类路径。

Make sure that log4j2.jar is deployed, for example you can build the war unzip it and see what it contains. 确保部署了log4j2.jar,例如你可以构建war解压缩它并查看它包含的内容。

Well it got resolved after I restarted my Tomcat and cleared my Browser cache. 好吧,在我重新启动Tomcat并清除浏览器缓存后,它得到了解决。 I am able to see the logs being printed now. 我现在可以看到正在打印的日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM