log4j Learn the first log example of "two"

Source: Internet
Author: User

First need to download log4j package, the current use of the most is still log4j 1.2, although Log4j 2 has been released, Log4j 1.2: http://logging.apache.org/log4j/1.2/download.html

Download complete decompression, log4j is a very simple code package, which itself does not rely on any other jar package, only need to copy the Log4j-1.2.17.jar to the corresponding classpath.

Start using log4j now:

Package Cn.hucc.log.first;import org.apache.log4j.logger;/** * Simplest example of log4j * @auth HUCC * @date August 26, 2015 */public class Simplelog {private static Logger Logger = Logger.getlogger (simplelog.class);p ublic static void Main (string[] args) {Logge R.debug ("Debug Log ..."); Logger.info ("Info log ..."); Logger.warn ("Warn log ..."); Logger.error ("Error log ....");
}

Please note that when importing the Logger class, you need to select Org.apache.log4j.Logger, Instead of using Java.util.logging.Logger, because after JDK4, Java itself provides a set of log frameworks.

This code is literally a simple introduction. First, we use the static method of logger GetLogger by passing in the type of Simplelog, get a Logger object instance, it is easy to understand, this logger instance is

Simplelog the exclusive logger for this class, then we can use the Logger object to log information.

After completing the code, print the console output as follows warning:

This is because we did not tell log4j where the log output was, that we did not provide a configuration file for log4j, so log4j does not know how to process the log information, you can add a sentence in the code:

Basicconfigurator.configure ();

What is the function of this sentence, we can look at the note:

The Configure () method of the base Configurator configures a consoleappender and TTCC layout for the Logger object and outputs it to System.out.

With this code, run again, and you'll see the log messages we want to see:

As simple as this example, in fact we have demonstrated the use of log4j, to add log4j to your application, only a few steps:
1, import the log4j package;
2, complete the log4j configuration. In the above application, we only use the simplest and most inflexible basicconfigurator to complete the configuration, we will gradually see a more flexible configuration, but the configuration always requires a step.
3, for each class that requires log output, a logger that belongs to this class is obtained through the Logger.getlogger method.

Log4j Learning "Two" example of the first log

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.