Java Code Examples for java.util.logging.LogRecord#getSourceClassName()

The following examples show how to use java.util.logging.LogRecord#getSourceClassName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BriefLogFormatter.java    From RipplePower with Apache License 2.0 7 votes vote down vote up
@Override
public String format(LogRecord logRecord) {
	Object[] arguments = new Object[6];
	arguments[0] = logRecord.getLevel().getName();
	String fullClassName = logRecord.getSourceClassName();
	int lastDot = fullClassName.lastIndexOf('.');
	String className = fullClassName.substring(lastDot + 1);
	arguments[1] = className;
	arguments[2] = logRecord.getSourceMethodName();
	arguments[3] = new Date(logRecord.getMillis());
	arguments[4] = logRecord.getMessage();
	if (logRecord.getThrown() != null) {
		Writer result = new StringWriter();
		logRecord.getThrown().printStackTrace(new PrintWriter(result));
		arguments[5] = result.toString();
	} else {
		arguments[5] = "";
	}

	return messageFormat.format(arguments);
}
 
Example 2
Source File: JsonFormatter.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String formatMessage(LogRecord record) {
    StringBuilder sb = new StringBuilder();
    if (record.getSourceClassName() != null) {
        sb.append(record.getSourceClassName());
    } else {
        sb.append(record.getLoggerName());
    }
    if (record.getSourceMethodName() != null) {
        sb.append(' ');
        sb.append(record.getSourceMethodName());
    }
    sb.append(": ");
    sb.append(super.formatMessage(record));
    Throwable thrown = record.getThrown();
    if (thrown != null) {
        StringWriter sw = new StringWriter();
        try (PrintWriter pw = new PrintWriter(sw);) {
            sb.append("\n");
            thrown.printStackTrace(pw);
        }
        sb.append(sw.getBuffer());
    }
    return sb.toString();
}
 
Example 3
Source File: JavaUtilFormatter.java    From kumuluzee with MIT License 6 votes vote down vote up
@Override
public String format(LogRecord record) {

    ZonedDateTime zonedDateTime = Instant.ofEpochMilli(record.getMillis()).atZone(ZoneId.systemDefault());

    String source = record.getSourceClassName() != null ? record.getSourceClassName() : record.getLoggerName();
    String message = formatMessage(record);
    String throwable = "";

    if (record.getThrown() != null) {

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        pw.println();
        record.getThrown().printStackTrace(pw);
        pw.close();
        throwable = sw.toString();
    }

    return zonedDateTime.format(timestampFormat) + " " +
            record.getLevel().getName() + " -- " +
            source + " -- " +
            message + throwable + "\n";
}
 
Example 4
Source File: LeanFormatter.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public String format(LogRecord record) {
	StringBuilder b = new StringBuilder();
	b.append(dateFormat.format(new Date(record.getMillis())));
	b.append(" ");
	b.append(record.getLevel().getLocalizedName());
	b.append(": ");
	b.append(formatMessage(record));
	String className = record.getSourceClassName();
	if (className != null) {
		int dot = className.lastIndexOf('.');
		if (dot != -1) {
			className = className.substring(dot + 1);
		}
		b.append(" (").append(className).append(".").append(record.getSourceMethodName()).append("())");
	}
	b.append("\n");
	append(record.getThrown(), b);
	return b.toString();
}
 
Example 5
Source File: TestLogHandler.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void publish(LogRecord record) {
    String msg = record.getMessage();
    String method = record.getSourceMethodName();
    String className = record.getSourceClassName();
    if (msg.contains(illegal)) {
        testFailed = true;
    }
    if (msg.contains("attribute names=")) {
        System.err.println("LOG: " + className + "." + method + ": " + msg);
    }
}
 
