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.