Log4j uses Daquan

xiaoxiao2021-03-05  29

One: Configuration A :) Use log4jstep 1 in Web Application: Configure log4j configuration file ========================= # set root logger level to debug And Its Only appender to a1 # log4j Having five Logger # Fatal0 # error3 # WARN4 # info6 # debug7log4j.rootlogger = debug, A1 # a1 is set to be a consolerappender. # log4j Appender has several layers #consoleAppender output to control Taiwan log4j.appender.a1 = org.apache.log4j.consoleAppender

# A1 uses PatternLayout.log4j.appender.A1.layout = org.apache.log4j.PatternLayout # output format to view specific time log4j javadoc org.apache.log4j.PatternLayout # d .... log4j.appender.A1.layout.ConversionPattern =% - 4R [% T]% -5P% C% X -% M% N

================================ Dairy Log4j.Rootlogger = Debug, Stdout, R

Log4j.appender.stdout = org.apache.log4j.consoleappenderlog4j.Appender.stdout.Layout = org.apache.log4j.patternlayout

# Pattern to output the caller's file name and line number.log4j.Appender.stdout.Layout.conversionPattern =% 5P [% t] (% F:% L) -% M% N # r Output to file log4j.Appender.r = Org.apache.log4j.rollingfileappenderlog4j.Appender.r.file = example.log

Log4j.Appender.r.maxfilesize = 100kb # Keep One Backup filelog4j.Appender.r.maxbackupindex = 1

Log4j.Appender.r.Layout = org.apache.log4j.patternLayoutLog4j.Appender.r.Layout.conversionPattern =% P% T% C -% M% N

Step 2: Write started servlet

Public class initservlet extends httpservlet {

Public void init () {servletContext Sct = getServletContext (); string prefix = Sct.getRealPath ("/"); // log4j profile storage directory system.out.println ("[.... log4j]: the root path : " prefix); System.out.Println (" [.... log4j]: initservlet init start ... "); string file = getInitParameter (" log4j "); // log4j configuration file if (file! = NULL) {PropertyConfigurator.configure (prefix file); // According to the configuration initializes log4j} System.out.Println ("[.... log4j]: initservlet init package ...");

}

Public void doget (httpservletRequest Req, httpservletResponse res) {

}

Public void dopost (httpservletRequest Req, httpservletResponse res) {

}

B: Use log4j in the general IDE because use log4j needs to do some initialization, in which part of this part is initialized to have AppServer boot is loaded. In the IDE, we need to configure yourself before using log4j. The following documents: public final class Lo4jTest {private static String CLASS_NAME = "com.bingo.test.Lo4jTest"; private static Logger log = Logger.getLogger (CLASS_NAME); // pattern Web Application in the following configuration file in the log4j Configured. / / Now we manually configure private static string pattern = "% - 4R [% -t] [% P] [% C] [% x] - [% m]% n"; private static string pattern2 = "% - D {YYYY-MM-DD HH: MM: SS} [% C] - [% P]% M% N "; // Setting Output layer // private static consolerappender consappender = new consolerappender (new pattern2); public Static void main (string [] args) {// Configure log4jbasicconfigurator.configure (consappender); // void configure (); // Void configure (); // Configure log4j provides two ways // The information is relatively simple, the output is not enough to detail // We can use the previous output we want format log.debug ("log4j debug."); Log.error ("log4j error."); Log.info ("log4j INFO. "); Log.fatal (" log4j fataal. ");}} // The output of the output using pattern2 is as follows 2002-08-30 13:49:09 [com.bingo.test.lo4jtest] - [debug] Log4j debug.2002-08-30 13:49:09 [com.bingo.test.lo4jtest] - [Error] log4j error.2002-08-30 13:49:09 [com.bingo.test.lo4jtest] - [ INFO] LOG4J INFO.2002-08-30 13:49:09 [com.bingo.test.lo4jtest] - [Fatal] log4j fat.// If we use the Pattern output as follows: 0 [main] [debug] [COM. Bingo.test.lo4jtest] [] - [log4j debug.] 0 [main] [error] [com.bingo.test.lo4jtest] [] - [log4j error.] 0 [main] [in FO] [com.bingo.test.lo4jtest] [] - [log4j info.] 0 [main] [fatal] [com.bingo.test.lo4jtest] [] - [log4j fatal.] 2: Use log4j in actual use In the process, it is actually very simple.

1 :) Define logs below, using category, log4j1.2 before log4j1.2, using logger instead of private static string class_name = "com.bingo.test.lo4jtest"; // log4j1.2private static logger log = logger.getlogger Class_name); // LO4J1.2 Previous Private Static Category Log = Category.GetInstance (Class_name); // Get a category or logger, there are several ways, according to yourself, 2 :) Write log.debug ("log4j debug. "); log.error (" log4j error. "); log.info (" log4j info. "); log.fatal (" log4j fatal. "); // void debug (Object Message) // log a Message Object ..........................

Three: Precautions 1: Pay attention to different information in specific use Use a different log mode, select one of DEBUG, ERROR, INFO, FATAL, and can be shielded according to the desired portion output 2: During the development process to the console Modify the configuration during the running process to output it to the file. 3: Define LOG Try to use the file name as a parameter, so it is easy to find errors.

转载请注明原文地址:https://www.9cbs.com/read-33698.html

New Post(0)