001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.log4j;
018
019/**
020 * <font color="#AA4444">Refrain from using this class directly, use
021 * the {@link Level} class instead</font>.
022 */
023public class Priority {
024
025    /**
026     * The <code>OFF</code> has the highest possible rank and is
027     * intended to turn off logging.
028     */
029    public static final int OFF_INT = Integer.MAX_VALUE;
030    /**
031     * The <code>FATAL</code> level designates very severe error
032     * events that will presumably lead the application to abort.
033     */
034    public static final int FATAL_INT = 50000;
035    /**
036     * The <code>ERROR</code> level designates error events that
037     * might still allow the application to continue running.
038     */
039    public static final int ERROR_INT = 40000;
040    /**
041     * The <code>WARN</code> level designates potentially harmful situations.
042     */
043    public static final int WARN_INT = 30000;
044    /**
045     * The <code>INFO</code> level designates informational messages
046     * that highlight the progress of the application at coarse-grained
047     * level.
048     */
049    public static final int INFO_INT = 20000;
050    /**
051     * The <code>DEBUG</code> Level designates fine-grained
052     * informational events that are most useful to debug an
053     * application.
054     */
055    public static final int DEBUG_INT = 10000;
056    //public final static int FINE_INT = DEBUG_INT;
057    /**
058     * The <code>ALL</code> has the lowest possible rank and is intended to
059     * turn on all logging.
060     */
061    public static final int ALL_INT = Integer.MIN_VALUE;
062
063    /**
064     * @deprecated Use {@link Level#FATAL} instead.
065     */
066    @Deprecated
067    public static final Priority FATAL = new Level(FATAL_INT, "FATAL", 0);
068
069    /**
070     * @deprecated Use {@link Level#ERROR} instead.
071     */
072    @Deprecated
073    public static final Priority ERROR = new Level(ERROR_INT, "ERROR", 3);
074
075    /**
076     * @deprecated Use {@link Level#WARN} instead.
077     */
078    @Deprecated
079    public static final Priority WARN = new Level(WARN_INT, "WARN", 4);
080
081    /**
082     * @deprecated Use {@link Level#INFO} instead.
083     */
084    @Deprecated
085    public static final Priority INFO = new Level(INFO_INT, "INFO", 6);
086
087    /**
088     * @deprecated Use {@link Level#DEBUG} instead.
089     */
090    @Deprecated
091    public static final Priority DEBUG = new Level(DEBUG_INT, "DEBUG", 7);
092
093    /*
094     * These variables should be private but were not in Log4j 1.2 so are left the same way here.
095     */
096    transient int level;
097    transient String levelStr;
098    transient int syslogEquivalent;
099
100    /**
101     * Default constructor for deserialization.
102     */
103    protected Priority() {
104        level = DEBUG_INT;
105        levelStr = "DEBUG";
106        syslogEquivalent = 7;
107    }
108
109    /**
110     * Instantiate a level object.
111     * @param level The level value.
112     * @param levelStr The level name.
113     * @param syslogEquivalent The equivalent syslog value.
114     */
115    protected Priority(final int level, final String levelStr, final int syslogEquivalent) {
116        this.level = level;
117        this.levelStr = levelStr;
118        this.syslogEquivalent = syslogEquivalent;
119    }
120
121    /**
122     * Two priorities are equal if their level fields are equal.
123     * @param o The Object to check.
124     * @return true if the objects are equal, false otherwise.
125     *
126     * @since 1.2
127     */
128    @Override
129    public boolean equals(final Object o) {
130        if (o instanceof Priority) {
131            final Priority r = (Priority) o;
132            return this.level == r.level;
133        }
134        return false;
135    }
136
137    @Override
138    public int hashCode() {
139        return this.level;
140    }
141
142    /**
143     * Returns the syslog equivalent of this priority as an integer.
144     * @return The equivalent syslog value.
145     */
146    public
147    final int getSyslogEquivalent() {
148        return syslogEquivalent;
149    }
150
151
152    /**
153     * Returns {@code true} if this level has a higher or equal
154     * level than the level passed as argument, {@code false}
155     * otherwise.
156     * <p/>
157     * <p>You should think twice before overriding the default
158     * implementation of <code>isGreaterOrEqual</code> method.
159     * @param r The Priority to check.
160     * @return true if the current level is greater or equal to the specified Priority.
161     */
162    public boolean isGreaterOrEqual(final Priority r) {
163        return level >= r.level;
164    }
165
166    /**
167     * Returns all possible priorities as an array of Level objects in
168     * descending order.
169     * @return An array of all possible Priorities.
170     *
171     * @deprecated This method will be removed with no replacement.
172     */
173    @Deprecated
174    public static Priority[] getAllPossiblePriorities() {
175        return new Priority[]{Priority.FATAL, Priority.ERROR, Level.WARN,
176            Priority.INFO, Priority.DEBUG};
177    }
178
179
180    /**
181     * Returns the string representation of this priority.
182     * @return The name of the Priority.
183     */
184    @Override
185    public final String toString() {
186        return levelStr;
187    }
188
189    /**
190     * Returns the integer representation of this level.
191     * @return The integer value of this level.
192     */
193    public final int toInt() {
194        return level;
195    }
196
197    /**
198     * @param sArg The name of the Priority.
199     * @return The Priority matching the name.
200     * @deprecated Please use the {@link Level#toLevel(String)} method instead.
201     */
202    @Deprecated
203    public static Priority toPriority(final String sArg) {
204        return Level.toLevel(sArg);
205    }
206
207    /**
208     * @param val The value of the Priority.
209     * @return The Priority matching the value.
210     * @deprecated Please use the {@link Level#toLevel(int)} method instead.
211     */
212    @Deprecated
213    public static Priority toPriority(final int val) {
214        return toPriority(val, Priority.DEBUG);
215    }
216
217    /**
218     * @param val The value of the Priority.
219     * @param defaultPriority The default Priority to use if the value is invalid.
220     * @return The Priority matching the value or the default Priority if no match is found.
221     * @deprecated Please use the {@link Level#toLevel(int, Level)} method instead.
222     */
223    @Deprecated
224    public static Priority toPriority(final int val, final Priority defaultPriority) {
225        return Level.toLevel(val, (Level) defaultPriority);
226    }
227
228    /**
229     * @param sArg The name of the Priority.
230     * @param defaultPriority The default Priority to use if the name is not found.
231     * @return The Priority matching the name or the default Priority if no match is found.
232     * @deprecated Please use the {@link Level#toLevel(String, Level)} method instead.
233     */
234    @Deprecated
235    public static Priority toPriority(final String sArg, final Priority defaultPriority) {
236        return Level.toLevel(sArg, (Level) defaultPriority);
237    }
238}