1 package com.lutris.logging; 2 3 import org.apache.log4j.Level; 4 import org.apache.log4j.Logger; 5 6 10 public class LogAdapter implements LogChannel { 11 private static Logger logger = null; 12 private static final int INT_ERROR = -1; 13 public LogAdapter() {} 14 15 public LogAdapter(org.apache.log4j.Logger logger) { 16 this.logger = logger; 17 } 18 19 public void setLogger(org.apache.log4j.Logger log) { 20 logger = log; 21 } 22 23 public org.apache.log4j.Logger getLogger() { 24 return logger; 25 } 26 27 public int getLevel(String level) { 28 if (logger != null) { 29 if (logger.getLevel() != null) { 30 return logger.getLevel().toInt(); 31 } 32 } 33 return INT_ERROR; 34 } 35 36 39 public boolean isEnabled(int level) { 40 return logger.isEnabledFor(intToLevel(level)); 41 } 42 43 46 public boolean isEnabled(String level) { 47 return false; 48 } 49 50 53 public void write(int level, String msg) { 54 Level lev; 55 56 if (logger == null) { 57 logger = Logger.getLogger(LogAdapter.class.getName()); 58 } 59 lev = intToLevel(level); 60 if (lev == null) { 61 lev = Level.INFO; 62 } 63 logger.log(lev, msg); 64 } 65 66 69 public void write(int level, String msg, Throwable throwable) { 70 Level lev; 71 72 if (logger == null) { 73 logger = Logger.getLogger(LogAdapter.class.getName()); 74 } 75 lev = intToLevel(level); 76 if (lev == null) { 77 lev = Level.INFO; 78 } 79 logger.log(lev, msg, throwable); 80 } 81 82 85 public void write(String level, String msg) { 86 Level lev; 87 88 if (logger == null) { 89 logger = Logger.getLogger(LogAdapter.class.getName()); 90 } 91 lev = stringToLevel(level); 92 if (lev == null) { 93 lev = Level.INFO; 94 } 95 logger.log(lev, msg); 96 } 97 98 101 public void write(String level, String msg, Throwable throwable) { 102 Level lev; 103 104 if (logger == null) { 105 logger = Logger.getLogger(LogAdapter.class.getName()); 106 } 107 lev = stringToLevel(level); 108 if (lev == null) { 109 lev = Level.INFO; 110 } 111 logger.log(lev, msg); 112 } 113 114 private Level intToLevel(int level) { 115 Level lev; 116 117 switch (level) { 118 case com.lutris.logging.Logger.EMERGENCY: 119 case com.lutris.logging.Logger.ALERT: 120 lev = Level.FATAL; 121 break; 122 123 case com.lutris.logging.Logger.CRITICAL: 124 case com.lutris.logging.Logger.ERROR: 125 lev = Level.ERROR; 126 break; 127 128 case com.lutris.logging.Logger.WARNING: 129 lev = Level.WARN; 130 break; 131 132 case com.lutris.logging.Logger.NOTICE: 133 case com.lutris.logging.Logger.INFO: 134 lev = Level.INFO; 135 break; 136 137 case com.lutris.logging.Logger.DEBUG: 138 case com.lutris.logging.Logger.DEBUG1: 139 case com.lutris.logging.Logger.DEBUG2: 140 case com.lutris.logging.Logger.DEBUG3: 141 case com.lutris.logging.Logger.DEBUG4: 142 case com.lutris.logging.Logger.DEBUG5: 143 case com.lutris.logging.Logger.DEBUG6: 144 case com.lutris.logging.Logger.DEBUG7: 145 case com.lutris.logging.Logger.DEBUG8: 146 case com.lutris.logging.Logger.DEBUG9: 147 case com.lutris.logging.Logger.DEBUGTMP: 148 lev = Level.DEBUG; 149 break; 150 151 default: 152 lev = null; 153 } return lev; 155 } 156 157 private Level stringToLevel(String level) { 158 Level lev; 159 160 if (level.equalsIgnoreCase("EMERGENCY") 161 || level.equalsIgnoreCase("ALERT")) { 162 lev = Level.FATAL; 163 } else if (level.equalsIgnoreCase("CRITICAL") 164 || level.equalsIgnoreCase("ERROR")) { 165 lev = Level.ERROR; 166 } else if (level.equalsIgnoreCase("WARNING")) { 167 lev = Level.WARN; 168 } else if (level.equalsIgnoreCase("NOTICE") 169 || level.equalsIgnoreCase("INFO")) { 170 lev = Level.INFO; 171 } else if (level.equalsIgnoreCase("DEBUG") 172 || level.equalsIgnoreCase("DEBUG1") 173 || level.equalsIgnoreCase("DEBUG2") 174 || level.equalsIgnoreCase("DEBUG3") 175 || level.equalsIgnoreCase("DEBUG4") 176 || level.equalsIgnoreCase("DEBUG5") 177 || level.equalsIgnoreCase("DEBUG6") 178 || level.equalsIgnoreCase("DEBUG7") 179 || level.equalsIgnoreCase("DEBUG8") 180 || level.equalsIgnoreCase("DEBUG9") 181 || level.equalsIgnoreCase("DEBUGTMP")) { 182 lev = Level.DEBUG; 183 } else { lev = null; 185 } 186 return lev; 187 } 188 189 197 public LogWriter getLogWriter(String level) { 198 return null; 200 } 201 202 210 public LogWriter getLogWriter(int level) { 211 return null; 213 } 214 } 215 216 237 | Popular Tags |