什么是日志记录,以及Apache Commons日志记录是如何使用的? [英] What is Logging, and how is Apache Commons logging used?
问题描述
Web应用程序服务器希望记录哪些信息,以及为什么?
What information would a web application server wish to log, and why?
根据我的理解
org.apache.commons.logging.Log
是一个接口,它抽象其他Logging类提供的功能,同样适用于接口LogFactory。
is an interface that abstracts the functionality provided by other Logging classes, and the same applies to the interface LogFactory.
我想了解的代码有 -
Code I am trying to understand has -
Log auditLogger = LogFactory.getLog(someString);
如何使用String someString来识别要生成的LogFactory?如何查看正在使用的Log和LogFactory类的实现?
How is the String someString used to identify what LogFactory to generate? How can I see the implementation of the Log and LogFactory classes being used?
推荐答案
字符串是任意标识符
我们可以使用重定向或过滤日志记录输出。一种常见的方法是使用className,例如,
The string is an arbitrary identifier
which we can use to redirect or filter the logging output. One common approach is to use the className, e.g.,
Log auditLogger = LogFactory.getLog(MyCurrentClass.class);
如你所说, commons-logging
是如果没有提供其他日志记录库,则默认为 java.util.logging
的Facade。
我建议将日志记录实现,例如 log4j , logback ,或 类路径中的slf4j 。
As you said, commons-logging
is a facade which defaults to java.util.logging
if no other logging library is supplied.
I would recommend to put a logging implementation such as log4j, logback, or slf4j in the classpath.
假设您将eg, log4j
放在那里,您可以使用配置文件 log4j.xml
,例如:
Assuming you put e.g., log4j
there you can control the logging output using a configuration file log4j.xml
, such as:
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
.....
</appender>
<!-- Send debug information from "com.company.MyCurrentClass" to CONSOLE -->
<logger name="com.company.MyCurrentClass">
<level value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
</logger>
</log4j:configuration>
这篇关于什么是日志记录,以及Apache Commons日志记录是如何使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!