Attachment 'LevelPatternParser.java'

Download

   1 /* Creado en 09-mar-2005 */
   2 package fcc.ima.trazado;
   3 
   4 import org.apache.log4j.helpers.PatternConverter;
   5 import org.apache.log4j.spi.LoggingEvent;
   6 
   7 /**Adds support for new field specifiers:<ul>
   8  * 	<li>%P that shows the first letter of the priority.
   9  *  <li>%T dump stack trace with optional stop packages and filtering.
  10  * </ul>
  11  * @see LevelPatternLayout
  12  * @author dperezcar*/
  13 public class LevelPatternParser extends org.apache.log4j.helpers.PatternParser {
  14 	static class LevelPatternConverter extends PatternConverter {
  15 		/**@see org.apache.log4j.helpers.PatternConverter#convert(org.apache.log4j.spi.LoggingEvent) */
  16 		protected String convert(LoggingEvent event) {
  17 			return event.getLevel().toString().substring(0, 1);
  18 		}
  19 	}
  20 	class StackTraceConverter extends PatternConverter {
  21 		private boolean beginsWith(String s, String[] prefixes) {
  22 			if (prefixes != null) {
  23 				for (int j = 0; j < prefixes.length; j++) {
  24 					if (s.startsWith(prefixes[j])) {
  25 						return true;
  26 					}
  27 				}
  28 			}
  29 			return false;
  30 		}
  31 		protected String convert(LoggingEvent event) {
  32 			String[] msgs = event.getThrowableStrRep();
  33 			if (msgs == null || msgs.length == 0) {
  34 				return "";
  35 			}
  36 			StringBuffer sb = new StringBuffer(msgs.length*30);
  37 			boolean stopped = false;
  38 			for (int i = 0; i < msgs.length; i++) {
  39 				if (!stopped) {
  40 					if (i > 0 && beginsWith(msgs[i], stop)) {
  41 						stopped = true;
  42 					}
  43 				} else if (!msgs[i].startsWith("\tat")) {
  44 					// Possibly a nested exception, show again stack traces
  45 					stopped = false;
  46 				}
  47 				if (!stopped && !beginsWith(msgs[i], filter)) {
  48 					sb.append("\n");
  49 					sb.append(msgs[i]);
  50 				}
  51 			}
  52 			return sb.toString();
  53 		}
  54 	}
  55 	protected String[] stop;
  56 	protected String[] filter;
  57 
  58 	public LevelPatternParser(String pattern, String[] stop, String[] filter) {
  59 		super(pattern);
  60 		this.stop = stop;
  61 		this.filter = filter;
  62 	}
  63 	/**@see org.apache.log4j.helpers.PatternParser#finalizeConverter(char) */
  64 	protected void finalizeConverter(char c) {
  65 		if (c == 'P') {
  66 		    currentLiteral.setLength(0);
  67 			addConverter(new LevelPatternConverter());
  68 		} else if (c == 'T') {
  69 		    currentLiteral.setLength(0);
  70 			addConverter(new StackTraceConverter());
  71 		} else {
  72 			super.finalizeConverter(c);
  73 		}
  74 	}
  75 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.