1 20 21 package org.snmp4j.log; 22 23 import java.util.Arrays ; 24 import java.util.Iterator ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 27 28 import org.snmp4j.log.LogAdapter; 29 import org.snmp4j.log.LogLevel; 30 31 40 public class JavaLogAdapter implements LogAdapter { 41 42 private final Logger logger; 43 44 public JavaLogAdapter(Logger logger) { 45 this.logger = logger; 46 } 47 48 50 public boolean isDebugEnabled() { 51 return isLoggable(LogLevel.DEBUG); 52 } 53 54 public boolean isInfoEnabled() { 55 return isLoggable(LogLevel.INFO); 56 } 57 58 public boolean isWarnEnabled() { 59 return isLoggable(LogLevel.WARN); 60 } 61 62 64 public void debug(Object message) { 65 log(LogLevel.DEBUG, message.toString(), null); 66 } 67 68 public void info(Object message) { 69 log(LogLevel.INFO, message.toString(), null); 70 } 71 72 public void warn(Object message) { 73 log(LogLevel.WARN, message.toString(), null); 74 } 75 76 public void error(Object message) { 77 log(LogLevel.ERROR, message.toString(), null); 78 } 79 80 public void error(Object message, Throwable t) { 81 log(LogLevel.ERROR, message.toString(), t); 82 } 83 84 public void fatal(Object message) { 85 log(LogLevel.FATAL, message.toString(), null); 86 } 87 88 public void fatal(Object message, Throwable t) { 89 log(LogLevel.FATAL, message.toString(), t); 90 } 91 92 94 public LogLevel getEffectiveLogLevel() { 95 return fromJavaToSnmp4jLevel(logger.getLevel()); 96 } 97 98 public Iterator getLogHandler() { 99 return Arrays.asList(logger.getHandlers()).iterator(); 100 } 101 102 public LogLevel getLogLevel() { 103 return getEffectiveLogLevel(); 104 } 105 106 public String getName() { 107 return logger.getName(); 108 } 109 110 public void setLogLevel(LogLevel logLevel) { 111 logger.setLevel(fromSnmp4jToJdk(logLevel)); 112 } 113 114 116 private boolean isLoggable(LogLevel logLevel) { 117 return logger.isLoggable(fromSnmp4jToJdk(logLevel)); 118 } 119 120 private void log(LogLevel logLevel, String msg, Throwable t) { 121 logger.log(fromSnmp4jToJdk(logLevel), msg, t); 122 } 123 124 134 private static Level fromSnmp4jToJdk(LogLevel logLevel) { 135 if (logLevel == null) { 136 return null; 137 } 138 switch (logLevel.getLevel()) { 139 case LogLevel.LEVEL_ALL: 140 return Level.ALL; 141 case LogLevel.LEVEL_DEBUG: 142 return Level.FINE; 143 case LogLevel.LEVEL_TRACE: 144 return Level.FINEST; 145 case LogLevel.LEVEL_INFO: 146 return Level.INFO; 147 case LogLevel.LEVEL_WARN: 148 return Level.WARNING; 149 case LogLevel.LEVEL_ERROR: 150 return Level.SEVERE; 151 case LogLevel.LEVEL_FATAL: 152 return Level.SEVERE; 153 case LogLevel.LEVEL_OFF: 154 return Level.OFF; 155 case LogLevel.LEVEL_NONE: 156 return Level.OFF; 157 default: 158 throw new IllegalArgumentException ( 159 "Mapping not defined from SNMP4J level " + logLevel 160 + " to Java logging level"); 161 } 162 } 163 164 174 private static LogLevel fromJavaToSnmp4jLevel(Level level) { 175 if (level == null) { 176 return null; 177 } 178 else if (Level.ALL.equals(level)) { 179 return LogLevel.ALL; 180 } 181 else if (Level.SEVERE.equals(level)) { 182 return LogLevel.FATAL; 183 } 184 else if (Level.WARNING.equals(level)) { 185 return LogLevel.WARN; 186 } 187 else if (Level.INFO.equals(level)) { 188 return LogLevel.INFO; 189 } 190 else if (Level.CONFIG.equals(level)) { 191 return LogLevel.DEBUG; 192 } 193 else if (Level.FINE.equals(level)) { 194 return LogLevel.DEBUG; 195 } 196 else if (Level.FINER.equals(level)) { 197 return LogLevel.TRACE; 198 } 199 else if (Level.FINEST.equals(level)) { 200 return LogLevel.TRACE; 201 } 202 else if (Level.OFF.equals(level)) { 203 return LogLevel.DEBUG; 204 } 205 else { 206 throw new IllegalArgumentException ("Mapping not defined from Java level " 207 + level.getName() + 208 " to SNMP4J logging level"); 209 } 210 } 211 } 212 | Popular Tags |