Log4j use experience

xiaoxiao2021-03-06  67

Simple log4j Use only the following packages can be imported // import log4j packagesimport org.Apache.log4j.logger;

Then you need to write the following private final static logger log = logger.getlogger (Classname.class); // Note that the class name of the current class is written. This way, you have a log object called log, this log has the following set of use log.info (); // General information log.debug (); // debugging information log.Error (); // error message log.warn (); // Warning message log.fatal (); // Fatal error message is listed above is the so-called log4j output level, log4j is recommended to use 4 levels, which are Error, Warn, Info from top to bottom, Debug, assuming that the level you define is INFO, then Error and Warn's logs can be displayed and the Debug information lower than his low is not displayed. I know that the above content is not enough. If you want to use Log4j, you also need to configure the log4j environment. Because each log can achieve the purpose of the control log output by configuring its properties file, I will give a configuration process that uses the log4j under WebLogic, first give the full information of this configuration file.

# log4j.rootlogger = info, a1, r // This sentence specifies where the log output is INFO, A1, and R represent the log output to wherever log output. Log4j.category.hybl_wshabcm = debug, a1, r // This sentence specifies which package of log specific output, and output location log4j.Appender.a1 = org.apache.log4j.consoleAppender // This specified the log output One position A1 is the console ConsoleAppender / ** where the appender provided by log4j has the following: * org.apache.log4j.consoleAppender, * org.apache.log4j.fileAppender (file), * ORG. Apache.log4j.dailyRollingFileAppender (a log file is generated every day), * org.apache.log4j.rollingFileAppender Generate a new file when the specified size is required, * org.apache.log4j.writerappender (traffic log information Format Send to any specified place) ** / log4j.Appender.a1.Layout = org.apache.log4j.patternlayout // Specify the layout mode of A1

/ ** Among them, Layout provided by LOG4J has the following: × Org.apache.log4j.htmlLayout (layout in HTML form), * org.apache.log4j.patternlayout (you can flexibly specify layout mode), * ORG. Apache.log4j.simplelayout (level and information string containing log information), * org.apache.log4j.ttccLayout (including information, thread, category, etc.) * / log4j.Appender.a1.Layout.conversionPattern =% - D {YYYY-MM-DD HH: MM: SS, SSS} [% C] - [% P]% m% n // Specify log's output format

Log4j.Appender.r = org.apache.log4j.rollingFileAppender // Specify the log4j.rapnder.r.r.maxFileSize = 500kb // / Document size log4j.Appender.r.maxbackupindex = 1 // Backup log4j.Appender.r.Layout = org.apache.log4j.htmllayout // file format is HTML format # log4j.Appender.R.Layout = Org .apache.log4j.patternLayout log4j.appender.r.Layout.conversionPattern =% D {YYYY-MM-DD HH: mm: SS, SSS} [% T] [% C] [% P] -% M% N The above file we have to load this file through a class when WebLogic is started, you can use the following method 1, load the Servlet for the configuration file.

Package hybl_wshabcm.servelet;

import java.io.File; import java.io.LineNumberReader; import java.io.FileReader; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Vector; import java.sql.Driver; import Java.sql.driverManager;

// Import servlet packagesimport javax.servlet.http.httpservlet; import javax.servlet.servletconfig; import javax.servlet.servletException;

// Import log4j packagesimport org.apache.log4j.logger; import org.apache.log4j.propertyConfigurator; public class setupservlet Extends httpservlet {

Public void init (servletconfig config) throws servletexception {

Super.init (config);

// first Thing to do, is to set up the driver what we might be used // in case of jdbcappendertry {driver d = ("org.gjt.mm.mysql.driver). NewInstance ()); DRIVERMANAGER.REGISTERDRIVER (D); // Load the JDBC driver, you can use} catch (Exception E) {system.err.println (e) when you are ready to record the log to the database;}

// NEXT LOAD UP THE PROPERTIES // Gets information from Web.xml from Web.xml String Props = Config.GetInitParameter ("Props");

IF (PrOPS == Null || Props.Length () == 0 ||! (New File ()). isfile ()) {

System.err.Println ("Error: Cannot Read The Configuration File." "Please check the path of the config init param in Web.xml"); throw new servletexception ();}} public void design; DESTROY ();}}

2. The servlet on the deployment of the web.xml file will load some initialization information when the server is started, so how does this class be loaded when WebLogic starts? Look at an example

Setup

Hybl_wshabcm.servelet.setupservlet

/ / Launch the class loaded

PROPS

/Web-inf/log4j.properties

/ / Deployment of the file

2

// This sentence is very important

Ok, now you can use log4j to record the log like you.

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

New Post(0)