log4j Control write log switch in Java

Source: Internet
Author: User

We usually use most of the log is Java.util.logger and log4j.

We use log4j to control this because we want to be flexible externally.

First, we need Log4j.jar and log4j.properties.

Now we write the log output in the program

introduction of import Org.apache.log4j.Logger;

Logger Logger = Logger.getlogger ("Here's the name to take, generally this class name, here is Checkuserservlet");   
The log information to be exported in log4j defines 5 levels, in turn, Debug, INFO, WARN, error, and fatal, when the output, only the level of higher than the level specified in the configuration of the information can be true output, so it is convenient to configure different circumstances to output the content, You do not need to change   
the code logger.info ("Hello,world");

Next, the key.

Configure the Log4j.properties file as follows

Log4j.logger.checkuserservlet=info,a   
# Log4j.logger.CheckUserServlet's Checkuserservlet is the name   
I just took in the program Log4j.appender.a=org.apache.log4j.dailyrollingfileappender   
Log4j.appender.a.file=d:/test.log   
# Here will log output to D disk in the Test.log, specific configuration you see the detailed instructions below, you can also use ${webapp.root} to represent the Tomcat root directory, the specific people try it 
log4j.appender.a.layout= Org.apache.log4j.PatternLayout   
log4j.appender.a.layout.conversionpattern=%d%m%n

Now we can output the log to the Test.log file in D disk.

But what should I do to turn off log writing?

In addition to the above 5 levels, we can also set this:

Log4j.logger.checkuserservlet=info,a

Set the info level here to off so that log writes can be turned off.

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.