Java Log Technology Rollup (log4j, commons-logging,.....)

Source: Internet
Author: User
Tags log log java util log4j

Objective

There are a number of ways to implement logging in Java,

1. The simplest way to do this is system.print.out, err to print the message directly on the console.

2. java.util.logging; After the JDK version 1.4, a log API was provided to write logs to the file.

3. log4j, the most powerful way to log logs. You can configure the destination, format, and so on for the log by configuring the. Properties or. xml files.

4. Commons-logging, the most comprehensive and common logging method, is often used in conjunction with log4j.

Java.util.logging--JDK logging mode

System.print that's not much to say,

Take a look at the usage examples of logging logs in the Java API:

[Java]View Plaincopy
  1. /**
  2. * @author oscar999
  3. * @date 2013-8-1
  4. * @version V1.0
  5. */
  6. Package com.oscar999.log;
  7. Import java.io.IOException;
  8. Import Java.util.Date;
  9. Import Java.util.logging.FileHandler;
  10. Import Java.util.logging.Formatter;
  11. Import Java.util.logging.Level;
  12. Import Java.util.logging.LogRecord;
  13. Import Java.util.logging.Logger;
  14. Public class Testlogjava {
  15. public static void Main (string[] args) throws ioexception{
  16. Logger log = Logger.getlogger ("Tesglog");
  17. Log.setlevel (Level.all);
  18. Filehandler Filehandler = new Filehandler ("Testlog.log");
  19. Filehandler.setlevel (Level.all);
  20. Filehandler.setformatter (new Logformatter ());
  21. Log.addhandler (Filehandler);
  22. Log.info ("This is test Java util log");
  23. }
  24. }
  25. Class Logformatter extends Formatter {
  26. @Override
  27. Public String Format (LogRecord record) {
  28. Date date = new Date ();
  29. String sdate = date.tostring ();
  30. return "[" + Sdate + "]" + "[" + record.getlevel () + "]"
  31. + record.getclass () + record.getmessage () + "\ n";
  32. }
  33. }

Here is the code and test under Eclipse.

First define an instance of Logeer, set the log level, and then add a filehander that writes the log to the file. When writing to a file, define a logformatter to render the log in a format.

By default, the log is printed to the console. When Filehandler is added, the file is written at the same time. If you do not specify a path, the log file will be located under the project root path.

log4j logging mode

The log4j is the jar file provided by Apache for logging.

Download path:

Http://logging.apache.org/log4j/1.2/download.html

There's a little bit more to do here:

1. Download the log4j jar package and put it in the project's Lib package (added to the project's build path).

2. Configure the log4j.properties and put it under the project's root path. (You can also put other paths, which you need to specify when reading)

Take a look at a configuration instance:

[HTML]View Plaincopy
  1. log4j.rootlogger=Debug,stdout,logfile
  2. log4j.appender.stdout=Org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l)-%m%n
  5. log4j.appender.logfile=Org.apache.log4j.RollingFileAppender
  6. log4j.appender.logfile.file=Logfile.log
  7. log4j.appender.logfile.maxfilesize=512KB
  8. log4j.appender.logfile.maxbackupindex=3
  9. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
  10. log4j.appender.logfile.layout.conversionpattern=%d%p [%c]-%m%n

This specifies the level of log output debug.

STDOUT, logfile specifies the destination of the log output. These two names can be taken casually, such as a, or B. The actual configuration is Org.apache.log4j.ConsoleAppender and Rollingfileappender used to specify whether the console or the file.

It also specifies the format of the output, the rules of the file that have been generated.

3. Test Java files

[Java]View Plaincopy
  1. /**
  2. * @author oscar999
  3. * @date 2013-8-1
  4. * @version V1.0
  5. */
  6. Package com.oscar999.log;
  7. Import Org.apache.log4j.Logger;
  8. Import Org.apache.log4j.PropertyConfigurator;
  9. Public class Testlog4j {
  10. public static void Main (string[] args) {
  11. //1. Create Log
  12. Logger log = Logger.getlogger (testlog4j.   Class);
  13. //2. Get log config file
  14. Propertyconfigurator.configure ("log4j.properties");
  15. //3. Start Log
  16. Log.debug ("Here is some debug");
  17. Log.info ("Here is some info");
  18. Log.warn ("Here is some warn");
  19. Log.error ("Here is some error");
  20. Log.fatal ("Here is some fatal");
  21. }
  22. }


Configuration is a little cumbersome, but code is much simpler.

Commons-logging Write Log mode

Commons-logging is also the log jar file provided by Apache.

http://commons.apache.org/proper/commons-logging/download_logging.cgi

You may have to ask why there are log4j and offer commons-logging? What is the difference between the two?

In fact, from the name of Commons-logging can be seen, this should be a log of the common interface. In fact, it really is such a role,

When using Commons-logging's logfactory to get the log processing class:

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, find out if the system environment variable ORG.APACHE.COMMONS.LOGGING.LOG is defined and the Log implementation class with its definition is found;
If you can create an environment variable called: catalina_opts in Tomact
Give him the value:-Dorg.apache.commons.logging.Log = Org.apache.commons.logging.impl.SimpleLog- Dorg.apache.commons.logging.simplelog.defaultlog = Warn
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;

Use the first way to see an instance, configure Commons-logging.properties, and use log4j to log logs.

Note that the commons-logging must be imported into the project with the log4j jar package to match the log4j log.

1. Importing jar packages for log4j and commons-logging

2. Configure commons-logging.properties and log4j.properties to be placed under the project's classpath (i.e. src directory)

Note: When using log4j alone, log4j.properties is placed in the project's root directory by default.

The content of the log4j.properties is exactly the same as above.

Take a look at the configuration of commons-logging.properties

[HTML]View Plaincopy
    1. org.apache.commons.logging.log=Org.apache.commons.logging.impl.Log4JLogger

In a word, specify the use of log4j

3. Test the code:

[Java]View Plaincopy
  1. /**
  2. * @author oscar999
  3. * @date 2013-8-1
  4. * @version V1.0
  5. */
  6. Package com.oscar999.log;
  7. Import Org.apache.commons.logging.Log;
  8. Import Org.apache.commons.logging.LogFactory;
  9. Public class Testlogcom {
  10. static Log log = Logfactory.getlog (testlog.   Class);
  11. public static void Main (string[] args) {
  12. Log.debug ("Here is some debug");
  13. Log.info ("Here is some info");
  14. Log.warn ("Here is some warn");
  15. Log.error ("Here is some error");
  16. Log.fatal ("Here is some fatal");
  17. }
  18. }


In addition to using LOG4J, you can configure

-org.apache.commons.logging.impl. Jdk14loggerUse JDK1.4. -org.apache.commons.logging.impl. Log4jloggerUse log4j. -org.apache.commons.logging.impl. LogkitloggerUse Avalon-logkit. -org.apache.commons.logging.impl. SimplelogCommon-logging comes with a log implementation class. It implements the log interface and outputs the log messages to the system error stream system.err. -org.apache.commons.logging.impl. NooplogCommon-logging comes with a log implementation class. It implements the log interface. No action is made in the method of its output log.

Summarize

The above has a

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

The project also imports the jar packages for both log4j and commons-logging, without the need to configure commons-logging.properties, just configure them in Classpath Log4j.properties can log logs using the Log4j method. This is also the current way to use a more logged log.

Java Log Technology Rollup (log4j, commons-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.