001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.logging;
006:
007: import org.apache.log4j.Level;
008: import org.apache.log4j.Priority;
009:
010: import com.tc.util.Assert;
011:
012: /**
013: * Defines constants for various logging levels
014: *
015: * @author teck
016: */
017: public class LogLevel {
018: static final int LEVEL_DEBUG = 5;
019: static final int LEVEL_INFO = 4;
020: static final int LEVEL_WARN = 3;
021: static final int LEVEL_ERROR = 2;
022: static final int LEVEL_FATAL = 1;
023: static final int LEVEL_OFF = 0;
024:
025: public static final LogLevel DEBUG = new LogLevel(LEVEL_DEBUG);
026: public static final LogLevel INFO = new LogLevel(LEVEL_INFO);
027: public static final LogLevel WARN = new LogLevel(LEVEL_WARN);
028: public static final LogLevel ERROR = new LogLevel(LEVEL_ERROR);
029: public static final LogLevel FATAL = new LogLevel(LEVEL_FATAL);
030: public static final LogLevel OFF = new LogLevel(LEVEL_OFF);
031:
032: public static final String DEBUG_NAME = "DEBUG";
033: public static final String INFO_NAME = "INFO";
034: public static final String WARN_NAME = "WARN";
035: public static final String ERROR_NAME = "ERROR";
036: public static final String FATAL_NAME = "FATAL";
037: public static final String OFF_NAME = "OFF";
038:
039: private final int level;
040:
041: private LogLevel(int level) {
042: this .level = level;
043: }
044:
045: public int getLevel() {
046: return level;
047: }
048:
049: public boolean isInfo() {
050: return level == LEVEL_INFO;
051: }
052:
053: static Level toLog4JLevel(LogLevel level) {
054: if (level == null)
055: return null;
056:
057: switch (level.getLevel()) {
058: case LEVEL_DEBUG:
059: return Level.DEBUG;
060: case LEVEL_INFO:
061: return Level.INFO;
062: case LEVEL_WARN:
063: return Level.WARN;
064: case LEVEL_ERROR:
065: return Level.ERROR;
066: case LEVEL_FATAL:
067: return Level.FATAL;
068: case LEVEL_OFF:
069: return Level.OFF;
070: default:
071: throw Assert
072: .failure("Logic Error: Invalid Level: " + level);
073: }
074: }
075:
076: static LogLevel fromLog4JLevel(Level level) {
077: if (level == null)
078: return null;
079: switch (level.toInt()) {
080: case Priority.DEBUG_INT:
081: return LogLevel.DEBUG;
082: case Priority.INFO_INT:
083: return LogLevel.INFO;
084: case Priority.WARN_INT:
085: return LogLevel.WARN;
086: case Priority.ERROR_INT:
087: return LogLevel.ERROR;
088: case Priority.FATAL_INT:
089: return LogLevel.FATAL;
090: case Priority.OFF_INT:
091: return LogLevel.OFF;
092: default:
093: throw Assert.failure("Unsupported Level" + level);
094: }
095: }
096:
097: public String toString() {
098: switch (getLevel()) {
099: case LEVEL_DEBUG:
100: return DEBUG_NAME;
101: case LEVEL_INFO:
102: return INFO_NAME;
103: case LEVEL_WARN:
104: return WARN_NAME;
105: case LEVEL_ERROR:
106: return ERROR_NAME;
107: case LEVEL_FATAL:
108: return FATAL_NAME;
109: case LEVEL_OFF:
110: return OFF_NAME;
111: default:
112: return "Unknown";
113: }
114: }
115:
116: public static LogLevel valueOf(String v) {
117: if (DEBUG_NAME.equals(v)) {
118: return DEBUG;
119: } else if (INFO_NAME.equals(v)) {
120: return INFO;
121: } else if (WARN_NAME.equals(v)) {
122: return WARN;
123: } else if (ERROR_NAME.equals(v)) {
124: return ERROR;
125: } else if (FATAL_NAME.equals(v)) {
126: return FATAL;
127: } else if (OFF_NAME.equals(v)) {
128: return OFF;
129: } else {
130: return null;
131: }
132: }
133:
134: }
|