在 Web 应用程序中使用 log4j 的最佳方式是什么?

发布于 2025-01-05 21:45:58 字数 355 浏览 2 评论 0原文

我们正在启动一个基于 Spring MVC 的 Web 应用程序。我们将使用 tomcat 作为 Web 服务器。

我需要在应用程序中配置 log4j 并记录到应用程序特定文件而不是 tomcat 日志文件。

例如,tomcat 有自己的日志文件,例如 localhost.log 等。我想要在 tomcats 日志文件夹中包含类似 myAppName.log 的内容。

日志记录配置将位于应用程序 war 文件中的 lo4j.xml 或 log4j.properties 中。

另外,我不想在 Web 应用程序中对输出日志文件进行硬编码。

但我不知道该怎么做。

请帮我。如果我有错的地方也请纠正我。

We are starting a Spring MVC based web application. We will be using tomcat as the web server.

I need to configure log4j in the application and log to the application specific file and not to the tomcat log files.

e.g. tomcat has its own log files like localhost.log etc. I want something like myAppName.log in the tomcats log folder.

The logging configuration will go in lo4j.xml or log4j.properties in the application war file.

Plus I dont want to hard code the output log file in the web application.

But I am not sure how to do this.

Please help me. As well correct me if I am wrong somewhere.

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

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

发布评论

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

评论(2

痴骨ら 2025-01-12 21:45:58

这样做,使用以下代码初始化记录器,

Logger log = Logger.getLogger(this.getClass());

记录如下信息,

log.debug("My message");

并将 log4j.xml 放在类路径中。内容如下,

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
     <appender name="infoLogsFile" class="org.apache.log4j.RollingFileAppender">
         <param name="File" value="MyApplication.log"/>     
         <param name="Threshold" value="DEBUG"/>
         <param name="MaxFileSize" value="100MB"/>
         <param name="ImmediateFlush" value="TRUE"/>
         <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
         </layout>
    </appender>
   <root> 
      <priority value ="DEBUG" /> 
      <appender-ref ref="infoLogsFile"/>
   </root>
</log4j:configuration>

不要忘记添加所需的 jar,如 log4jXXX.jars 和 apache 常见日志记录 jar。这样,您将能够看到在 Tomcat 的 bin 文件夹中创建的 MyApplication.log 文件中的所有日志消息。

Do like this, initialize the logger with following code,

Logger log = Logger.getLogger(this.getClass());

log the information like follows,

log.debug("My message");

and place the log4j.xml in your class path. content like follows,

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
     <appender name="infoLogsFile" class="org.apache.log4j.RollingFileAppender">
         <param name="File" value="MyApplication.log"/>     
         <param name="Threshold" value="DEBUG"/>
         <param name="MaxFileSize" value="100MB"/>
         <param name="ImmediateFlush" value="TRUE"/>
         <layout class="org.apache.log4j.PatternLayout">
           <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
         </layout>
    </appender>
   <root> 
      <priority value ="DEBUG" /> 
      <appender-ref ref="infoLogsFile"/>
   </root>
</log4j:configuration>

Do not forget to add the required jars like log4jXXX.jars and apache common logging jars. with this you will be able to see all log messages in MyApplication.log file creates in bin folder of your tomcat.

2025-01-12 21:45:58

试试这个:

  • 将 log4j jar 作为 Web 应用程序的一部分

  • 不要将配置作为 Web 应用程序的一部分

  • 在任意位置创建 log4j.xml

  • 启动 tomcat 时提供此参数
    -Dlog4j.configuration=file:///.../log4j.xml

Try this:

  • Put the log4j jar as part of the web application

  • Do not put a configuration as part of your web application

  • Create your log4j.xml wherever you like

  • When you start tomcat provide this argument
    -Dlog4j.configuration=file:///.../log4j.xml

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