日志记录是如何使用的

日志记录是如何使用的

本文介绍了什么是日志记录,Apache Commons 日志记录是如何使用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

字符串是一个任意的identifier,我们可以用它来重定向过滤强> 日志输出.一种常见的方法是使用 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.我建议放置一个日志记录实现,例如 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.

假设您在其中放置了例如 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 日志记录是如何使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 13:57