Example 6
Source File: LogFormatter.java    From alcor with Apache License 2.0 5 votes vote down vote up
@Override
public String format(LogRecord lr) {
    String strSourceClassName = lr.getSourceClassName();
    if (strSourceClassName == null)
        strSourceClassName = " ";
    return String.format("[%1$tF %1$tT] [%2$-7s] %4$s %6$s %7$s%n",
            new Date(lr.getMillis()),
            lr.getLevel(),
            (lr.getLoggerName() == null) ? " " : lr.getLoggerName(),
            (strSourceClassName == null) ? " " : strSourceClassName.substring(strSourceClassName.lastIndexOf(".") + 1),
            (lr.getSourceMethodName() == null) ? " " : lr.getSourceMethodName(),
            lr.getMessage(),
            (lr.getThrown() == null) ? " " : lr.getThrown().getMessage()
    );
}
 
Example 7
Source File: MyFormatter.java    From journaldev with MIT License 5 votes vote down vote up
@Override
public String format(LogRecord record) {
    return record.getThreadID()+"::"+record.getSourceClassName()+"::"
            +record.getSourceMethodName()+"::"
            +new Date(record.getMillis())+"::"
            +record.getMessage()+"\n";
}
 
Example 8
Source File: MyLogFormatter.java    From Kylin with Apache License 2.0 5 votes vote down vote up
/**
 * Format the given LogRecord.
 * 
 * @param record
 *            the log record to be formatted.
 * @return a formatted log record
 */
public synchronized String format(LogRecord record) {
    StringBuffer sb = new StringBuffer();
    // Minimize memory allocations here.
    Timestamp ts = new Timestamp(record.getMillis());
    String text = ts.toString();
    sb.append("JUL ");
    sb.append(text);
    sb.append(" ");
    if (record.getSourceClassName() != null) {
        sb.append(record.getSourceClassName());
    } else {
        sb.append(record.getLoggerName());
    }
    if (record.getSourceMethodName() != null) {
        sb.append(" ");
        sb.append(record.getSourceMethodName());
    }
    sb.append(lineSeparator);
    String message = formatMessage(record);
    sb.append(record.getLevel().getLocalizedName());
    sb.append(": ");
    sb.append(message);
    sb.append(lineSeparator);
    if (record.getThrown() != null) {
        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            record.getThrown().printStackTrace(pw);
            pw.close();
            sb.append(sw.toString());
        } catch (Exception ex) {
        }
    }
    return sb.toString();
}
 
Example 9
Source File: SFFormatter.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public String format(LogRecord record)
{
  int lineNumber = -1;
  String className = record.getSourceClassName();
  final String methodName = record.getSourceMethodName();
  StackTraceElement[] stackTraces = Thread.currentThread().getStackTrace();
  for (StackTraceElement ste : stackTraces)
  {
    if (className.equals(ste.getClassName())
        && methodName.equals(ste.getMethodName()))
    {
      lineNumber = ste.getLineNumber();
      break;
    }
  }
  if (className.startsWith(CLASS_NAME_PREFIX))
  {
    className = "n.s.c" + className.substring(CLASS_NAME_PREFIX.length());
  }
  else if (className.startsWith(INFORMATICA_V1_CLASS_NAME_PREFIX))
  {
    className = "c.s" + className.substring(
        INFORMATICA_V1_CLASS_NAME_PREFIX.length());
  }

  StringBuilder builder = new StringBuilder(1000);
  builder.append(df.format(new Date(record.getMillis()))).append(" ");
  builder.append(className).append(" ");
  builder.append(record.getLevel()).append(" ");
  builder.append(methodName).append(":");
  builder.append(lineNumber).append(" - ");
  builder.append(formatMessage(record));
  builder.append("\n");
  return builder.toString();
}
 
Example 10
Source File: VmRuntimeLogHandler.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
/**
 * Format the given LogRecord.
 * @param record the log record to be formatted.
 * @return a formatted log record
 */
