(Transfer) org.apache.commons.logging usage

xiaoxiao2021-03-05  23

Commons-logging.properties This file is as follows. Org.apache.commons.logging.log = org.apache.commons.logging.Impl.log4jcategoryLog then with a log4j.properties file. Example is as follows: log4j.rootLogger = DEBUG, A_defaultlog4j.appender.A_default = org.apache.log4j.RollingFileAppenderlog4j.appender.A_default.File = applicationName.loglog4j.appender.A_default.MaxFileSize = 4000KBlog4j.appender.A_default.MaxBackupIndex = 10log4j.appender .A_default.Layout = org.apache.log4j.patternlayoutlog4j.appender.a_default.Layout.conversionPattern =% D {ISO8601} -% P -% M% N

These 2 files are packed under Web-INF / CLASSES. Copy Commons-Logging.jar and Log4j-1.2.4.jar to WEB-INF / LIB. Ok!

Add the following: Package StrutsDemo;

Import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory;

Public class debuge {public static void debug (string classname, string debugstr) {log log = logfactory.getlog (classname); if (log.IndebugeNabled ()) {log.debug (debugstr);}}

Public Static Void Info (String ClassName, String Infostr) {log log = logfactory.getlog (ClassName); if (log.isinfoenabled ()) {log.info (infostr);}}

Public Static Void Warn (String Warnstr) {log log = logfactory.getlog (classname); if (log.iswarnenabled ()) {log.warn (warnStr);}}

Public Static Void Error (String ClassName, String Infostr) {log log = logfactory.getlog (classname); if (log.iserrorenabled ()) {log.error (infstr);}}}

You can call anywhere in the following way:

................................ .debuger.debug (this.getclass (). Getname (), "debug information!"); Debuger.info (this.getClass (). GetName (), "info information ! "); Debuger.warn (this.getclass (). Getname ()," warn information! "); Debuger.error (this.getClass (). Getname ()," error information! "); ............ ..................... So your log will record the file ApplicationName.log. This file is a directory where you launched the app server. You can change ApplicationName.log to your favorite file name in log4j.properties. Basic use method log4j consists of three important components: the priority of log information, output destinations of log information, and output format of log information. The priority of the log information is from high to Error, Warn, INFO, DEBUG, and is used to specify the importance of this log information. The output destination of log information specifies the log to print to the console or in the file; The format controls the display of log information. Defining the profile actually you can also configure a log4j environment in your code. However, using the profile will make your application more flexible. Log4j supports two configuration file formats, one is a file in XML format, one is a Java feature file (key = value). Below we introduce the method of using the Java feature file as a configuration file: 1. Configure the root Logger, whose syntax is: log4j.rootlogger = [level], appendername, appendername, ... where Level is the priority of logging, divided into OFF , Fatal, Error, Warn, INFO, DEBUG, ALL, or the level you define. Log4J is recommended to use only four levels, priority from high to low, Error, Warn, Info, Debug. By the level defined here, you can control the switch to the corresponding level of log information in the application. For example, the INFO level is defined here, and all the DEBUG level log information in the application will not be printed. Appendername is where the specified log information is output to which place. You can specify multiple output destinations at the same time.

2. Configure the log information Output Destination Appender, whose syntax is 3.Log4j.quaLified.Name.OF.Appender.class4.log4j.Appender.Appendername.Option1 = value15. ... log4j.appender.Appendername. Option = valuen, 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 specified, org.apache.log4j.writraPpender (send log information to any specified place in the flow format) 6. Configure log information format (layout), its syntax is:. 7.log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class8.log4j.appender.appenderName.layout.option1 = value19 ... log4j.appender.appenderName . Keyout.Option = Valuen, where Layout provided by log4j has the following: org.apache.log4j.htmlLayout (layout in HTML form), org.apache.log4j.patternlayout (flexibly specified layout mode), org. Apache.log4j.sImpleLayout (Level and Information string containing log information), org.apache.log4j.ttccLayout (including information, thread, category, etc.) ------------ -------------------------------------------------- ---------------------- ZXL19790710 (Snow Dragon) () Reputation: 101 2003-11-20 09: 09: 52Z

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

New Post(0)