001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.util;
017:
018: /**
019: * An implementation of LoggingOutput that sends stuff to System.out
020: * @author Joe Walker [joe at getahead dot ltd dot uk]
021: */
022: public class SystemOutLoggingOutput implements LoggingOutput {
023: /**
024: * @param base All LoggingOutput must have a constructor like this
025: */
026: public SystemOutLoggingOutput(Class<?> base) {
027: }
028:
029: /* (non-Javadoc)
030: * @see org.directwebremoting.util.LoggingOutput#debug(java.lang.String)
031: */
032: public void debug(String message) {
033: log(LEVEL_DEBUG, message, null);
034: }
035:
036: /* (non-Javadoc)
037: * @see org.directwebremoting.util.LoggingOutput#info(java.lang.String)
038: */
039: public void info(String message) {
040: log(LEVEL_INFO, message, null);
041: }
042:
043: /* (non-Javadoc)
044: * @see org.directwebremoting.util.LoggingOutput#warn(java.lang.String)
045: */
046: public void warn(String message) {
047: log(LEVEL_WARN, message, null);
048: }
049:
050: /* (non-Javadoc)
051: * @see org.directwebremoting.util.LoggingOutput#warn(java.lang.String, java.lang.Throwable)
052: */
053: public void warn(String message, Throwable th) {
054: log(LEVEL_WARN, message, th);
055: }
056:
057: /* (non-Javadoc)
058: * @see org.directwebremoting.util.LoggingOutput#error(java.lang.String)
059: */
060: public void error(String message) {
061: log(LEVEL_ERROR, message, null);
062: }
063:
064: /* (non-Javadoc)
065: * @see org.directwebremoting.util.LoggingOutput#error(java.lang.String, java.lang.Throwable)
066: */
067: public void error(String message, Throwable th) {
068: log(LEVEL_ERROR, message, th);
069: }
070:
071: /* (non-Javadoc)
072: * @see org.directwebremoting.util.LoggingOutput#fatal(java.lang.String)
073: */
074: public void fatal(String message) {
075: log(LEVEL_FATAL, message, null);
076: }
077:
078: /* (non-Javadoc)
079: * @see org.directwebremoting.util.LoggingOutput#fatal(java.lang.String, java.lang.Throwable)
080: */
081: public void fatal(String message, Throwable th) {
082: log(LEVEL_FATAL, message, th);
083: }
084:
085: /**
086: * Internal log implementation.
087: * @param loglevel The level to log at
088: * @param message The (optional) message to log
089: * @param th The (optional) exception
090: */
091: private static void log(int loglevel, String message, Throwable th) {
092: if (loglevel >= level) {
093: if (message != null) {
094: System.out.println(message);
095: }
096:
097: if (th != null) {
098: th.printStackTrace();
099: }
100: }
101: }
102:
103: /**
104: * String version of setLevel.
105: * @param logLevel One of FATAL, ERROR, WARN, INFO, DEBUG
106: */
107: public static void setLevel(String logLevel) {
108: if ("FATAL".equalsIgnoreCase(logLevel)) {
109: setLevel(LEVEL_FATAL);
110: } else if ("ERROR".equalsIgnoreCase(logLevel)) {
111: setLevel(LEVEL_ERROR);
112: } else if ("WARN".equalsIgnoreCase(logLevel)) {
113: setLevel(LEVEL_WARN);
114: } else if ("INFO".equalsIgnoreCase(logLevel)) {
115: setLevel(LEVEL_INFO);
116: } else if ("DEBUG".equalsIgnoreCase(logLevel)) {
117: setLevel(LEVEL_DEBUG);
118: } else {
119: throw new IllegalArgumentException("Unknown log level: "
120: + logLevel);
121: }
122: }
123:
124: /* (non-Javadoc)
125: * @see org.directwebremoting.util.LoggingOutput#isDebugEnabled()
126: */
127: public boolean isDebugEnabled() {
128: return level == LEVEL_DEBUG;
129: }
130:
131: /**
132: * @param level The logging level to set.
133: */
134: public static void setLevel(int level) {
135: SystemOutLoggingOutput.level = level;
136: }
137:
138: /**
139: * @return Returns the logging level.
140: */
141: public static int getLevel() {
142: return level;
143: }
144:
145: /**
146: * What is the current debug level?
147: */
148: private static int level = LEVEL_WARN;
149: }
|