1 18 package org.objectweb.util.monolog.wrapper.log4j; 19 20 import org.objectweb.util.monolog.file.api.Pattern; 21 22 28 public class PatternConverter implements Pattern { 29 30 public static String monolog2log4j(String p) { 31 String res = new String (p); 32 int percentPos = res.indexOf('%', 0); 33 boolean end = (percentPos == -1); 34 int nextPercentPos = 0; 35 while (!end) { 36 nextPercentPos = res.indexOf('%', percentPos + 1); 37 if (nextPercentPos == -1) { 38 nextPercentPos = res.length(); 39 end = true; 40 } 41 for (int i = percentPos + 1; i < nextPercentPos; i++) { 42 char el = res.charAt(i); 43 if (el == Pattern.LEVEL) { 44 res = substitute(res, 'p', i); 45 break; 46 } 47 else if (el == Pattern.OBJECT) { 48 res = substitute(res, 'C', i); 49 break; 50 } 51 else if (el == Pattern.THREAD) { 52 res = substitute(res, 't', i); 53 break; 54 } 55 else if (el == Pattern.TOPIC) { 56 res = substitute(res, 'c', i); 57 break; 58 } 59 else if(Character.isLetter(el)) { 60 break; 61 } 62 } 63 percentPos = nextPercentPos; 64 } 65 return res; 66 } 67 68 public static String log4j2monolog(String p) { 69 String res = new String (p); 70 int percentPos = res.indexOf('%', 0); 71 boolean end = (percentPos == -1); 72 int nextPercentPos = 0; 73 while (!end) { 74 nextPercentPos = res.indexOf('%', percentPos + 1); 75 if (nextPercentPos == -1) { 76 nextPercentPos = res.length(); 77 end = true; 78 } 79 for (int i = percentPos + 1; i < nextPercentPos; i++) { 80 char el = res.charAt(i); 81 if (el == 'p') { 82 res = substitute(res, Pattern.LEVEL, i); 83 break; 84 } 85 else if (el == 'C') { 86 res = substitute(res, Pattern.OBJECT, i); 87 break; 88 } 89 else if (el == 't') { 90 res = substitute(res, Pattern.THREAD, i); 91 break; 92 } 93 else if (el == 'c') { 94 res = substitute(res, Pattern.TOPIC, i); 95 break; 96 } 97 } 98 percentPos = nextPercentPos; 99 } 100 return res; 101 } 102 103 private static String substitute(String s, char c, int pos) { 104 if (s == null) 105 return null; 106 if (s.length() <= pos) 107 return s; 108 return s.substring(0, pos) + c + s.substring(pos + 1, s.length()); 109 } 110 } 111 | Popular Tags |