Differences between Logger.getlogger () and Logfactory.getlog () in Java logging

Source: Internet
Author: User

When I recently studied the project code, I found that the same project used two different ways of writing about the log:

The difference between Logger.getlogger () and Logfactory.getlog ()
1.logger.getlogger () is logging using the Log4j method;API documentation

2.logfactory.getlog () is from Apache's common-logging package. API documentation

Depending on the nature, log information is usually divided into different levels, from low to High: Debug (Debug) information (info) "Warning (WARN)" "Error (ERR)" "Fatal Error (FATAL)"

Spring AOP inside Private Logger Logger=logger.getlogger (Tasktest.class);

Tasktest This class registers to log, then it can call the configuration inside the log4j for log output

Log logger = Logfactory.getlog (Tasktest.class);


Why use both commons-logging and log4j?

Common-logging components:
Jakarta Commons Logging (JCL) provides a log interface (interface), taking into account both lightweight and non-dependent log implementation tools. It provides a simple log manipulation abstraction for middleware/log tool developers, allowing program developers to use different specific log implementation tools.

logfactory from common-logging bag. If you use logfactory.getlog, you can replace log4j with any logger that implements the common log interface, and the program is not affected. Apache's common-logging package is a general-purpose log interface through which you can randomly specify which log system to use. Increase the flexibility of the system. If log4j does not exist, commons-logging will select a different log implementation class. This ensures that the log4j log file is not necessarily used in the program.

Reasons to increase flexibility:
1)
first find its own configuration file under Classpath commons-logging.properties, if found, then use the Log implementation class defined therein;

2) If the commons-logging.properties file is not found, locate the Log implementation class whose definition is used if the system environment variable ORG.APACHE.COMMONS.LOGGING.LOG is defined;

3) Otherwise, see if there are log4j packages in classpath and, if found, automatically use log4j as the log implementation class;

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

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


In order to simplify the configuration commons-logging, the commons-logging configuration file is not used, and the commons-logging-related system environment variables are not set, but only the log4j Jar package can be placed in Classpash. In this way, it is easy to complete the fusion of commons-logging and log4j.

log4j :

log4j System of three major sections: Log writer, log output terminal, log layout mode
In practice, log4j is typically used through configuration file configuration. There are two types of configuration files, Java properties and XML files. The properties file is usually used for configuration, because it is simple and easy to read. The following describes only how Java properties is configured.
The configuration of the log4j is the configuration of the Rootlogger and sub-logger. The main configuration items are: Rootlogger, output terminal, output layout mode, all configuration items must start with log4j.

