1 23 package com.lutris.logging; 24 25 import java.text.SimpleDateFormat ; 26 27 import org.apache.log4j.Level; 28 import org.apache.log4j.Logger; 29 30 42 public class Log4jLogChannel implements LogChannel { 43 44 47 String separatorLine =""; 48 49 private String facility; 50 51 private static final int INT_ERROR = -1; 52 53 56 private static final SimpleDateFormat dateFormatter = new SimpleDateFormat ("yyyy.MM.dd HH:mm:ss"); 57 58 61 private org.apache.log4j.Logger logger; 62 63 70 protected Log4jLogChannel(String chanFacility, 71 org.apache.log4j.Logger loggerObj,String separator) { 72 facility = chanFacility; 73 logger = loggerObj; 74 separatorLine=separator; 75 76 } 77 78 public int getLevel(String level) { 79 if (logger != null) { 80 if (logger.getLevel() != null) { 81 return logger.getLevel().toInt(); 82 } 83 } 84 return INT_ERROR; 85 } 86 87 90 public boolean isEnabled(int level) { 91 return true; 92 } 94 95 98 public boolean isEnabled(String level) { 99 return true; 100 } 102 103 106 public void write(int level, String msg) { 107 Level lev; 108 109 if (logger == null) { 110 logger = Logger.getLogger(LogAdapter.class.getName()); 111 } 112 lev = intToLevel(level); 113 if (lev == null) { 114 lev = Level.INFO; 115 } 116 117 logger.log(lev, msg + separatorLine); 118 } 119 120 123 public void write(int level, String msg, Throwable throwable) { 124 Level lev; 125 126 if (logger == null) { 127 logger = Logger.getLogger(LogAdapter.class.getName()); 128 } 129 lev = intToLevel(level); 130 if (lev == null) { 131 lev = Level.INFO; 132 } 133 logger.log(lev, msg + separatorLine, throwable); 134 } 135 136 139 public void write(String level, String msg) { 140 Level lev; 141 142 if (logger == null) { 143 logger = Logger.getLogger(LogAdapter.class.getName()); 144 } 145 lev = stringToLevel(level); 146 if (lev == null) { 147 lev = Level.INFO; 148 } 149 logger.log(lev, msg + separatorLine); 150 } 151 152 155 public void write(String level, String msg, Throwable throwable) { 156 Level lev; 157 158 if (logger == null) { 159 logger = Logger.getLogger(LogAdapter.class.getName()); 160 } 161 lev = stringToLevel(level); 162 if (lev == null) { 163 lev = Level.INFO; 164 } 165 logger.log(lev, msg + separatorLine, throwable); 166 } 167 168 private Level intToLevel(int level) { 169 Level lev; 170 171 switch (level) { 172 case com.lutris.logging.Logger.EMERGENCY: 173 case com.lutris.logging.Logger.ALERT: 174 lev = Level.FATAL; 175 break; 176 177 case com.lutris.logging.Logger.CRITICAL: 178 case com.lutris.logging.Logger.ERROR: 179 lev = Level.ERROR; 180 break; 181 182 case com.lutris.logging.Logger.WARNING: 183 lev = Level.WARN; 184 break; 185 186 case com.lutris.logging.Logger.NOTICE: 187 case com.lutris.logging.Logger.INFO: 188 lev = Level.INFO; 189 break; 190 191 case com.lutris.logging.Logger.DEBUG: 192 case com.lutris.logging.Logger.DEBUG1: 193 case com.lutris.logging.Logger.DEBUG2: 194 case com.lutris.logging.Logger.DEBUG3: 195 case com.lutris.logging.Logger.DEBUG4: 196 case com.lutris.logging.Logger.DEBUG5: 197 case com.lutris.logging.Logger.DEBUG6: 198 case com.lutris.logging.Logger.DEBUG7: 199 case com.lutris.logging.Logger.DEBUG8: 200 case com.lutris.logging.Logger.DEBUG9: 201 case com.lutris.logging.Logger.DEBUGTMP: 202 lev = Level.DEBUG; 203 break; 204 205 default: 206 lev = Level.DEBUG; 207 } return lev; 209 } 210 211 private Level stringToLevel(String level) { 212 Level lev; 213 214 if (level.equalsIgnoreCase("EMERGENCY") 215 || level.equalsIgnoreCase("ALERT")) { 216 lev = Level.FATAL; 217 } else if (level.equalsIgnoreCase("CRITICAL") 218 || level.equalsIgnoreCase("ERROR")) { 219 lev = Level.ERROR; 220 } else if (level.equalsIgnoreCase("WARNING")) { 221 lev = Level.WARN; 222 } else if (level.equalsIgnoreCase("NOTICE") 223 || level.equalsIgnoreCase("INFO")) { 224 lev = Level.INFO; 225 } else if (level.equalsIgnoreCase("DEBUG") 226 || level.equalsIgnoreCase("DEBUG1") 227 || level.equalsIgnoreCase("DEBUG2") 228 || level.equalsIgnoreCase("DEBUG3") 229 || level.equalsIgnoreCase("DEBUG4") 230 || level.equalsIgnoreCase("DEBUG5") 231 || level.equalsIgnoreCase("DEBUG6") 232 || level.equalsIgnoreCase("DEBUG7") 233 || level.equalsIgnoreCase("DEBUG8") 234 || level.equalsIgnoreCase("DEBUG9") 235 || level.equalsIgnoreCase("DEBUGTMP")) { 236 lev = Level.DEBUG; 237 } else { lev = Level.DEBUG; 239 } 240 return lev; 241 } 242 243 249 public LogWriter getLogWriter(String level) { 250 return new Log4jLogWriter(this, level); 252 253 } 254 255 261 public LogWriter getLogWriter(int level) { 262 return new Log4jLogWriter(this, level); 264 } 265 } 266 | Popular Tags |