KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > log > LogProxy


1 /*_############################################################################
2   _##
3   _## SNMP4J - LogProxy.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21 package org.snmp4j.log;
22
23 import java.util.*;
24
25 /**
26  * A <code>LogProxy</code> can be used for late binding loggers. A proxy
27  * does not log anything, if its logger member is <code>null</code>.
28  * Otherwise it forwards all its messages to that logger. Even if the
29  * proxy logger is not set, the <code>LogProxy</code> has a name.
30  *
31  * @author Frank Fock
32  * @version 1.8
33  */

34 public class LogProxy implements LogAdapter {
35
36   private String JavaDoc name;
37   private LogAdapter logger;
38
39   public LogProxy(String JavaDoc name) {
40     this.name = name;
41   }
42
43   public LogProxy(LogAdapter logger) {
44     this.logger = logger;
45   }
46
47   public void debug(Object JavaDoc message) {
48     if (logger != null) {
49       logger.debug(message);
50     }
51   }
52
53   public void error(Object JavaDoc message) {
54     if (logger != null) {
55       logger.error(message);
56     }
57   }
58
59   public void error(Object JavaDoc message, Throwable JavaDoc throwable) {
60     if (logger != null) {
61       logger.error(message, throwable);
62     }
63   }
64
65   public void fatal(Object JavaDoc message) {
66     if (logger != null) {
67       logger.fatal(message);
68     }
69   }
70
71   public void fatal(Object JavaDoc message, Throwable JavaDoc throwable) {
72     if (logger != null) {
73       logger.fatal(message, throwable);
74     }
75   }
76
77   public LogLevel getEffectiveLogLevel() {
78     if (logger != null) {
79       return logger.getEffectiveLogLevel();
80     }
81     return LogLevel.OFF;
82   }
83
84   public Iterator getLogHandler() {
85     if (logger != null) {
86       return logger.getLogHandler();
87     }
88     return Collections.EMPTY_LIST.iterator();
89   }
90
91   public LogLevel getLogLevel() {
92     if (logger != null) {
93       return logger.getLogLevel();
94     }
95     return LogLevel.OFF;
96   }
97
98   public String JavaDoc getName() {
99     if (logger != null) {
100       logger.getName();
101     }
102     return name;
103   }
104
105   public void info(Object JavaDoc message) {
106     if (logger != null) {
107       logger.info(message);
108     }
109   }
110
111   public boolean isDebugEnabled() {
112     if (logger != null) {
113       return logger.isDebugEnabled();
114     }
115     return false;
116   }
117
118   public boolean isInfoEnabled() {
119     if (logger != null) {
120       return logger.isInfoEnabled();
121     }
122     return false;
123   }
124
125   public boolean isWarnEnabled() {
126     if (logger != null) {
127       return logger.isWarnEnabled();
128     }
129     return false;
130   }
131
132   public void setLogLevel(LogLevel level) {
133     if (logger != null) {
134       logger.setLogLevel(level);
135     }
136   }
137
138   public void warn(Object JavaDoc message) {
139     if (logger != null) {
140       logger.warn(message);
141     }
142   }
143
144   /**
145    * Gets the proxied logger.
146    * @return
147    * a LogAdapter the actually logs the messages.
148    */

149   public LogAdapter getLogger() {
150     return logger;
151   }
152
153   /**
154    * Sets the logger that logs the log messages logged with this proxy.
155    * @param logger
156    * a LogAdapter.
157    */

158   public void setLogger(LogAdapter logger) {
159     this.logger = logger;
160   }
161 }
162
Popular Tags