What is logging and how is Apache Commons logging used?

What information does the web application server want to register and why?

From what I understand

org.apache.commons.logging.Log

is an interface that abstracts the functionality provided by other logging classes, and the same goes for the LogFactory interface.

The code I'm trying to understand is

Log auditLogger = LogFactory.getLog(someString);

How is String someString used to determine what LogFactory will generate? How can I see the implementation of the used Log and LogFactory classes?

+3
source share
3 answers

identifier, . - , ,

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

, commons-logging - , java.util.logging, . , ​​ log4j, logback slf4j .

, , , 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>
+5

someString - ( , , ). Log LogFactory .

, - , - , (, , , ).

+1

, Log logger = LogFactory.getLog(MyCurrentClass.class); - (Struts2 Actions JSF Backing Beans), EJB 3 Beans.

, (, , , , , ..).

logger.debug("Debug message") , , ( Log4J DEBUG , ). / .., , , logger.isDebugEnabled().

, , LogFactory.getLog(), . .

For example, if you have a log for my.package.MyClassand use the library with the package org.library.*, you can configure the logging framework to enable debugging messages for my.package.*, but not for org.library.*. This way you can filter messages to get a smaller / improved readable log.

+1
source

Source: https://habr.com/ru/post/1795986/


All Articles