Writing audit logs using log4j

梦想与她 提交于 2021-02-05 20:15:15

问题


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

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?


回答1:


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>

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




回答2:


I don't think you need a new level. Rather, you need a particular Logger (or set of Loggers).

Normally you instantiate these with the class/package name. However for audit purposes you could simply instantiate a new Logger with the name "Audit" (or similar) and then configure appropriately using the standard mechanisms.




回答3:


Log4j 2.0 was created with support for audit logging as one of its primary purposes. A recommended usage pattern is to use Log4j's (or SLF4J's) EventLogger and then use the FlumeAppender, which can provide guaranteed delivery to your target log repository. See http://logging.apache.org/log4j/2.x/manual/eventlogging.html and http://logging.apache.org/log4j/2.x/manual/appenders.html#FlumeAppender for more information. If you have other questions please feel free to ask on the Apache Log4j mailing lists.




回答4:


A custom log level is an option but I'm not sure you need it.

You can to define 2 appenders: One for logging and the other for auditing that writes to the auditing file. Then you can create a logger that is connected with the audit appender and use it to report audit operations while keeping all other reports using the regular loggers you normally use.




回答5:


An appender in Log4J supports filters. For separating the messages in the 2 files you can use a LevelMatchFilter http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/varia/LevelMatchFilter.html

So basically you define 2 appenders for the 2 different files, each one with the appropiate filters.



来源:https://stackoverflow.com/questions/12579187/writing-audit-logs-using-log4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!