<span style= "FONT-SIZE:18PX;" ># #Log4J的配置之简单使它遍及于越来越多的应用中了 # #Log4J配置文件实现了输出到控制台, files, rollback files, send log messages, output to database log tables, custom labels, and more.     The one or two use is enough.   # #此文件 (log4j.properties) content from the network, not the author Liigo original. Log4j.rootlogger = DEBUG, console,a1 Log4j.addivity.org.apache = true # applied to console Log4j.appender.CONSOLE = Org.apache. Log4j. Consoleappender Log4j.appender.Threshold = DEBUG Log4j.appender.CONSOLE.Target = System.out LOG4J.APPENDER.CONSOLE.L Ayout = Org.apache.log4j.PatternLayout Log4j.appender.CONSOLE.layout.ConversionPattern = [Framework]% d-% c-%-4r [ % T]%-5p% c% x-% m% n #log4j. Appender.CONSOLE.layout.ConversionPattern = [start]% d {date} [date]% n P[priori TY]% n X[NDC]% n t[thread] n% c[category]% n% m[message]% n n #应用于文件 log4j.appender.FILE = Org.apache.lo G4j. Fileappender Log4j.appender.FILE.File = File.log Log4j.appender.FILE.Append = False log4j.appender.FILE.layout = Org. Apache.log4j.PatternLayout Log4j.appender.FILE.layout.ConversionPatteRN = [Framework]% d-% c-%-4r [% t]%-5p% c% x-% m% n # Use this layout for Logfactor 5 Analysis # apply to file back Roll Log4j.appender.ROLLING_FILE = Org.apache.log4j.RollingFileAppender Log4j.appender.ROLLING_FILE. Threshold = ERROR Log4j.appender.ROLLING_FILE. File = Rolling.log Log4j.appender.ROLLING_FILE. Append = True Log4j.appender.ROLLING_FILE. MaxFileSize = 10KB Log4j.appender.ROLLING_FILE. Maxbackupindex = 1 Log4j.appender.ROLLING_FILE.layout = org.apache.log4j.PatternLayout log4j.appender.ROLLING_ FILE.layout.ConversionPattern = [Framework]% d-% c-%-4r [% t]%-5p% c% x-% m% n #应用于socket log4j.appende   R.socket = Org.apache.log4j.RollingFileAppender Log4j.appender.SOCKET.RemoteHost = localhost Log4j.appender.SOCKET.Port = 5001 Log4j.appender.SOCKET.LocationInfo = true # Set up for Log Facter 5 Log4j.appender.SO  Cket.layout = Org.apache.log4j.PatternLayout Log4j.appender.SOCET.layout.ConversionPattern = [start]% d {date} [date]% N% p[priority]% n% x[ndc]% n t[thread]% n% c[category]% n M[message]% n N # Log Factor 5 Appender log4j.appender. Lf5_appender = Org.apache.log4j.lf5.LF5Appender Log4j.appender.LF5_APPENDER. maxnumberofrecords = 2000 # send log to message Log4j.appender.MAIL = Org.apache.log4j.net.SMTPAppender log4j.appender.MAIL.Thr Eshold = FATA Log4j.appender.MAIL.BufferSize = Ten log4j.appender.MAIL.From = [email protected] Log4j.appender.MA IL.   SMTPHost = www.wusetu.com Log4j.appender.MAIL.Subject = log4j Message log4j.appender.MAIL.To = [email protected] Log4j.appender.MAIL.layout = Org.apache.log4j.PatternLayout Log4j.appender.MAIL.layout.ConversionPattern = [ Framework]% d-% c-%-4r [% t]%-5p% c% x-% m% n # for database Log4j.appender.DATABASE = ORG.APACHE.LOG4J.JDBC . Jdbcappender Log4j.appender.DATABASE.URL = jdbc:mysql://localhost:3306/test log4j.appender.DATABASE.driver = Com.mys Ql.jdbc.Driver Log4j.appender.DATABASE.user = root Log4j.appender.DATABase.password = Log4j.appender.DATABASE.sql = INSERT into log4j (Message) VALUES (' [Framework]%d-%c-%-4r [%t]%-5p %c%x-%m%n ') Log4j.appender.DATABASE.layout = Org.apache.log4j.PatternLayout Log4j.appender.DATABASE.layout.Conve Rsionpattern = [Framework]% d-% c-%-4r [% t]%-5p% c% x-% m% n log4j.appender.A1 = Org.apache.log4j.DailyRol Lingfileappender Log4j.appender.A1.File = samplemessages.log4j Log4j.appender.A1.DatePattern = Yyyymmdd-hh '. log4j ' Log4j.appender.A1.layout = org.apache.log4j.xml.XMLLayout #自定义Appender log4j.appender.im = NET.CYBERCORLIN.UTIL.L Ogger.appender.IMAppender Log4j.appender.im.host = mail.cybercorlin.net log4j.appender.im.username = Username log4j. Appender.im.password = Password Log4j.appender.im.recipient = [email protected] log4j.appender.im.layout = Org.ap Ache.log4j.PatternLayout Log4j.appender.im.layout.ConversionPattern = [Framework]% d-% c-%-4r [% t]%-5p% c x -% m% n # End </SPAN> 



Differences between Logger.getlogger () and Logfactory.getlog () in Java logging

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.