Logger. getLogger and LogFactory. getLog, logger. getlogger

Source: Internet
Author: User

Logger. getLogger and LogFactory. getLog, logger. getlogger

Logger. getLogger and LogFactory. getLog

Logger. getLogger
LogFactory. getLog

Logger comes from the log4j package. If you use Logger. getLogger, you need a log4j jar package. In this way, you can only rely on log4j:
LogFactory comes from the common-logging package. If LogFactory. getLog is used, you can replace log4j with any logger that implements a common log interface, without affecting the program. Apache's common-logging package is a common log interface. Through this intermediate layer, you can specify which log system to use. Increase system flexibility. If log4j does not exist, commons-logging will select another log implementation class. This ensures that the log4j log file does not have to be used in the program.
Reasons for increasing flexibility:
1) First find your own configuration file commons-logging.properties under classpath, if found, use the defined Log implementation class;

2) If the commons-logging.properties file is not found, you can use the defined Log implementation class to find the system environment variable org. apache. commons. logging. Log that has been defined;

3) Otherwise, check whether the classpath contains a Log4j package. If any package is found, Log4j is automatically used as the log implementation class;

4) Otherwise, use the JDK's own log implementation class (the log implementation class is available only after JDK1.4 );

5) Otherwise, use commons-logging to provide a simple log implementation class SimpleLog;


To simplify the configuration of commons-logging, do not use the commons-logging configuration file or set system environment variables related to commons-logging, instead, you only need to place the Jar package of Log4j in classpash. This completes the integration of commons-logging and Log4j.

Based on different properties, log information is usually divided into different levels, from low to high: "DEBUG", "INFO", and "warning (WARN) "" ERROR "" FATAL )".


Running Mode Based on common-logging:
Package org;
Import org. apache. commons. logging. Log;
Import org. apache. log4j. Logger;
Public class Test extends TagSupport {
Public static Log log = LogFactory. getLog (Test. class );
Public static void test ()
{
Log. debug ("111 ");
Log.info ("125 ");
Log. warn ("485 ");
Log. error ("error ");

}
Public static void main (String [])
{
Test. test ();
}
}

Log4j-based Running Mode
Import org. apache. log4j. Logger;
Import org. apache. log4j. PropertyConfigurator;
Public class TestLog4j {

Static Logger logger = Logger. getLogger (TestLog4j. class );
Public static void main (String args []) {
PropertyConfigurator. configure ("log4j. properties ");
Logger. debug ("Here is some DEBUG ");
Logger.info ("Here is some INFO ");
Logger. warn ("Here is some WARN ");
Logger. error ("Here is some ERROR ");
Logger. fatal ("Here is some FATAL ");
}
}

-----------
Commons-logging only packs Log4j (including other LOG implementations of course) layer by layer. The specific LOG output is still transferred internally to Log4j behind it for processing, log4j also goes to the classes directory by default to find log4j. properties File

 

 

 

======================================

Can log4j automatically create folders?
For example, I want to output the log file
F: \ log \ testLog. log
However, my drive does not have a log folder.
Can log4j be configured in the program to automatically create a log folder?

The log4j version is 1.2.9 or later.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.