log4j自定义新的级别
生活随笔
收集整理的這篇文章主要介紹了
log4j自定义新的级别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為不用和其他級別沖突,有時候需要自己定義級別,查看源碼,代碼如下:
package org.apache.log4j;/**<font color="#AA4444">Refrain from using this class directly, usethe {@link Level} class instead</font>.@author Ceki Gülcü */ public class Priority {transient int level;transient String levelStr;transient int syslogEquivalent;public final static int OFF_INT = Integer.MAX_VALUE;public final static int FATAL_INT = 50000;public final static int ERROR_INT = 40000;public final static int WARN_INT = 30000;public final static int INFO_INT = 20000;public final static int DEBUG_INT = 10000;//public final static int FINE_INT = DEBUG_INT;public final static int ALL_INT = Integer.MIN_VALUE;通過代碼可以出來Level替代了Priority,那么Level代碼如下:
/**Defines the minimum set of levels recognized by the system, that is<code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>,<code>WARN</code>, <code>INFO</code, <code>DEBUG</code> and<code>ALL</code>.<p>The <code>Level</code> class may be subclassed to define a largerlevel set.@author Ceki Gülcü*/ public class Level extends Priority implements Serializable {/*** TRACE level integer value.* @since 1.2.12*/public static final int TRACE_INT = 5000;/**The <code>OFF</code> has the highest possible rank and isintended to turn off logging. */final static public Level OFF = new Level(OFF_INT, "OFF", 0);/**The <code>FATAL</code> level designates very severe errorevents that will presumably lead the application to abort.*/final static public Level FATAL = new Level(FATAL_INT, "FATAL", 0);/**The <code>ERROR</code> level designates error events thatmight still allow the application to continue running. */final static public Level ERROR = new Level(ERROR_INT, "ERROR", 3);/**The <code>WARN</code> level designates potentially harmful situations.*/final static public Level WARN = new Level(WARN_INT, "WARN", 4);/**The <code>INFO</code> level designates informational messagesthat highlight the progress of the application at coarse-grainedlevel. */final static public Level INFO = new Level(INFO_INT, "INFO", 6);/**The <code>DEBUG</code> Level designates fine-grainedinformational events that are most useful to debug anapplication. */final static public Level DEBUG = new Level(DEBUG_INT, "DEBUG", 7);Category如下: /*** <font color="#AA2222"><b>This class has been deprecated and* replaced by the {@link Logger} <em>subclass</em></b></font>. It* will be kept around to preserve backward compatibility until mid* 2003.* * <p><code>Logger</code> is a subclass of Category, i.e. it extends* Category. In other words, a logger <em>is</em> a category. Thus,* all operations that can be performed on a category can be* performed on a logger. Internally, whenever log4j is asked to* produce a Category object, it will instead produce a Logger* object. Log4j 1.2 will <em>never</em> produce Category objects but* only <code>Logger</code> instances. In order to preserve backward* compatibility, methods that previously accepted category objects* still continue to accept category objects.* * <p>For example, the following are all legal and will work as* expected.* <pre>// Deprecated form:Category cat = Category.getInstance("foo.bar")// Preferred form for retrieving loggers:Logger logger = Logger.getLogger("foo.bar")</pre>* <p>The first form is deprecated and should be avoided.* * <p><b>There is absolutely no need for new client code to use or* refer to the <code>Category</code> class.</b> Whenever possible,* please avoid referring to it or using it.* * <p>See the <a href="../../../../manual.html">short manual</a> for an* introduction on this class.* <p>* See the document entitled <a href="http://www.qos.ch/logging/preparingFor13.html">preparing* for log4j 1.3</a> for a more detailed discussion.** @author Ceki Gülcü* @author Anders Kristensen */ public class Category implements AppenderAttachable {/**The hierarchy where categories are attached to by default.*///static//public//final Hierarchy defaultHierarchy = new Hierarchy(new// RootCategory(Level.DEBUG));/**The name of this category.*/protected String name;/**The assigned level of this category. The<code>level</code> variable need not be assigned a value inwhich case it is inherited form the hierarchy. */volatile protected Level level;/**The parent of this category. All categories have at least oneancestor which is the root category. */volatile protected Category parent;/**The fully qualified name of the Category class. See also thegetFQCN method. */private static final String FQCN = Category.class.getName();protected ResourceBundle resourceBundle;// Categories need to know what Hierarchy they are inprotected LoggerRepository repository;AppenderAttachableImpl aai;/** Additivity is set to true by default, that is children inheritthe appenders of their ancestors by default. If this variable isset to <code>false</code> then the appenders found in theancestors of this category are not used. However, the childrenof this category will inherit its appenders, unless the childrenhave their additivity flag set to <code>false</code> too. Seethe user manual for more details. */protected boolean additive = true;/**This constructor created a new <code>Category</code> instance andsets its name.<p>It is intended to be used by sub-classes only. You should notcreate categories directly.@param name The name of the category.*/protectedCategory(String name) {this.name = name;}/**Add <code>newAppender</code> to the list of appenders of thisCategory instance.<p>If <code>newAppender</code> is already in the list ofappenders, then it won't be added again.*/synchronizedpublicvoid addAppender(Appender newAppender) {if(aai == null) {aai = new AppenderAttachableImpl();}aai.addAppender(newAppender);repository.fireAddAppenderEvent(this, newAppender);}/**If <code>assertion</code> parameter is <code>false</code>, thenlogs <code>msg</code> as an {@link #error(Object) error} statement.<p>The <code>assert</code> method has been renamed to<code>assertLog</code> because <code>assert</code> is a languagereserved word in JDK 1.4.@param assertion@param msg The message to print if <code>assertion</code> isfalse.@since 1.2 */publicvoid assertLog(boolean assertion, String msg) {if(!assertion)this.error(msg);}/**Call the appenders in the hierrachy starting at<code>this</code>. If no appenders could be found, emit awarning.<p>This method calls all the appenders inherited from thehierarchy circumventing any evaluation of whether to log or notto log the particular log request.@param event the event to log. */publicvoid callAppenders(LoggingEvent event) {int writes = 0;for(Category c = this; c != null; c=c.parent) {// Protected against simultaneous call to addAppender, removeAppender,...synchronized(c) {if(c.aai != null) {writes += c.aai.appendLoopOnAppenders(event);}if(!c.additive) {break;}}}if(writes == 0) {repository.emitNoAppenderWarning(this);}}/**Close all attached appenders implementing the AppenderAttachableinterface.@since 1.0*/synchronizedvoid closeNestedAppenders() {Enumeration enumeration = this.getAllAppenders();if(enumeration != null) {while(enumeration.hasMoreElements()) {Appender a = (Appender) enumeration.nextElement();if(a instanceof AppenderAttachable) {a.close();}}}}/**Log a message object with the {@link Level#DEBUG DEBUG} level.<p>This method first checks if this category is <code>DEBUG</code>enabled by comparing the level of this category with the {@linkLevel#DEBUG DEBUG} level. If this category is<code>DEBUG</code> enabled, then it converts the message object(passed as parameter) to a string by invoking the appropriate{@link org.apache.log4j.or.ObjectRenderer}. It then proceeds to call all theregistered appenders in this category and also higher in thehierarchy depending on the value of the additivity flag.<p><b>WARNING</b> Note that passing a {@link Throwable} to thismethod will print the name of the <code>Throwable</code> but nostack trace. To print a stack trace use the {@link #debug(Object,Throwable)} form instead.@param message the message object to log. */publicvoid debug(Object message) {if(repository.isDisabled(Level.DEBUG_INT))return;if(Level.DEBUG.isGreaterOrEqual(this.getEffectiveLevel())) {forcedLog(FQCN, Level.DEBUG, message, null);}}/**Log a message object with the <code>DEBUG</code> level includingthe stack trace of the {@link Throwable} <code>t</code> passed asparameter.<p>See {@link #debug(Object)} form for more detailed information.@param message the message object to log.@param t the exception to log, including its stack trace. */publicvoid debug(Object message, Throwable t) {if(repository.isDisabled(Level.DEBUG_INT))return;if(Level.DEBUG.isGreaterOrEqual(this.getEffectiveLevel()))forcedLog(FQCN, Level.DEBUG, message, t);}/**Log a message object with the {@link Level#ERROR ERROR} Level.<p>This method first checks if this category is <code>ERROR</code>enabled by comparing the level of this category with {@linkLevel#ERROR ERROR} Level. If this category is <code>ERROR</code>enabled, then it converts the message object passed as parameter已經被Logger類替代了,修改新的級別,就找到Error類似的級別 拷貝修改即可。
總結
以上是生活随笔為你收集整理的log4j自定义新的级别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《使用IEC61499为控制系统建模》-
- 下一篇: 操作系统MSXML组件版本过低,导致启动