Java 8 LogRecord - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 LogRecord class.

This code example shows how to use the following methods:getLevel, getLoggerName, getMessage, getParameters, getThrown
     * juli handler that Redirect to log4j 
     */ 
    public static class JuliToLog4jHandler extends Handler {
 
        public void publish(LogRecord record) {
            org.apache.log4j.Logger log4j = getTargetLogger(record.getLoggerName());
            Priority priority = toLog4j(record.getLevel());
            log4j.log(priority, toLog4jMessage(record), record.getThrown());
        } 
 
        static org.apache.log4j.Logger getTargetLogger(String loggerName) {
            return org.apache.log4j.Logger.getLogger(loggerName);
        } 
 
        public static org.apache.log4j.Logger getTargetLogger(Class<?> clazz) {
            return getTargetLogger(clazz.getName());
        } 
 
        private String toLog4jMessage(LogRecord record) {
            String message = record.getMessage();

8
CodeRank
This code example shows how to use the following methods:getLevel, getLoggerName, getMillis, getThreadID, getThrown
   * @return a formatted log record 
   */ 
  @Override 
  public synchronized String format(final LogRecord record) {
    buffer.setLength(PREFIX.length());
    buffer.append(timestampFormatter.format(new Date(record.getMillis())));
    buffer.append(' ');
    buffer.append(levelNumberToCommonsLevelName(record.getLevel()));
    if (longForm) {
      buffer.append(" [");
      buffer.append(record.getThreadID());
      buffer.append("] ");
      buffer.append(record.getLoggerName());
    } 
    buffer.append(SUFFIX);
    buffer.append(formatMessage(record)).append('\n');
    if (record.getThrown() != null) {
      final StringWriter trace = new StringWriter();
      record.getThrown().printStackTrace(new PrintWriter(trace));
      buffer.append(trace);

8
CodeRank
This code example shows how to use the following methods:getLevel, getMessage, getThrown
 */ 
public class LogFormat extends Formatter {
    @Override 
    public String format(LogRecord record) {
        StringBuilder text = new StringBuilder();
        Level level = record.getLevel();
 
        if (level == Level.FINEST) {
            text.append("[FINEST] ");
        } else if (level == Level.FINER) {
            text.append("[FINER] ");
        } else if (level == Level.FINE) {
            text.append("[FINE] ");
        } else if (level == Level.INFO) {
            text.append("[INFO] ");
        } else if (level == Level.WARNING) {
            text.append("[WARNING] ");
        } else if (level == Level.SEVERE) {
            text.append("[SEVERE] ");
        } 

8
CodeRank
This code example shows how to use the following methods:setLoggerName, setSourceClassName, setThrown
  private void log(Level level, Object message, Throwable t) {
    if (!logger.isLoggable(level)) {
      return; 
    } 
    String msg = (message == null) ? "NULL" : message.toString();
    LogRecord record = new LogRecord(level, msg);
    record.setLoggerName(logger.getName());
    record.setThrown(t);
    // This will disable stack trace lookup inside JUL. If someone wants location info, they can use their own formatter 
    // or use a different logging framework like sl4j, or log4j 
    record.setSourceClassName(null);
 
    logger.log(record);
  } 
} 
8
CodeRank
This code example shows how to use the following methods:getMessage, getThrown
  @Override 
  public void publish(LogRecord record) {
    Formatter formatter = getFormatter();
    String message;
    if (formatter == null) {
      message = record.getMessage();
    } else { 
      message = formatter.format(record);
    } 
    Throwable throwable = record.getThrown();
    if (throwable == null) {
      context.log(message);
    } else { 
      context.log(message, throwable);
    } 
  } 
 
  @Override 
  public void flush() { 
    // do nothing 
8
CodeRank
This code example shows how to use the following methods:getLevel, getLoggerName, getMillis, getThrown
    // MCPC+ end 
 
    public String format(LogRecord record)
    { 
        StringBuilder msg = new StringBuilder();
        msg.append(this.dateFormat.format(Long.valueOf(record.getMillis())));
        Level lvl = record.getLevel();
 
        String name = lvl.getLocalizedName();
        if ( name == null )
        { 
            name = lvl.getName();        	
        } 
 
        if ( ( name != null ) && ( name.length() > 0 ) )
        { 
            msg.append(" [" + name + "] ");
        } 
        else 
        { 

8
CodeRank
This code example shows how to use the following methods:getLevel, getMessage, getMillis, getSourceClassName, getThrown
 
public class LoggerShortFormat extends java.util.logging.Formatter {
  @Override 
  @Nonnull 
  public String format(@Nonnull final LogRecord record) {
    return record.getMillis() + " " +
        record.getLevel() + " [" +
        record.getSourceClassName() + "] " +
        record.getMessage() +
        (record.getThrown() != null ? " Stack trace:\n" + getStackTrace(record.getThrown()) : "") + "\n";
  } 
 
  public static void intialize() throws Exception { 
    InputStream input = null;
    try { 
      input = LoggerShortFormat.class.getClassLoader().getResourceAsStream("logging.properties");
      LogManager.getLogManager().readConfiguration(input);
    } finally { 
      if (input != null) {
        input.close();
8
CodeRank
This code example shows how to use the following methods:setLoggerName, setMillis, setThreadID
        Locale.US,
        TimeZone.getTimeZone("America/Los_Angeles"));
    threadIdToCommandId.put(64738L, "testCommandId");
    LogRecord record = logRecord(Level.INFO, "Test", "testLogger", 64738, 1409072580000L);
    assertEquals( 
        logFormatter.format(record),
        "[2014-08-26 10:03:00.000][info ][command:testCommandId][tid:64738][testLogger] Test\n"); 
  } 
 
  private static LogRecord logRecord(
      Level level,
      String message,
      String loggerName,
      int tid,
      long millis) {
    LogRecord result = new LogRecord(level, message);
    result.setLoggerName(loggerName);
    result.setMillis(millis);
    result.setThreadID(tid);
    return result;

7
CodeRank
This code example shows how to use the following methods:getMessage, getThrown
     * Holds the formatter for all Android log handlers. 
     */ 
    private static final Formatter THE_FORMATTER = new Formatter() {
        @Override 
        public String format(LogRecord r) { 
            Throwable thrown = r.getThrown(); 
            if (thrown != null) { 
                StringWriter sw = new StringWriter(); 
                PrintWriter pw = new PrintWriter(sw); 
                sw.write(r.getMessage()); 
                sw.write("\n"); 
                thrown.printStackTrace(pw); 
                pw.flush(); 
                return sw.toString(); 
            } else { 
                return r.getMessage(); 
            } 
        } 
    }; 
 
    /** 
7
CodeRank
This code example shows how to use the following methods:getMessage, setMessage
    } 
 
    @Override 
    public void log(LogRecord logRecord)
    { 
        logRecord.setMessage( pluginName + logRecord.getMessage() );
        ProxyServer.getInstance().getLogger().log( logRecord );
    } 
} 
7
CodeRank
See Code Examples for Java 8 LogRecord Methods: