使用log4j编写审核日志

使用log4j编写审核日志

本文介绍了使用log4j编写审核日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要编写两种不同类型日志的应用程序:应用程序日志和审核日志.应用程序日志用于调试目的,而审核日志用于记录执行的操作.这两个日志都将位于不同的文件中,并且每个文件都应仅具有如上所述的那些日志(意味着审核日志文件不能具有应用程序日志,反之亦然).

I have an application that needs to write logs of two different types: application log and audit log. Application log is used for debug purpose whereas audit log is to record the operations performed. Both logs will be in different files and each file should have only those logs as mentioned (means audit log file cannot have application log and vice versa).

如何使用log4j来实现?
我知道一种实现此目的的方法是通过在log4j中定义自定义日志级别.还有其他更好的方法吗?

How this can be implemented using log4j?
I know one way to implement this is by defining custom log level in log4j. Is there any other/better way to do?

推荐答案

我有相同的用例.在您的log4j.xml中,您可以定义两个不同的记录器和一个附加器.因此,一个例子:

I have had the same use case. In your log4j.xml you can define two different loggers and an appender for each one. An example therefore:

<logger name="LOGGER_1" additivity="false">
    <appender-ref ref="LOGGER_FILE_1"/>
</logger>

<appender name="LOGGER_FILE_1" class="org.jboss.logging.appender.RollingFileAppender">
   <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
   <param name="File" value="${jboss.server.log.dir}/loggerFile1.log"/>
   <param name="Append" value="true"/>
   <param name="MaxFileSize" value="20MB"/>
      <param name="MaxBackupIndex" value="5"/>

      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
      </layout>
</appender>

在Java代码中,您可以使用"Logger.getLogger("LOGGER_1")"创建一个Logger,它将日志输出写入定义的文件.

In your Java-Code you can create a Logger with "Logger.getLogger("LOGGER_1")" which will write the log-outputs to the defined file.

这篇关于使用log4j编写审核日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:51