001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.logging;
005:
006: import org.apache.log4j.Logger;
007:
008: /**
009: * An implementation of TCLogger that just delegates to a log4j Logger instance NOTE: This implementation differs from
010: * log4j in at least one detail....When calling the various log methods (info, warn, etc..) that take a single
011: * <code>Object</code> parameter (eg. <code>debug(Object message)</code>), if an instance of <code>Throwable</code>
012: * is passed as the message paramater, the call will be translated to the <code>xxx(Object Message, Throwable t)</code>
013: * signature
014: *
015: * @author teck
016: */
017: class TCLoggerImpl implements TCLogger {
018:
019: private final Logger logger;
020:
021: TCLoggerImpl(String name) {
022: if (name == null) {
023: throw new IllegalArgumentException(
024: "Logger name cannot be null");
025: }
026: logger = Logger.getLogger(name);
027: }
028:
029: Logger getLogger() {
030: return logger;
031: }
032:
033: public void debug(Object message) {
034: if (message instanceof Throwable) {
035: debug("Exception thrown", (Throwable) message);
036: } else {
037: logger.debug(message);
038: }
039: }
040:
041: public void debug(Object message, Throwable t) {
042: logger.debug(message, t);
043: }
044:
045: public void error(Object message) {
046: if (message instanceof Throwable) {
047: error("Exception thrown", (Throwable) message);
048: } else {
049: logger.error(message);
050: }
051: }
052:
053: public void error(Object message, Throwable t) {
054: logger.error(message, t);
055: }
056:
057: public void fatal(Object message) {
058: if (message instanceof Throwable) {
059: fatal("Exception thrown", (Throwable) message);
060: } else {
061: logger.fatal(message);
062: }
063: }
064:
065: public void fatal(Object message, Throwable t) {
066: logger.fatal(message, t);
067: }
068:
069: public void info(Object message) {
070: if (message instanceof Throwable) {
071: info("Exception thrown", (Throwable) message);
072: } else {
073: logger.info(message);
074: }
075: }
076:
077: public void info(Object message, Throwable t) {
078: logger.info(message, t);
079: }
080:
081: public void warn(Object message) {
082: if (message instanceof Throwable) {
083: warn("Exception thrown", (Throwable) message);
084: } else {
085: logger.warn(message);
086: }
087: }
088:
089: public void warn(Object message, Throwable t) {
090: logger.warn(message, t);
091: }
092:
093: public boolean isDebugEnabled() {
094: return logger.isDebugEnabled();
095: }
096:
097: public boolean isInfoEnabled() {
098: return logger.isInfoEnabled();
099: }
100:
101: public void log(LogLevel level, Object message) {
102: if (message instanceof Throwable) {
103: log(level, "Exception thrown", (Throwable) message);
104: }
105:
106: _log(level, message, null, false);
107: }
108:
109: public void log(LogLevel level, Object message, Throwable t) {
110: _log(level, message, t, true);
111: }
112:
113: private void _log(LogLevel level, Object message, Throwable t,
114: boolean withException) {
115: switch (level.getLevel()) {
116: case LogLevel.LEVEL_DEBUG: {
117: if (withException) {
118: debug(message, t);
119: } else {
120: debug(message);
121: }
122: break;
123: }
124: case LogLevel.LEVEL_INFO: {
125: if (withException) {
126: info(message, t);
127: } else {
128: info(message);
129: }
130: break;
131: }
132: case LogLevel.LEVEL_WARN: {
133: if (withException) {
134: warn(message, t);
135: } else {
136: warn(message);
137: }
138: break;
139: }
140: case LogLevel.LEVEL_ERROR: {
141: if (withException) {
142: error(message, t);
143: } else {
144: error(message);
145: }
146: break;
147: }
148: case LogLevel.LEVEL_FATAL: {
149: if (withException) {
150: fatal(message, t);
151: } else {
152: fatal(message);
153: }
154: break;
155: }
156: default: {
157: logger.error("Unknown log level: " + level.getLevel());
158: break;
159: }
160: }
161: }
162:
163: public void setLevel(LogLevel level) {
164: logger.setLevel(LogLevel.toLog4JLevel(level));
165: }
166:
167: public LogLevel getLevel() {
168: return LogLevel.fromLog4JLevel(logger.getLevel());
169: }
170:
171: public String getName() {
172: return logger.getName();
173: }
174: }
|