KickJava   Java API By Example, From Geeks To Geeks.

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


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.Logger;
7
8 /**
9  * An implementation of TCLogger that just delegates to a log4j Logger instance NOTE: This implementation differs from
10  * log4j in at least one detail....When calling the various log methods (info, warn, etc..) that take a single
11  * <code>Object</code> parameter (eg. <code>debug(Object message)</code>), if an instance of <code>Throwable</code>
12  * is passed as the message paramater, the call will be translated to the <code>xxx(Object Message, Throwable t)</code>
13  * signature
14  *
15  * @author teck
16  */

17 class TCLoggerImpl implements TCLogger {
18
19   private final Logger logger;
20
21   TCLoggerImpl(String JavaDoc name) {
22     if (name == null) { throw new IllegalArgumentException JavaDoc("Logger name cannot be null"); }
23     logger = Logger.getLogger(name);
24   }
25
26   Logger getLogger() {
27     return logger;
28   }
29
30   public void debug(Object JavaDoc message) {
31     if (message instanceof Throwable JavaDoc) {
32       debug("Exception thrown", (Throwable JavaDoc) message);
33     } else {
34       logger.debug(message);
35     }
36   }
37
38   public void debug(Object JavaDoc message, Throwable JavaDoc t) {
39     logger.debug(message, t);
40   }
41
42   public void error(Object JavaDoc message) {
43     if (message instanceof Throwable JavaDoc) {
44       error("Exception thrown", (Throwable JavaDoc) message);
45     } else {
46       logger.error(message);
47     }
48   }
49
50   public void error(Object JavaDoc message, Throwable JavaDoc t) {
51     logger.error(message, t);
52   }
53
54   public void fatal(Object JavaDoc message) {
55     if (message instanceof Throwable JavaDoc) {
56       fatal("Exception thrown", (Throwable JavaDoc) message);
57     } else {
58       logger.fatal(message);
59     }
60   }
61
62   public void fatal(Object JavaDoc message, Throwable JavaDoc t) {
63     logger.fatal(message, t);
64   }
65
66   public void info(Object JavaDoc message) {
67     if (message instanceof Throwable JavaDoc) {
68       info("Exception thrown", (Throwable JavaDoc) message);
69     } else {
70       logger.info(message);
71     }
72   }
73
74   public void info(Object JavaDoc message, Throwable JavaDoc t) {
75     logger.info(message, t);
76   }
77
78   public void warn(Object JavaDoc message) {
79     if (message instanceof Throwable JavaDoc) {
80       warn("Exception thrown", (Throwable JavaDoc) message);
81     } else {
82       logger.warn(message);
83     }
84   }
85
86   public void warn(Object JavaDoc message, Throwable JavaDoc t) {
87     logger.warn(message, t);
88   }
89
90   public boolean isDebugEnabled() {
91     return logger.isDebugEnabled();
92   }
93
94   public boolean isInfoEnabled() {
95     return logger.isInfoEnabled();
96   }
97
98   public void log(LogLevel level, Object JavaDoc message) {
99     if (message instanceof Throwable JavaDoc) {
100       log(level, "Exception thrown", (Throwable JavaDoc) message);
101     }
102
103     _log(level, message, null, false);
104   }
105
106   public void log(LogLevel level, Object JavaDoc message, Throwable JavaDoc t) {
107     _log(level, message, t, true);
108   }
109
110   private void _log(LogLevel level, Object JavaDoc message, Throwable JavaDoc t, boolean withException) {
111     switch (level.getLevel()) {
112       case LogLevel.LEVEL_DEBUG: {
113         if (withException) {
114           debug(message, t);
115         } else {
116           debug(message);
117         }
118         break;
119       }
120       case LogLevel.LEVEL_INFO: {
121         if (withException) {
122           info(message, t);
123         } else {
124           info(message);
125         }
126         break;
127       }
128       case LogLevel.LEVEL_WARN: {
129         if (withException) {
130           warn(message, t);
131         } else {
132           warn(message);
133         }
134         break;
135       }
136       case LogLevel.LEVEL_ERROR: {
137         if (withException) {
138           error(message, t);
139         } else {
140           error(message);
141         }
142         break;
143       }
144       case LogLevel.LEVEL_FATAL: {
145         if (withException) {
146           fatal(message, t);
147         } else {
148           fatal(message);
149         }
150         break;
151       }
152       default: {
153         logger.error("Unknown log level: " + level.getLevel());
154         break;
155       }
156     }
157   }
158
159   public void setLevel(LogLevel level) {
160     logger.setLevel(LogLevel.toLog4JLevel(level));
161   }
162
163   public LogLevel getLevel() {
164     return LogLevel.fromLog4JLevel(logger.getLevel());
165   }
166
167   public String JavaDoc getName() {
168     return logger.getName();
169   }
170 }
171
Popular Tags