简体   繁体   English

什么是日志记录,Apache Commons日志记录是如何使用的?

[英]What is Logging, and how is Apache Commons logging used?

What information would a web application server wish to log, and why? Web应用程序服务器希望记录哪些信息,为什么?

From what I understand 据我所知

org.apache.commons.logging.Log

is an interface that abstracts the functionality provided by other Logging classes, and the same applies to the interface LogFactory. 是一个抽象其他Logging类提供的功能的接口,同样适用于接口LogFactory。

Code I am trying to understand has - 我想了解的代码有 -

Log auditLogger = LogFactory.getLog(someString);

How is the String someString used to identify what LogFactory to generate? String someString如何用于标识要生成的LogFactory? How can I see the implementation of the Log and LogFactory classes being used? 如何查看正在使用的Log和LogFactory类的实现?

The string is an arbitrary identifier which we can use to redirect or filter the logging output. 该字符串是一个任意identifier ,我们可以用它来重定向过滤日志输出。 One common approach is to use the className, eg, 一种常见的方法是使用className,例如,

Log auditLogger = LogFactory.getLog(MyCurrentClass.class);

As you said, commons-logging is a facade which defaults to java.util.logging if no other logging library is supplied. 正如您所说, commons-logging是一个外观,如果没有提供其他日志库,则默认为java.util.logging I would recommend to put a logging implementation such as log4j , logback , or slf4j in the classpath . 我建议在类路径中放置一个日志实现,如log4jlogbackslf4j

Assuming you put eg, log4j there you can control the logging output using a configuration file log4j.xml , such as: 假设您将eg, log4j放在那里,您可以使用配置文件log4j.xml控制日志记录输出,例如:

<?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>

someString is just logger name (or alias if you want, usually class name). someString只是记录器名称(如果需要,可以是别名,通常是类名)。 You not need to know implementation of Log and LogFactory just configure and use it. 您不需要知道LogLogFactory只需配置和使用它。

Information that web application should to log is what developer of this application want to know (eg handling of exceptions, some state flow, other useful information) Web应用程序应记录的信息是此应用程序的开发人员想要知道的(例如,处理异常,某些状态流,其他有用信息)

We're using code similar to Log logger = LogFactory.getLog(MyCurrentClass.class); 我们使用的代码类似于Log logger = LogFactory.getLog(MyCurrentClass.class); in our web layer (Struts2 Actions or JSF Backing Beans) as well as our EJB 3 Session Beans. 在我们的Web层(Struts2 Actions或JSF Backing Beans)以及我们的EJB 3会话Bean中。

We then primarily log errors and warnings but usefull information, too (like the start and end of a scheduled job, user login, certain user actions to see what they were doing just before an error occured etc.). 然后,我们主要记录错误和警告,但也有用的信息(例如预定作业的开始和结束,用户登录,某些用户操作,以便在发生错误之前查看他们正在做什么等)。

You also might want to use logger.debug("Debug message") to log more information when the logging framework is enabled to accept those (in Log4J you would set the threshold to DEBUG or lower, which can be done at runtime). 您还可能希望使用logger.debug("Debug message")在启用日志框架以接受这些信息时记录更多信息(在Log4J中,您可以将阈值设置为DEBUG或更低,这可以在运行时完成)。 If the debug message first needs to be extracted/assembled etc. which might be too expensive for the common case where you don't need it, you could first check logger.isDebugEnabled() . 如果首先需要提取/组装调试消息,这对于您不需要它的常见情况可能太昂贵,您可以先检查logger.isDebugEnabled()

Generally, each logger gets a name which you provide to the LogFactory.getLog() method, either as a string or class. 通常,每个记录器都会获得一个名称,您可以将其作为字符串或类提供给LogFactory.getLog()方法。 This enables you to set different settings per logger or group. 这使您可以为每个记录器或组设置不同的设置。

For example, if you have a logger for my.package.MyClass and use a library with package org.library.* you could configure the logging framework to enable debug messages for my.package.* but not for org.library.* . 例如,如果您有my.package.MyClass的记录器并使用包org.library.*的库,则可以配置日志记录框架以启用my.package.*调试消息,但不能用于org.library.* This way you can filter messages in order to get a smaller/better readable log. 这样,您可以过滤消息以获得更小/更好的可读日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM