Logging Audit Logs Using log4j

I have an application that needs to record two different types of logs: application log and audit log. The application log is used for debugging purposes, while the audit log is used to record completed operations. Both logs will be in different files, and each file should only have the logs that are mentioned (for example, the audit log file cannot have the application log and vice versa).

How can this be implemented with log4j?
I know one way to implement this by defining a user level log in log4j. Is there any other / better way to do it?

+7
source share
5 answers

I had the same use case. In your log4j.xml you can define two different loggers and an application for each of them. Example:

<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 logical outputs to the specified file.

+4
source

I do not think you need a new level. Rather, you'll need a Logger (or a set of Loggers ).

Usually you create an instance with the name of the class / package. However, for audit purposes, you can simply create a new Logger with the name "Audit" (or similar), and then configure it accordingly using standard mechanisms.

+3
source

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

+2
source

User level logging is an option, but I'm not sure if you need it.

You can define 2 applications: one for logging and one for auditing, which writes to the audit file. You can then create a logger associated with the audit application and use it to report on the audit progress, saving all other reports using the regular logs that you usually use.

0
source

The application in Log4J supports filters. You can use LevelMatchFilter http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/varia/LevelMatchFilter.html to separate messages in 2 files

So basically you define 2 applications for two different files, each of which has its own filters.

0
source

All Articles