Level.java 4.4 KB
Newer Older
武汉红喜's avatar
武汉红喜 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

武汉红喜's avatar
武汉红喜 已提交
18
package org.hongxi.whatsmars.common.logging.inner;
武汉红喜's avatar
武汉红喜 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

import java.io.Serializable;

public class Level implements Serializable {

    transient int level;
    transient String levelStr;
    transient int syslogEquivalent;

    public final static int OFF_INT = Integer.MAX_VALUE;
    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 ALL_INT = Integer.MIN_VALUE;


    private static final String ALL_NAME = "ALL";

    private static final String DEBUG_NAME = "DEBUG";

    private static final String INFO_NAME = "INFO";

    private static final String WARN_NAME = "WARN";

    private static final String ERROR_NAME = "ERROR";

    private static final String OFF_NAME = "OFF";

    final static public Level OFF = new Level(OFF_INT, OFF_NAME, 0);

    final static public Level ERROR = new Level(ERROR_INT, ERROR_NAME, 3);

    final static public Level WARN = new Level(WARN_INT, WARN_NAME, 4);

    final static public Level INFO = new Level(INFO_INT, INFO_NAME, 6);

    final static public Level DEBUG = new Level(DEBUG_INT, DEBUG_NAME, 7);

    final static public Level ALL = new Level(ALL_INT, ALL_NAME, 7);

    static final long serialVersionUID = 3491141966387921974L;

    protected Level(int level, String levelStr, int syslogEquivalent) {
        this.level = level;
        this.levelStr = levelStr;
        this.syslogEquivalent = syslogEquivalent;
    }

    public static Level toLevel(String sArg) {
        return toLevel(sArg, Level.DEBUG);
    }

    public static Level toLevel(int val) {
        return toLevel(val, Level.DEBUG);
    }

    public static Level toLevel(int val, Level defaultLevel) {
        switch (val) {
            case ALL_INT:
                return ALL;
            case DEBUG_INT:
                return Level.DEBUG;
            case INFO_INT:
                return Level.INFO;
            case WARN_INT:
                return Level.WARN;
            case ERROR_INT:
                return Level.ERROR;
            case OFF_INT:
                return OFF;
            default:
                return defaultLevel;
        }
    }

    public static Level toLevel(String sArg, Level defaultLevel) {
        if (sArg == null) {
            return defaultLevel;
        }
        String s = sArg.toUpperCase();

        if (s.equals(ALL_NAME)) {
            return Level.ALL;
        }
        if (s.equals(DEBUG_NAME)) {
            return Level.DEBUG;
        }
        if (s.equals(INFO_NAME)) {
            return Level.INFO;
        }
        if (s.equals(WARN_NAME)) {
            return Level.WARN;
        }
        if (s.equals(ERROR_NAME)) {
            return Level.ERROR;
        }
        if (s.equals(OFF_NAME)) {
            return Level.OFF;
        }

        if (s.equals(INFO_NAME)) {
            return Level.INFO;
        }
        return defaultLevel;
    }


武汉红喜's avatar
武汉红喜 已提交
127
    @Override
武汉红喜's avatar
武汉红喜 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    public boolean equals(Object o) {
        if (o instanceof Level) {
            Level r = (Level) o;
            return this.level == r.level;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int result = level;
        result = 31 * result + (levelStr != null ? levelStr.hashCode() : 0);
        result = 31 * result + syslogEquivalent;
        return result;
    }

    public boolean isGreaterOrEqual(Level r) {
        return level >= r.level;
    }

武汉红喜's avatar
武汉红喜 已提交
149
    @Override
武汉红喜's avatar
武汉红喜 已提交
150 151 152 153 154 155 156 157 158
    final public String toString() {
        return levelStr;
    }

    public final int toInt() {
        return level;
    }

}