1 28 29 package com.caucho.log; 30 31 import com.caucho.config.ConfigELContext; 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.types.RawString; 34 import com.caucho.el.AbstractVariableResolver; 35 import com.caucho.el.ELParser; 36 import com.caucho.el.Expr; 37 import com.caucho.util.L10N; 38 39 import javax.annotation.PostConstruct; 40 import javax.el.ELContext; 41 import javax.el.ELException; 42 import java.util.ResourceBundle ; 43 import java.util.logging.Level ; 44 import java.util.logging.LogRecord ; 45 46 49 public class ELFormatter extends MessageFormatter { 50 static final L10N L = new L10N(ELFormatter.class); 51 52 private String _format; 53 private Expr _expr; 54 55 public void setFormat(RawString format) 56 { 57 _format = format.getValue(); 58 } 59 60 public String getFormat() 61 { 62 return _format; 63 } 64 65 @PostConstruct 66 public void init() 67 throws ConfigException 68 { 69 if (_format != null) { 70 try { 71 _expr = (new ELParser(new ConfigELContext(), _format)).parse(); 72 } catch (Exception ex) { 73 throw new ConfigException(ex); 74 } 75 } 76 } 77 78 public String format(LogRecord logRecord) 79 { 80 if (_expr == null) { 81 return super.format(logRecord); 82 } 83 84 String ret; 85 if (_expr == null) { 86 ret = super.format(logRecord); 87 } 88 else { 89 try { 90 ELFormatterVariableResolver vr = new ELFormatterVariableResolver(); 91 vr.setLogRecord(logRecord); 92 93 ret = _expr.evalString(new ConfigELContext(vr)); 94 } 95 catch (Exception ex) { 96 throw new RuntimeException (ex); 97 } 98 } 99 100 return ret; 101 } 102 103 class ELFormatterVariableResolver extends AbstractVariableResolver 104 { 105 ELFormatterLogRecord _logRecord; 106 107 public void setLogRecord(LogRecord logRecord) 108 { 109 _logRecord = new ELFormatterLogRecord(logRecord); 110 } 111 112 @Override 113 public Object getValue(ELContext env, Object base, Object property) 114 throws ELException 115 { 116 if (base != null || ! (property instanceof String )) 117 return null; 118 119 if ("log".equals(property)) { 120 env.setPropertyResolved(true); 121 122 return _logRecord; 123 } 124 125 return null; 126 } 127 } 128 129 133 public class ELFormatterLogRecord 134 { 135 LogRecord _logRecord; 136 137 ELFormatterLogRecord(LogRecord logRecord) 138 { 139 _logRecord = logRecord; 140 } 141 142 160 public String getMessage() 161 { 162 163 return formatMessage(_logRecord); 164 } 165 166 171 public String getName() 172 { 173 return _logRecord.getLoggerName(); 174 } 175 176 181 public String getLoggerName() 182 { 183 return _logRecord.getLoggerName(); 184 } 185 186 194 public String getShortName() 195 { 196 String name = _logRecord.getLoggerName(); 197 198 if (name != null) { 199 int index = name.lastIndexOf('.') + 1; 200 if (index > 0 && index < name.length()) { 201 name = name.substring(index); 202 } 203 } 204 205 return name; 206 } 207 208 213 public Level getLevel() 214 { 215 return _logRecord.getLevel(); 216 } 217 218 221 public long getMillis() 222 { 223 return _logRecord.getMillis(); 224 } 225 226 229 public int getThreadID() 230 { 231 return _logRecord.getThreadID(); 232 } 233 234 237 public Throwable getThrown() 238 { 239 return _logRecord.getThrown(); 240 } 241 242 245 public long getSequenceNumber() 246 { 247 return _logRecord.getSequenceNumber(); 248 } 249 250 255 public String getSourceClassName() 256 { 257 return _logRecord.getSourceClassName(); 258 } 259 260 268 public String getShortSourceClassName() 269 { 270 String name = _logRecord.getSourceClassName(); 271 272 if (name != null) { 273 int index = name.lastIndexOf('.') + 1; 274 if (index > 0 && index < name.length()) { 275 name = name.substring(index); 276 } 277 } 278 279 return name; 280 } 281 282 287 public String getSourceMethodName() 288 { 289 return _logRecord.getSourceMethodName(); 290 } 291 292 302 public String getRawMessage() 303 { 304 return _logRecord.getMessage(); 305 } 306 307 310 public ResourceBundle getResourceBundle() 311 { 312 return _logRecord.getResourceBundle(); 313 } 314 315 318 public String getResourceBundleName() 319 { 320 return _logRecord.getResourceBundleName(); 321 } 322 323 public Object [] getParameters() 324 { 325 return _logRecord.getParameters(); 326 } 327 } 328 } 329 330 | Popular Tags |