java 使用 log4j 编写审计日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12579187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing audit logs using log4j
提问by Newbie
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?
如何使用 log4j 实现?
我知道实现这一点的一种方法是在 log4j 中定义自定义日志级别。还有其他/更好的方法吗?
采纳答案by AnarchoEnte
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:
我有同样的用例。在您的 log4j.xml 中,您可以为每个定义两个不同的记录器和一个附加程序。因此,一个例子:
<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.
在您的 Java 代码中,您可以使用“Logger.getLogger("LOGGER_1")”创建一个记录器,它将日志输出写入定义的文件。
回答by Brian Agnew
I don't think you need a new level. Rather, you need a particular Logger(or set of Loggers
).
我不认为你需要一个新的水平。相反,您需要一个特定的Logger(或一组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.
通常,您使用类/包名称实例化它们。但是,出于审计目的,您可以简单地实例化一个Logger
名为“Audit”(或类似名称)的新对象,然后使用标准机制进行适当的配置。
回答by rgoers
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.htmland http://logging.apache.org/log4j/2.x/manual/appenders.html#FlumeAppenderfor more information. If you have other questions please feel free to ask on the Apache Log4j mailing lists.
Log4j 2.0 的创建支持审计日志记录作为其主要目的之一。推荐的使用模式是使用 Log4j(或 SLF4J)的 EventLogger,然后使用 FlumeAppender,它可以为您的目标日志存储库提供有保证的交付。有关更多信息,请参阅http://logging.apache.org/log4j/2.x/manual/eventlogging.html和http://logging.apache.org/log4j/2.x/manual/appenders.html#FlumeAppender。如果您有其他问题,请随时在 Apache Log4j 邮件列表上提问。
回答by arikg
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.
您可以定义 2 个 appender:一个用于日志记录,另一个用于写入审计文件的审计。然后,您可以创建一个与审计附加程序连接的记录器,并使用它来报告审计操作,同时使用您通常使用的常规记录器保留所有其他报告。
回答by Matei Florescu
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
Log4J 中的 appender 支持过滤器。为了分离 2 个文件中的消息,您可以使用 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.
所以基本上你为 2 个不同的文件定义了 2 个 appender,每个文件都有合适的过滤器。