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