1 16 17 package examples; 18 19 import org.apache.log4j.*; 20 import org.apache.log4j.helpers.FormattingInfo; 21 import org.apache.log4j.helpers.PatternConverter; 22 import org.apache.log4j.helpers.PatternParser; 23 import org.apache.log4j.spi.LoggingEvent; 24 25 43 public class MyPatternParser extends PatternParser { 44 45 int counter = 0; 46 47 public 48 MyPatternParser(String pattern) { 49 super(pattern); 50 } 51 52 public 53 void finalizeConverter(char c) { 54 if (c == '#') { 55 addConverter(new UserDirPatternConverter(formattingInfo)); 56 currentLiteral.setLength(0); 57 } else { 58 super.finalizeConverter(c); 59 } 60 } 61 62 private class UserDirPatternConverter extends PatternConverter { 63 UserDirPatternConverter(FormattingInfo formattingInfo) { 64 super(formattingInfo); 65 } 66 67 public 68 String convert(LoggingEvent event) { 69 return String.valueOf(++counter); 70 } 71 } 72 } 73 | Popular Tags |