1 23 package com.lutris.logging; 24 25 import java.text.SimpleDateFormat ; 26 27 import org.objectweb.util.monolog.api.BasicLevel; 28 import org.objectweb.util.monolog.api.Level; 29 31 32 42 public class MonologLogChannel implements LogChannel { 43 44 47 String separatorLine =""; 48 private String facility; 49 50 private static final int INT_ERROR = -1; 51 52 55 private static final SimpleDateFormat dateFormatter = new SimpleDateFormat ("yyyy.MM.dd HH:mm:ss"); 56 57 60 private org.objectweb.util.monolog.api.Logger logger; 61 62 69 protected MonologLogChannel(String chanFacility, 70 org.objectweb.util.monolog.api.Logger loggerObj,String separator) { 71 facility = chanFacility; 72 logger = loggerObj; 73 separatorLine=separator; 74 } 75 76 public int getLevel(String level) { 77 if (logger != null) { 78 return logger.getCurrentIntLevel(); 79 } 80 return INT_ERROR; 81 } 82 83 86 public boolean isEnabled(int level) { 87 return true; 88 } 90 91 94 public boolean isEnabled(String level) { 95 return true; 96 } 97 98 101 public void write(int level, String msg) { 102 Level lev; 103 104 lev = intToLevel(level); 105 if (lev == null) { 106 lev = BasicLevel.LEVEL_INFO; 107 } 108 logger.log(lev, msg + separatorLine); 109 } 110 111 114 public void write(int level, String msg, Throwable throwable) { 115 Level lev; 116 117 lev = intToLevel(level); 118 if (lev == null) { 119 lev = BasicLevel.LEVEL_INFO; 120 } 121 logger.log(lev, msg + separatorLine, throwable); 122 } 123 124 127 public void write(String level, String msg) { 128 Level lev; 129 130 lev = stringToLevel(level); 131 if (lev == null) { 132 lev = BasicLevel.LEVEL_INFO; 133 } 134 logger.log(lev, msg + separatorLine); 135 } 136 137 140 public void write(String level, String msg, Throwable throwable) { 141 Level lev; 142 lev = stringToLevel(level); 143 if (lev == null) { 144 lev = BasicLevel.LEVEL_INFO; 145 } 146 logger.log(lev, msg + separatorLine, throwable); 147 } 148 149 private Level intToLevel(int level) { 150 Level lev; 151 152 switch (level) { 153 case com.lutris.logging.Logger.EMERGENCY: 154 case com.lutris.logging.Logger.ALERT: 155 lev = BasicLevel.LEVEL_FATAL; 156 break; 157 158 case com.lutris.logging.Logger.CRITICAL: 159 case com.lutris.logging.Logger.ERROR: 160 lev = BasicLevel.LEVEL_ERROR; 161 break; 162 163 case com.lutris.logging.Logger.WARNING: 164 lev = BasicLevel.LEVEL_WARN; 165 break; 166 167 case com.lutris.logging.Logger.NOTICE: 168 case com.lutris.logging.Logger.INFO: 169 lev = BasicLevel.LEVEL_INFO; 170 break; 171 172 case com.lutris.logging.Logger.DEBUG: 173 case com.lutris.logging.Logger.DEBUG1: 174 case com.lutris.logging.Logger.DEBUG2: 175 case com.lutris.logging.Logger.DEBUG3: 176 case com.lutris.logging.Logger.DEBUG4: 177 case com.lutris.logging.Logger.DEBUG5: 178 case com.lutris.logging.Logger.DEBUG6: 179 case com.lutris.logging.Logger.DEBUG7: 180 case com.lutris.logging.Logger.DEBUG8: 181 case com.lutris.logging.Logger.DEBUG9: 182 case com.lutris.logging.Logger.DEBUGTMP: 183 lev = BasicLevel.LEVEL_DEBUG; 184 break; 185 186 default: 187 lev = BasicLevel.LEVEL_DEBUG; 188 } return lev; 190 } 191 192 private Level stringToLevel(String level) { 193 Level lev; 194 195 if (level.equalsIgnoreCase("EMERGENCY") 196 || level.equalsIgnoreCase("ALERT")) { 197 lev = BasicLevel.LEVEL_FATAL; 198 } else if (level.equalsIgnoreCase("CRITICAL") 199 || level.equalsIgnoreCase("ERROR")) { 200 lev = BasicLevel.LEVEL_ERROR; 201 } else if (level.equalsIgnoreCase("WARNING")) { 202 lev = BasicLevel.LEVEL_WARN; 203 } else if (level.equalsIgnoreCase("NOTICE") 204 || level.equalsIgnoreCase("INFO")) { 205 lev = BasicLevel.LEVEL_INFO; 206 } else if (level.equalsIgnoreCase("DEBUG") 207 || level.equalsIgnoreCase("DEBUG1") 208 || level.equalsIgnoreCase("DEBUG2") 209 || level.equalsIgnoreCase("DEBUG3") 210 || level.equalsIgnoreCase("DEBUG4") 211 || level.equalsIgnoreCase("DEBUG5") 212 || level.equalsIgnoreCase("DEBUG6") 213 || level.equalsIgnoreCase("DEBUG7") 214 || level.equalsIgnoreCase("DEBUG8") 215 || level.equalsIgnoreCase("DEBUG9") 216 || level.equalsIgnoreCase("DEBUGTMP")) { 217 lev = BasicLevel.LEVEL_DEBUG; 218 } else { lev = BasicLevel.LEVEL_DEBUG; 220 } 221 return lev; 222 } 223 224 230 public LogWriter getLogWriter(String level) { 231 return new MonologWriter(this, level); 232 233 } 234 235 241 public LogWriter getLogWriter(int level) { 242 return new MonologWriter(this, level); 243 } 244 } 245 | Popular Tags |