@Override
public synchronized String format(LogRecord record) {
  StringBuffer sb = new StringBuffer();
  if (record.getSourceClassName() != null) {
    sb.append(record.getSourceClassName());
  } else {
    sb.append(record.getLoggerName());
  }
  if (record.getSourceMethodName() != null) {
    sb.append(" ");
    sb.append(record.getSourceMethodName());
  }
  sb.append(": ");
  String message = formatMessage(record);
  sb.append(message);
  sb.append("\n");
  if (record.getThrown() != null) {
    try {
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      record.getThrown().printStackTrace(pw);
      pw.close();
      sb.append(sw.toString());
    } catch (Exception ex) {
    }
  }
  return sb.toString();
}
 
Example 11
Source File: TestLogHandler.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void publish(LogRecord record) {
    String msg = record.getMessage();
    String method = record.getSourceMethodName();
    String className = record.getSourceClassName();
    if (msg.contains(illegal)) {
        testFailed = true;
    }
    if (msg.contains("attribute names=")) {
        System.err.println("LOG: " + className + "." + method + ": " + msg);
    }
}
 
Example 12
Source File: LogFormatter.java    From cplus-libparser with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public String format(LogRecord record) {
  dat.setTime(record.getMillis());
  String source;
  if (record.getSourceClassName() != null) {
      source = record.getSourceClassName().substring(record.getSourceClassName().lastIndexOf('.') + 1);
      if (record.getSourceMethodName() != null) {
         source += "." + record.getSourceMethodName();
      }
  } else {
      source = record.getLoggerName();
  }
  String message = formatMessage(record);
  String throwable = "";
  if (record.getThrown() != null) {
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      pw.println();
      record.getThrown().printStackTrace(pw);
      pw.close();
      throwable = sw.toString();
  }
  return String.format(format,
                       dat,
                       source,
                       record.getLoggerName(),
                       record.getLevel(),
                       message,
                       throwable);
}
 
Example 13
Source File: TestLogHandler.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void publish(LogRecord record) {
    String msg = record.getMessage();
    String method = record.getSourceMethodName();
    String className = record.getSourceClassName();
    if (msg.contains(illegal)) {
        testFailed = true;
    }
    if (msg.contains("attribute names=")) {
        System.err.println("LOG: " + className + "." + method + ": " + msg);
    }
}
 
Example 14
Source File: Twister2LogFormatter.java    From twister2 with Apache License 2.0 5 votes vote down vote up
@Override
public String format(LogRecord record) {
  dat.setTime(record.getMillis());
  String source;
  if (record.getSourceClassName() != null) {
    source = record.getSourceClassName();
    if (record.getSourceMethodName() != null) {
      source += " " + record.getSourceMethodName();
    }
  } else {
    source = record.getLoggerName();
  }
  String message = formatMessage(record);
  String throwable = "";
  if (record.getThrown() != null) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    pw.println();
    record.getThrown().printStackTrace(pw);
    pw.close();
    throwable = sw.toString();
  }
  return String.format(FORMAT,
      dat,
      source,
      record.getLoggerName(),
      record.getLevel().getLocalizedName(),
      message,
      throwable,
      componentName,
      this.getThreadName(record.getThreadID())
  );
}
 
Example 15
Source File: GcpJsonFormatter.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Override
public String format(LogRecord record) {
  // Add an extra newline before the message. Stackdriver does not show newlines correctly, and
  // treats them as whitespace. If you want to see correctly formatted log message, expand the
  // log and look for the jsonPayload.message field. This newline makes sure that the entire
  // message starts on its own line, so that indentation within the message is correct.

  String message = "\n" + record.getMessage();
  String severity = severityFor(record.getLevel());

  // The rest is mostly lifted from java.util.logging.SimpleFormatter.
  String stacktrace = "";
  if (record.getThrown() != null) {
    StringWriter sw = new StringWriter();
    try (PrintWriter pw = new PrintWriter(sw)) {
      pw.println();
      record.getThrown().printStackTrace(pw);
    }
    stacktrace = sw.toString();
  }

  String source;
  if (record.getSourceClassName() != null) {
    source = record.getSourceClassName();
    if (record.getSourceMethodName() != null) {
      source += " " + record.getSourceMethodName();
    }
  } else {
    source = record.getLoggerName();
  }

  return gson.toJson(
          ImmutableMap.of(SEVERITY, severity, SOURCE, source, MESSAGE, message + stacktrace))
      + '\n';
}
 
Example 16
Source File: PatternFormatter.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void formatImpl(StringBuilder sb, LogRecord log)
{
  String name = log.getSourceClassName();
  
  sb.append(_filter.format(name));
}
 
Example 17
Source File: UIMALogFormatter.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public synchronized String format(LogRecord record) {
  // if record is null, return an empty String
  if (record == null) {
    return "";
  }

  StringBuffer buffer = new StringBuffer(100);

  // create timestamp
  Date timestamp = new Date(record.getMillis());
  String timestampStr = tsFormatter.format(timestamp);
  // append timestamp to the output string
  buffer.append(timestampStr);

  // append source thread ID
  buffer.append(" - ");
  buffer.append(record.getThreadID());

  // append source class if logrb() method was used, otherwise append logger name
  buffer.append(": ");
  if (record.getSourceClassName() == null || record.getSourceClassName().equals("")) {
    buffer.append(record.getLoggerName());
  } else {
    buffer.append(record.getSourceClassName());
  }

  // append source method if logrb() was used
  if (record.getSourceMethodName() != null) {
    buffer.append('.');
    buffer.append(record.getSourceMethodName());
  }

  // append message level
  buffer.append(": ");
  buffer.append(record.getLevel().getLocalizedName());

  // append message
  buffer.append(": ");
  buffer.append(record.getMessage());

  // append exception if avaialble
  if (record.getThrown() != null) {
    buffer.append(CRLF);
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    record.getThrown().printStackTrace(printWriter);
    printWriter.close();
    buffer.append(stringWriter.toString());
  }

  // append new line at the end
  buffer.append(CRLF);

  // return log entry
  return buffer.toString();
}
 
Example 18
Source File: LogFormatter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Format the given message to XML.
 * @param record the log record to be formatted.
 * @return a formatted log record
 */
public @Override String format(LogRecord record) {
    StringBuffer sb = new StringBuffer(1000);
    sb.append("<record>\n");// NOI18N
    
    sb.append("  <date>");// NOI18N
    appendISO8601(sb, record.getMillis());
    sb.append("</date>\n");// NOI18N
    
    sb.append("  <millis>");// NOI18N
    sb.append(record.getMillis());
    sb.append("</millis>\n");// NOI18N
    
    sb.append("  <sequence>");// NOI18N
    sb.append(record.getSequenceNumber());
    sb.append("</sequence>\n");// NOI18N
    
    String name = record.getLoggerName();
    if (name != null) {
        sb.append("  <logger>");// NOI18N
        escape(sb, name);
        sb.append("</logger>\n");// NOI18N
    }
    
    sb.append("  <level>");// NOI18N
    String level = Integer.toString(record.getLevel().intValue());
    escape(sb, level);
    sb.append("</level>\n");// NOI18N
    
    if (record.getSourceClassName() != null) {
        sb.append("  <class>");// NOI18N
        escape(sb, record.getSourceClassName());
        sb.append("</class>\n");// NOI18N
    }
    
    if (record.getSourceMethodName() != null) {
        sb.append("  <method>");// NOI18N
        escape(sb, record.getSourceMethodName());
        sb.append("</method>\n");// NOI18N
    }
    
    sb.append("  <thread>");// NOI18N
    sb.append(record.getThreadID());
    sb.append("</thread>\n");// NOI18N
    
    String message = record.getMessage();
    if (message != null) {
        sb.append("  <message>");// NOI18N
        escape(sb, message);
        sb.append("</message>\n");// NOI18N
    }
            
    // If the message is being localized, output the key, resource
    // bundle name, and params.
    ResourceBundle bundle = record.getResourceBundle();
    try {
        if (bundle != null && bundle.getString(message) != null) {
            sb.append("  <key>");// NOI18N
            escape(sb, message);
            sb.append("</key>\n");// NOI18N
            sb.append("  <catalog>");// NOI18N
            escape(sb, record.getResourceBundleName());
            sb.append("</catalog>\n");// NOI18N
        }
    } catch (Exception exc) {
        // The message is not in the catalog.  Drop through.
        Logger.getLogger(LogFormatter.class.getName()).log(Level.FINE, "Catalog loading error", exc);// NOI18N
    }

    Object parameters[] = record.getParameters();
    //  Check to see if the parameter was not a messagetext format
    //  or was not null or empty
    if ( parameters != null && parameters.length != 0
            && (message == null || message.indexOf("{") == -1) ) {
        for (int i = 0; i < parameters.length; i++) {
            sb.append("  <param>");// NOI18N
            try {
                escape(sb, paramToString(parameters[i]));
            } catch (Exception ex) {
                sb.append("???");// NOI18N
            }
            sb.append("</param>\n");// NOI18N
        }
    }
    
    if (record.getThrown() != null) {
        printThrown(record.getThrown(), sb);
    }
    
    sb.append("</record>\n");// NOI18N
    return sb.toString();
}
 
Example 19
Source File: Log.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Logger getPackageLogger() {
      if (logger == null) {
          String prop = System.getProperty(packageName, DEFAULT_NAME);
          for (int i = 1; i < LEVELS.length; i++) {
              if (prop.toLowerCase().equals(LEVEL_NAMES[i].toLowerCase())) {
                  level = LEVELS[i];
                  break;
              }
          }
          LogManager.getLogManager().addLogger(new Logger(packageName, null)
{});
          logger = LogManager.getLogManager().getLogger(packageName);
          if (logger == null) {
              System.out.println(packageName + ": "
		   + rb.getString("CANT_GET_LOGGER"));
              return Logger.getLogger("global");
          }
          try {
              logger.setLevel(level);
              Handler handler = new ConsoleHandler();
              handler.setLevel(level);
// Default handlers don't provide class name or method name, so roll our own
logger.setUseParentHandlers(false);
Formatter formatter = new Formatter() {
	public String format(LogRecord record) {
	    StringBuffer s = new StringBuffer();
	    s.append(record.getLevel().getLocalizedName());
	    s.append(' ');
	    if (record.getLoggerName() != null) {
		s.append('[');
		s.append(record.getLoggerName());
		s.append("]: ");
	    }
	    if (record.getSourceClassName() != null) {
		s.append("| ");
		if (record.getLevel().equals(Level.FINEST)) {
		    String className = record.getSourceClassName();
		    s.append(className.substring(Math.max(className.lastIndexOf('.')+1,0)));
		} else {
		    s.append(record.getSourceClassName());
		}
		s.append(' ');
	    }
	    if (record.getSourceMethodName() != null) {
		s.append("|  ");
		s.append(record.getSourceMethodName());
		s.append("( ");
		Object[] parms = record.getParameters();
		if (parms != null) {
		    for (int i = 0; i < parms.length; i++) {
			if (i != 0) {
			    s.append(", ");
			}
			s.append(parms[i]);
		    }
		}
		s.append(" ) ");
	    }
	    if (record.getThrown() != null) {
		s.append("| ");
		s.append(record.getThrown());
	    }
	    if (record.getMessage() != null) {
		s.append('|');
		s.append(record.getMessage());
	    }
	    s.append('\n');
	    return s.toString();
	}
                  };
              handler.setFormatter(formatter);
              logger.addHandler(handler);
          }
          catch (SecurityException e) {
          }
      }

      return logger;
  }
 
Example 20
Source File: LogUtilsTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void publish(LogRecord record) {
    cname = record.getSourceClassName();
    mname = record.getSourceMethodName();
}