开发者

What is Logging, and how is Apache Commons logging used?

开发者 https://www.devze.com 2023-02-14 18:13 出处:网络
What information would a web application server wish to log, and why? From what I understand org.apache.commons.logging.Log

What information would a web application server wish to log, and why?

From what I understand

org.apache.commons.logging.Log

is an interface that abstracts the functionality pr开发者_开发知识库ovided by other Logging classes, and the same applies to the interface 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? How can I see the implementation of the Log and LogFactory classes being used?


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);

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.

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>


someString is just logger name (or alias if you want, usually class name). You not need to know implementation of Log and LogFactory just configure and use it.

Information that web application should to log is what developer of this application want to know (e.g. handling of exceptions, some state flow, other useful information)


We're using code similar to Log logger = LogFactory.getLog(MyCurrentClass.class); in our web layer (Struts2 Actions or JSF Backing Beans) as well as our EJB 3 Session Beans.

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). 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().

Generally, each logger gets a name which you provide to the LogFactory.getLog() method, either as a string or class. 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.*. This way you can filter messages in order to get a smaller/better readable log.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号