Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. @Plugin(name = "property", category = StrLookup.CATEGORY)
  2. public class CustomLookup extends AbstractLookup {
  3.  
  4. private static final String LINE_SEPARATOR = System
  5. .getProperty("line.separator");
  6.  
  7. private static AtomicLong aLong = new AtomicLong(0);
  8.  
  9. private StringBuilder formattedString;
  10. private StringBuilder javaPropsString;
  11. private StringBuilder pathPropsString;
  12. private StringBuilder osPropsString;
  13. private StringBuilder otherPropesString;
  14. // flag to determine if we already have formatted string ready
  15. // as we don't want to create new string every-time.
  16. private boolean isStringReady = false;
  17.  
  18. // flag to determine if environment variables are added to system
  19. // properties.
  20. private boolean isEnvPropsSet = false;
  21.  
  22. private Properties properties;
  23.  
  24. @Override
  25. public String lookup(LogEvent event, String key) {
  26.  
  27. // if this is the first call (i.e. system startup) we
  28. // don't log the system properties.
  29. if (aLong.getAndIncrement() == 0)
  30. return "";
  31. // on any subsequent calls i.e. on file rollover
  32. // we return the formatted string with system properties.
  33. else if (key.equalsIgnoreCase("customKey")) {
  34. prepareCustomLog();
  35. return formattedString.toString();
  36. } else
  37. return "";
  38.  
  39. }
  40.  
  41. /**
  42. * This method prepares the custom string in a readable format. It checks if
  43. * the string is already prepared with the {@link #isStringReady} flag.
  44. */
  45. private void prepareCustomLog() {
  46.  
  47. // If already prepared a string in previous call then do nothing here
  48. // as the string will be ready.
  49. if (!isStringReady) {
  50. formattedString = new StringBuilder();
  51. javaPropsString = new StringBuilder();
  52. osPropsString = new StringBuilder();
  53. pathPropsString = new StringBuilder();
  54. otherPropesString = new StringBuilder();
  55.  
  56. // If environment properties are not added to system properties add
  57. // them now.
  58. if (!isEnvPropsSet)
  59. addEnvProps();
  60.  
  61. properties = System.getProperties();
  62. Set<Object> set = properties.keySet();
  63. set.removeAll(getIgnoreList());
  64. Iterator<Object> i = set.iterator();
  65.  
  66. formattedString.append(LINE_SEPARATOR)
  67. .append("/*********** System properties *************/")
  68. .append(LINE_SEPARATOR);
  69.  
  70. javaPropsString.append(LINE_SEPARATOR).append(" ")
  71. .append("JAVA Properties").append(LINE_SEPARATOR);
  72.  
  73. osPropsString.append(LINE_SEPARATOR).append(" ")
  74. .append("OS Properties").append(LINE_SEPARATOR);
  75.  
  76. pathPropsString.append(LINE_SEPARATOR).append(" ")
  77. .append("PATH variables and properties")
  78. .append(LINE_SEPARATOR);
  79.  
  80. otherPropesString.append(LINE_SEPARATOR).append(" ")
  81. .append("Other Properties").append(LINE_SEPARATOR);
  82.  
  83. while (i.hasNext()) {
  84. String key = i.next().toString();
  85.  
  86. // filter out any keys those may contain privacy sensitive
  87. // details.
  88. if (!(key.toLowerCase().contains("user"))
  89. && !(key.toLowerCase().contains("home"))
  90. && !(key.toLowerCase().contains("system"))
  91. && !(key.toLowerCase().contains("program"))) {
  92. if (key.contains("java"))
  93. appendJavaPropsString(key);
  94. else if (key.contains("os"))
  95. appendOsPropsString(key);
  96. else if (key.contains("path"))
  97. appendPathPropsString(key);
  98. else
  99. appendOtherPropesString(key);
  100. }
  101. }
  102. formattedString.append(javaPropsString).append(osPropsString)
  103. .append(pathPropsString).append(otherPropesString);
  104. formattedString.append(
  105. "/*********** System properties *************/").append(
  106. LINE_SEPARATOR);
  107. isStringReady = true;
  108. }
  109. }
  110.  
  111. private void addEnvProps() {
  112. for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
  113. System.setProperty(entry.getKey(), entry.getValue());
  114. }
  115. }
  116.  
  117. private void appendJavaPropsString(final String key) {
  118. javaPropsString.append(" ").append(key).append(" : ")
  119. .append(properties.get(key)).append(LINE_SEPARATOR);
  120. }
  121.  
  122. private void appendOsPropsString(final String key) {
  123. osPropsString.append(" ").append(key).append(" : ")
  124. .append(properties.get(key)).append(LINE_SEPARATOR);
  125. }
  126.  
  127. private void appendPathPropsString(final String key) {
  128. pathPropsString.append(" ").append(key).append(" : ")
  129. .append(properties.get(key)).append(LINE_SEPARATOR);
  130. }
  131.  
  132. private void appendOtherPropesString(final String key) {
  133. otherPropesString.append(" ").append(key).append(" : ")
  134. .append(properties.get(key)).append(LINE_SEPARATOR);
  135. }
  136.  
  137. /**
  138. * This method returns a set of keys that we do not require to log, or the
  139. * keys which may have some sort of private information associated with it.
  140. *
  141. * @return set of keys to ignore when logging.
  142. */
  143. private Set<Object> getIgnoreList() {
  144. Set<Object> ignore = new HashSet<Object>();
  145.  
  146. ignore.add("M2");
  147. ignore.add("COMPUTERNAME");
  148. ignore.add("ProgramData");
  149. ignore.add("HOMEPATH");
  150. ignore.add("LOGONSERVER");
  151. ignore.add("LOCALAPPDATA");
  152. ignore.add("PUBLIC");
  153. ignore.add("APPDATA");
  154. return ignore;
  155. }
  156. }
  157.  
  158. private final Object lock = new Object();
  159. private final String[] keysToIgnore = {"user", "home", "system", "program"};
  160. private String customLog;
  161.  
  162.  
  163. @Override public String lookup(LogEvent event, String key) {
  164.  
  165. // if this is the first call (i.e. system startup) we
  166. // don't log the system properties.
  167. if (aLong.getAndIncrement() == 0) {
  168. return "";
  169. } else if (key.equalsIgnoreCase("customKey")) {
  170. synchronized (lock) {
  171. if (customLog == null) {
  172. customLog = makeCustomLog();
  173. }
  174. }
  175. return customLog;
  176. } else {
  177. return "";
  178. }
  179. }
  180.  
  181. private String makeCustomLog() {
  182. StringBuilder formatted = new StringBuilder();
  183. StringBuilder javaProps = new StringBuilder();
  184. StringBuilder osProps = new StringBuilder();
  185. StringBuilder pathProps = new StringBuilder();
  186. StringBuilder otherPropes = new StringBuilder();
  187.  
  188. // ... Basic initialization ...
  189.  
  190. while (i.hasNext()) {
  191. String key = i.next().toString();
  192.  
  193. //filter out any keys those may contain privacy sensitive details.
  194. if (!shouldIgnore(key)) {
  195. if (key.contains("java"))
  196. append(javaProps, key);
  197. else if (key.contains("os"))
  198. append(osProps, key);
  199. else if (key.contains("path"))
  200. append(pathProps, key);
  201. else
  202. append(key);
  203. }
  204. }
  205. formatted.append(javaProps).append(osProps)
  206. .append(pathProps).append(otherPropes);
  207. formatted.append(
  208. "/*********** System properties *************/").append(
  209. LINE_SEPARATOR);
  210. return formatted.toString();
  211. }
  212.  
  213. private boolean shouldIgnore(String key) {
  214. String loweredKey = key.toLowerCase(); //only do this once
  215. for (String match : keysToIgnore) {
  216. if (loweredKey.contains(match)) {
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222.  
  223. private void append(final StringBuilder builder, final String key) {
  224. builder.append(" ").append(key).append(" : ")
  225. .append(properties.get(key)).append(LINE_SEPARATOR);
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement