1 20 21 package org.snmp4j.log; 22 23 import org.apache.log4j.*; 24 import java.util.Iterator ; 25 import org.snmp4j.util.EnumerationIterator; 26 27 34 public class Log4jLogAdapter implements LogAdapter, Comparable { 35 36 private Logger logger; 37 38 44 public Log4jLogAdapter(Logger logger) { 45 this.logger = logger; 46 } 47 48 53 public void debug(Object message) { 54 logger.debug(message); 55 } 56 57 62 public void error(Object message) { 63 logger.error(message); 64 } 65 66 72 public void error(Object message, Throwable throwable) { 73 logger.error(message, throwable); 74 } 75 76 81 public void info(Object message) { 82 logger.info(message); 83 } 84 85 91 public boolean isDebugEnabled() { 92 return logger.isDebugEnabled(); 93 } 94 95 101 public boolean isInfoEnabled() { 102 return logger.isInfoEnabled(); 103 } 104 105 111 public boolean isWarnEnabled() { 112 return logger.isEnabledFor(Level.WARN); 113 } 114 115 120 public void warn(Object message) { 121 logger.warn(message); 122 } 123 124 public void fatal(Object message) { 125 logger.fatal(message); 126 } 127 128 public void fatal(Object message, Throwable throwable) { 129 logger.fatal(message, throwable); 130 } 131 132 public void setLogLevel(LogLevel level) { 133 Level l; 134 switch (level.getLevel()) { 135 case LogLevel.LEVEL_OFF: 136 l = Level.OFF; 137 break; 138 case LogLevel.LEVEL_ALL: 139 l = Level.ALL; 140 break; 141 case LogLevel.LEVEL_TRACE: 142 l = Level.DEBUG; 143 break; 144 case LogLevel.LEVEL_DEBUG: 145 l = Level.DEBUG; 146 break; 147 case LogLevel.LEVEL_INFO: 148 l = Level.INFO; 149 break; 150 case LogLevel.LEVEL_WARN: 151 l = Level.WARN; 152 break; 153 case LogLevel.LEVEL_ERROR: 154 l = Level.ERROR; 155 break; 156 case LogLevel.LEVEL_FATAL: 157 l = Level.FATAL; 158 break; 159 default: 160 l = null; 161 } 162 logger.setLevel(l); 163 } 164 165 public Logger getLogger() { 166 return logger; 167 } 168 169 public String getName() { 170 return logger.getName(); 171 } 172 173 public LogLevel getLogLevel() { 174 Level l = logger.getLevel(); 175 return toLogLevel(l); 176 } 177 178 private LogLevel toLogLevel(Level l) { 179 if (l == null) { 180 return LogLevel.NONE; 181 } 182 switch (l.toInt()) { 183 case Level.OFF_INT: 184 return LogLevel.OFF; 185 case Level.ALL_INT: 186 return LogLevel.ALL; 187 case Level.DEBUG_INT: 188 return LogLevel.DEBUG; 189 case Level.INFO_INT: 190 return LogLevel.INFO; 191 case Level.WARN_INT: 192 return LogLevel.WARN; 193 case Level.ERROR_INT: 194 return LogLevel.ERROR; 195 case Level.FATAL_INT: 196 return LogLevel.FATAL; 197 } 198 return LogLevel.DEBUG; 199 } 200 201 public int compareTo(Object o) { 202 return getName().compareTo(((Log4jLogAdapter)o).getName()); 203 } 204 205 public LogLevel getEffectiveLogLevel() { 206 Level l = logger.getEffectiveLevel(); 207 return toLogLevel(l); 208 } 209 210 public Iterator getLogHandler() { 211 return new EnumerationIterator(logger.getAllAppenders()); 212 } 213 } 214 | Popular Tags |