KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > logging > LogLevel


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.logging;
5
6 import org.apache.log4j.Level;
7 import org.apache.log4j.Priority;
8
9 import com.tc.util.Assert;
10
11 /**
12  * Defines constants for various logging levels
13  *
14  * @author teck
15  */

16 public class LogLevel {
17   static final int LEVEL_DEBUG = 4;
18   static final int LEVEL_INFO = 3;
19   static final int LEVEL_WARN = 2;
20   static final int LEVEL_ERROR = 1;
21   static final int LEVEL_FATAL = 0;
22
23   public static final LogLevel DEBUG = new LogLevel(LEVEL_DEBUG);
24   public static final LogLevel INFO = new LogLevel(LEVEL_INFO);
25   public static final LogLevel WARN = new LogLevel(LEVEL_WARN);
26   public static final LogLevel ERROR = new LogLevel(LEVEL_ERROR);
27   public static final LogLevel FATAL = new LogLevel(LEVEL_FATAL);
28
29   public static final String JavaDoc DEBUG_NAME = "DEBUG";
30   public static final String JavaDoc INFO_NAME = "INFO" ;
31   public static final String JavaDoc WARN_NAME = "WARN" ;
32   public static final String JavaDoc ERROR_NAME = "ERROR";
33   public static final String JavaDoc FATAL_NAME = "FATAL";
34   
35   private final int level;
36
37   private LogLevel(int level) {
38     this.level = level;
39   }
40
41   public int getLevel() {
42     return level;
43   }
44   
45   public boolean isInfo() {
46     return level == LEVEL_INFO;
47   }
48
49   static Level toLog4JLevel(LogLevel level) {
50     if (level == null) return null;
51     
52     switch( level.getLevel() ) {
53       case LEVEL_DEBUG: return Level.DEBUG;
54       case LEVEL_INFO : return Level.INFO;
55       case LEVEL_WARN : return Level.WARN;
56       case LEVEL_ERROR: return Level.ERROR;
57       case LEVEL_FATAL: return Level.FATAL;
58       default : throw Assert.failure( "Logic Error: Invalid Level: " + level);
59     }
60   }
61   
62   static LogLevel fromLog4JLevel(Level level) {
63     if (level == null) return null;
64     switch( level.toInt()) {
65       case Priority.DEBUG_INT: return LogLevel.DEBUG;
66       case Priority.INFO_INT : return LogLevel.INFO;
67       case Priority.WARN_INT : return LogLevel.WARN;
68       case Priority.ERROR_INT: return LogLevel.ERROR;
69       case Priority.FATAL_INT: return LogLevel.FATAL;
70       default : throw Assert.failure("Unsupported Level" + level );
71     }
72   }
73
74   public String JavaDoc toString() {
75       switch( getLevel() ) {
76         case LEVEL_DEBUG: return DEBUG_NAME;
77         case LEVEL_INFO : return INFO_NAME;
78         case LEVEL_WARN : return WARN_NAME;
79         case LEVEL_ERROR: return ERROR_NAME;
80         case LEVEL_FATAL: return FATAL_NAME;
81         default : return "Unknown";
82       }
83     }
84
85   public static LogLevel valueOf(String JavaDoc v) {
86     if (DEBUG_NAME.equals(v)) {
87       return DEBUG;
88     } else if (INFO_NAME.equals(v)) {
89       return INFO;
90     } else if (WARN_NAME.equals(v)) {
91       return WARN;
92     } else if (ERROR_NAME.equals(v)) {
93       return ERROR;
94     } else if (FATAL_NAME.equals(v)) {
95       return FATAL;
96     } else {
97       return null;
98     }
99   }
100   
101
102 }
103
104
Popular Tags