Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Plugin(name = "property", category = StrLookup.CATEGORY)
- public class CustomLookup extends AbstractLookup {
- private static final String LINE_SEPARATOR = System
- .getProperty("line.separator");
- private static AtomicLong aLong = new AtomicLong(0);
- private StringBuilder formattedString;
- private StringBuilder javaPropsString;
- private StringBuilder pathPropsString;
- private StringBuilder osPropsString;
- private StringBuilder otherPropesString;
- // flag to determine if we already have formatted string ready
- // as we don't want to create new string every-time.
- private boolean isStringReady = false;
- // flag to determine if environment variables are added to system
- // properties.
- private boolean isEnvPropsSet = false;
- private Properties properties;
- @Override
- public String lookup(LogEvent event, String key) {
- // if this is the first call (i.e. system startup) we
- // don't log the system properties.
- if (aLong.getAndIncrement() == 0)
- return "";
- // on any subsequent calls i.e. on file rollover
- // we return the formatted string with system properties.
- else if (key.equalsIgnoreCase("customKey")) {
- prepareCustomLog();
- return formattedString.toString();
- } else
- return "";
- }
- /**
- * This method prepares the custom string in a readable format. It checks if
- * the string is already prepared with the {@link #isStringReady} flag.
- */
- private void prepareCustomLog() {
- // If already prepared a string in previous call then do nothing here
- // as the string will be ready.
- if (!isStringReady) {
- formattedString = new StringBuilder();
- javaPropsString = new StringBuilder();
- osPropsString = new StringBuilder();
- pathPropsString = new StringBuilder();
- otherPropesString = new StringBuilder();
- // If environment properties are not added to system properties add
- // them now.
- if (!isEnvPropsSet)
- addEnvProps();
- properties = System.getProperties();
- Set<Object> set = properties.keySet();
- set.removeAll(getIgnoreList());
- Iterator<Object> i = set.iterator();
- formattedString.append(LINE_SEPARATOR)
- .append("/*********** System properties *************/")
- .append(LINE_SEPARATOR);
- javaPropsString.append(LINE_SEPARATOR).append(" ")
- .append("JAVA Properties").append(LINE_SEPARATOR);
- osPropsString.append(LINE_SEPARATOR).append(" ")
- .append("OS Properties").append(LINE_SEPARATOR);
- pathPropsString.append(LINE_SEPARATOR).append(" ")
- .append("PATH variables and properties")
- .append(LINE_SEPARATOR);
- otherPropesString.append(LINE_SEPARATOR).append(" ")
- .append("Other Properties").append(LINE_SEPARATOR);
- while (i.hasNext()) {
- String key = i.next().toString();
- // filter out any keys those may contain privacy sensitive
- // details.
- if (!(key.toLowerCase().contains("user"))
- && !(key.toLowerCase().contains("home"))
- && !(key.toLowerCase().contains("system"))
- && !(key.toLowerCase().contains("program"))) {
- if (key.contains("java"))
- appendJavaPropsString(key);
- else if (key.contains("os"))
- appendOsPropsString(key);
- else if (key.contains("path"))
- appendPathPropsString(key);
- else
- appendOtherPropesString(key);
- }
- }
- formattedString.append(javaPropsString).append(osPropsString)
- .append(pathPropsString).append(otherPropesString);
- formattedString.append(
- "/*********** System properties *************/").append(
- LINE_SEPARATOR);
- isStringReady = true;
- }
- }
- private void addEnvProps() {
- for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
- System.setProperty(entry.getKey(), entry.getValue());
- }
- }
- private void appendJavaPropsString(final String key) {
- javaPropsString.append(" ").append(key).append(" : ")
- .append(properties.get(key)).append(LINE_SEPARATOR);
- }
- private void appendOsPropsString(final String key) {
- osPropsString.append(" ").append(key).append(" : ")
- .append(properties.get(key)).append(LINE_SEPARATOR);
- }
- private void appendPathPropsString(final String key) {
- pathPropsString.append(" ").append(key).append(" : ")
- .append(properties.get(key)).append(LINE_SEPARATOR);
- }
- private void appendOtherPropesString(final String key) {
- otherPropesString.append(" ").append(key).append(" : ")
- .append(properties.get(key)).append(LINE_SEPARATOR);
- }
- /**
- * This method returns a set of keys that we do not require to log, or the
- * keys which may have some sort of private information associated with it.
- *
- * @return set of keys to ignore when logging.
- */
- private Set<Object> getIgnoreList() {
- Set<Object> ignore = new HashSet<Object>();
- ignore.add("M2");
- ignore.add("COMPUTERNAME");
- ignore.add("ProgramData");
- ignore.add("HOMEPATH");
- ignore.add("LOGONSERVER");
- ignore.add("LOCALAPPDATA");
- ignore.add("PUBLIC");
- ignore.add("APPDATA");
- return ignore;
- }
- }
- private final Object lock = new Object();
- private final String[] keysToIgnore = {"user", "home", "system", "program"};
- private String customLog;
- @Override public String lookup(LogEvent event, String key) {
- // if this is the first call (i.e. system startup) we
- // don't log the system properties.
- if (aLong.getAndIncrement() == 0) {
- return "";
- } else if (key.equalsIgnoreCase("customKey")) {
- synchronized (lock) {
- if (customLog == null) {
- customLog = makeCustomLog();
- }
- }
- return customLog;
- } else {
- return "";
- }
- }
- private String makeCustomLog() {
- StringBuilder formatted = new StringBuilder();
- StringBuilder javaProps = new StringBuilder();
- StringBuilder osProps = new StringBuilder();
- StringBuilder pathProps = new StringBuilder();
- StringBuilder otherPropes = new StringBuilder();
- // ... Basic initialization ...
- while (i.hasNext()) {
- String key = i.next().toString();
- //filter out any keys those may contain privacy sensitive details.
- if (!shouldIgnore(key)) {
- if (key.contains("java"))
- append(javaProps, key);
- else if (key.contains("os"))
- append(osProps, key);
- else if (key.contains("path"))
- append(pathProps, key);
- else
- append(key);
- }
- }
- formatted.append(javaProps).append(osProps)
- .append(pathProps).append(otherPropes);
- formatted.append(
- "/*********** System properties *************/").append(
- LINE_SEPARATOR);
- return formatted.toString();
- }
- private boolean shouldIgnore(String key) {
- String loweredKey = key.toLowerCase(); //only do this once
- for (String match : keysToIgnore) {
- if (loweredKey.contains(match)) {
- return true;
- }
- }
- return false;
- }
- private void append(final StringBuilder builder, final String key) {
- builder.append(" ").append(key).append(" : ")
- .append(properties.get(key)).append(LINE_SEPARATOR);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement