1 4 package com.tc.logging; 5 6 import org.apache.log4j.Logger; 7 8 17 class TCLoggerImpl implements TCLogger { 18 19 private final Logger logger; 20 21 TCLoggerImpl(String name) { 22 if (name == null) { throw new IllegalArgumentException ("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 message) { 31 if (message instanceof Throwable ) { 32 debug("Exception thrown", (Throwable ) message); 33 } else { 34 logger.debug(message); 35 } 36 } 37 38 public void debug(Object message, Throwable t) { 39 logger.debug(message, t); 40 } 41 42 public void error(Object message) { 43 if (message instanceof Throwable ) { 44 error("Exception thrown", (Throwable ) message); 45 } else { 46 logger.error(message); 47 } 48 } 49 50 public void error(Object message, Throwable t) { 51 logger.error(message, t); 52 } 53 54 public void fatal(Object message) { 55 if (message instanceof Throwable ) { 56 fatal("Exception thrown", (Throwable ) message); 57 } else { 58 logger.fatal(message); 59 } 60 } 61 62 public void fatal(Object message, Throwable t) { 63 logger.fatal(message, t); 64 } 65 66 public void info(Object message) { 67 if (message instanceof Throwable ) { 68 info("Exception thrown", (Throwable ) message); 69 } else { 70 logger.info(message); 71 } 72 } 73 74 public void info(Object message, Throwable t) { 75 logger.info(message, t); 76 } 77 78 public void warn(Object message) { 79 if (message instanceof Throwable ) { 80 warn("Exception thrown", (Throwable ) message); 81 } else { 82 logger.warn(message); 83 } 84 } 85 86 public void warn(Object message, Throwable 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 message) { 99 if (message instanceof Throwable ) { 100 log(level, "Exception thrown", (Throwable ) message); 101 } 102 103 _log(level, message, null, false); 104 } 105 106 public void log(LogLevel level, Object message, Throwable t) { 107 _log(level, message, t, true); 108 } 109 110 private void _log(LogLevel level, Object message, Throwable 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 getName() { 168 return logger.getName(); 169 } 170 } 171 | Popular Tags |