ในขณะนี้รายการเริ่มต้นมีลักษณะดังนี้:
Oct 12, 2008 9:45:18 AM myClassInfoHere
INFO: MyLogMessageHereฉันต้องทำอย่างไร?
Oct 12, 2008 9:45:18 AM myClassInfoHere - INFO: MyLogMessageHereชี้แจงฉันใช้ java.util.logging
ในขณะนี้รายการเริ่มต้นมีลักษณะดังนี้:
Oct 12, 2008 9:45:18 AM myClassInfoHere
INFO: MyLogMessageHereฉันต้องทำอย่างไร?
Oct 12, 2008 9:45:18 AM myClassInfoHere - INFO: MyLogMessageHereชี้แจงฉันใช้ java.util.logging
คำตอบ:
สำหรับ Java 7, java.util.logging.SimpleFormatterรองรับการรับรูปแบบจากคุณสมบัติระบบดังนั้นการเพิ่มสิ่งนี้ลงในบรรทัดคำสั่ง JVM จะทำให้พิมพ์ในบรรทัดเดียว:
-Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'หรือคุณสามารถเพิ่มสิ่งนี้ในlogger.properties:
java.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'logging.propertiesด้วยการดัดแปลงตามคำแนะนำของ @BrunoEberhard และชื่อคนตัดไม้แบบสั้น:  java.util.logging.SimpleFormatter.format=%1$tF %1$tT %4$.1s %2$s %5$s%6$s%n
                    -Djava.util.logging.SimpleFormatter.formatJava 7 สนับสนุนคุณสมบัติที่มีjava.util.Formatterไวยากรณ์สตริงรูปแบบ
-Djava.util.logging.SimpleFormatter.format=... ดูที่นี่ .
สิ่งที่ฉันชอบคือ:
-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%nซึ่งทำให้ผลลัพธ์เช่น:
2014-09-02 16:44:57 SEVERE org.jboss.windup.util.ZipUtil unzip: Failed to load: foo.zipโดยทั่วไป IDE จะให้คุณตั้งค่าคุณสมบัติระบบสำหรับโปรเจ็กต์ เช่นใน NetBeans แทนที่จะเพิ่ม -D ... = ... ที่ใดที่หนึ่งให้เพิ่มคุณสมบัติในกล่องโต้ตอบการดำเนินการในรูปแบบjava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-...- โดยไม่มีเครื่องหมายคำพูดใด ๆ IDE ควรคิดออก
เพื่อความสะดวกของคุณนี่คือวิธีการนำไปที่ Surefire:
        <!-- Surefire -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <systemPropertyVariables>
                    <!-- Set JUL Formatting -->
                    <java.util.logging.SimpleFormatter.format>%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n</java.util.logging.SimpleFormatter.format>
                </systemPropertyVariables>
            </configuration>
        </plugin>ฉันมีห้องสมุดที่มีไม่กี่java.util.loggingชั้นเรียนที่เกี่ยวข้อง SingleLineFormatterในหมู่พวกเขาก็ ขวดที่สามารถดาวน์โหลดได้ที่นี่
public class SingleLineFormatter extends Formatter {
  Date dat = new Date();
  private final static String format = "{0,date} {0,time}";
  private MessageFormat formatter;
  private Object args[] = new Object[1];
  // Line separator string.  This is the value of the line.separator
  // property at the moment that the SimpleFormatter was created.
  //private String lineSeparator = (String) java.security.AccessController.doPrivileged(
  //        new sun.security.action.GetPropertyAction("line.separator"));
  private String lineSeparator = "\n";
  /**
   * Format the given LogRecord.
   * @param record the log record to be formatted.
   * @return a formatted log record
   */
  public synchronized String format(LogRecord record) {
    StringBuilder sb = new StringBuilder();
    // Minimize memory allocations here.
    dat.setTime(record.getMillis());    
    args[0] = dat;
    // Date and time 
    StringBuffer text = new StringBuffer();
    if (formatter == null) {
      formatter = new MessageFormat(format);
    }
    formatter.format(args, text, null);
    sb.append(text);
    sb.append(" ");
    // Class name 
    if (record.getSourceClassName() != null) {
      sb.append(record.getSourceClassName());
    } else {
      sb.append(record.getLoggerName());
    }
    // Method name 
    if (record.getSourceMethodName() != null) {
      sb.append(" ");
      sb.append(record.getSourceMethodName());
    }
    sb.append(" - "); // lineSeparator
    String message = formatMessage(record);
    // Level
    sb.append(record.getLevel().getLocalizedName());
    sb.append(": ");
    // Indent - the more serious, the more indented.
    //sb.append( String.format("% ""s") );
    int iOffset = (1000 - record.getLevel().intValue()) / 100;
    for( int i = 0; i < iOffset;  i++ ){
      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();
  }
}คล้ายกับ Tervor แต่ฉันชอบเปลี่ยนคุณสมบัติบนรันไทม์
โปรดทราบว่าสิ่งนี้จำเป็นต้องตั้งค่าก่อนที่จะสร้าง SimpleFormatter แรก - ตามที่เขียนไว้ในความคิดเห็น
    System.setProperty("java.util.logging.SimpleFormatter.format", 
            "%1$tF %1$tT %4$s %2$s %5$s%6$s%n");เช่นเดียวกับที่ Obediah Stane กล่าวว่าจำเป็นต้องสร้างformatวิธีการของคุณเอง แต่ฉันจะเปลี่ยนบางสิ่ง:
สร้างคลาสย่อยที่ได้รับมาโดยตรงจากไม่ได้มาจากFormatter มีอะไรจะเพิ่มอีกต่อไปSimpleFormatterSimpleFormatter
ระวังการสร้างDateวัตถุใหม่! คุณควรตรวจสอบให้แน่ใจว่าได้แสดงวันที่ของไฟล์LogRecord. เมื่อสร้างใหม่Dateด้วยตัวสร้างเริ่มต้นมันจะแสดงวันที่และเวลาที่FormatterประมวลผลLogRecordไม่ใช่วันที่LogRecordสร้างขึ้น
คลาสต่อไปนี้สามารถใช้เป็นตัวจัดรูปแบบใน a Handlerซึ่งสามารถเพิ่มลงในไฟล์Logger. โปรดทราบว่าจะละเว้นข้อมูลคลาสและวิธีการทั้งหมดที่มีอยู่ในไฟล์LogRecord.
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public final class LogFormatter extends Formatter {
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    @Override
    public String format(LogRecord record) {
        StringBuilder sb = new StringBuilder();
        sb.append(new Date(record.getMillis()))
            .append(" ")
            .append(record.getLevel().getLocalizedName())
            .append(": ")
            .append(formatMessage(record))
            .append(LINE_SEPARATOR);
        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) {
                // ignore
            }
        }
        return sb.toString();
    }
}นี่คือสิ่งที่ฉันใช้
public class VerySimpleFormatter extends Formatter {
    private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
    @Override
    public String format(final LogRecord record) {
        return String.format(
                "%1$s %2$-7s %3$s\n",
                new SimpleDateFormat(PATTERN).format(
                        new Date(record.getMillis())),
                record.getLevel().getName(), formatMessage(record));
    }
}คุณจะได้รับสิ่งที่ต้องการ ...
2016-08-19T17:43:14.295+09:00 INFO    Hey~
2016-08-19T17:43:16.068+09:00 SEVERE  Seriously?
2016-08-19T17:43:16.068+09:00 WARNING I'm warning you!!!ofNullable(record.getThrown()).ifPresent(v -> v.printStackTrace());ก่อนส่งคืนข้อความที่จัดรูปแบบ
                    
ต่อภาพหน้าจอใน Eclipse ให้เลือก "run as" จากนั้น "Run Configurations ... " และเพิ่มคำตอบจาก Trevor Robinson ด้วยเครื่องหมายคำพูดคู่แทนเครื่องหมายคำพูด หากคุณพลาดเครื่องหมายคำพูดคู่คุณจะได้รับข้อผิดพลาด "ไม่พบหรือโหลดคลาสหลัก"
ฉันคิดหาวิธีที่ใช้ได้ผลแล้ว คุณสามารถ subclass SimpleFormatter และแทนที่วิธีการจัดรูปแบบ
    public String format(LogRecord record) {
        return new java.util.Date() + " " + record.getLevel() + " " + record.getMessage() + "\r\n";
    }แปลกใจเล็กน้อยที่ API นี้ฉันคิดว่าจะมีฟังก์ชัน / ความยืดหยุ่นมากขึ้นให้นอกกรอบ
formatMessage(record) record.getMessage()ผู้ถือสถานที่จะไม่เปลี่ยนแปลง
                    การบันทึกนี้เป็นข้อมูลเฉพาะสำหรับแอปพลิเคชันของคุณไม่ใช่คุณลักษณะทั่วไปของ Java คุณกำลังใช้งานแอปพลิเคชันใดอยู่
อาจเป็นไปได้ว่าสิ่งนี้มาจากไลบรารีการบันทึกเฉพาะที่คุณใช้ภายในรหัสของคุณเอง ถ้าใช่โปรดโพสต์รายละเอียดว่าคุณใช้อันไหน
java.util.logging.Logger
                    หากคุณเข้าสู่เว็บแอปพลิเคชันโดยใช้ tomcat add:
-Djava.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatterเกี่ยวกับอาร์กิวเมนต์ VM
หากคุณใช้ java.util.logging แสดงว่ามีไฟล์คอนฟิกูเรชันที่ทำสิ่งนี้เพื่อบันทึกเนื้อหา (เว้นแต่คุณจะใช้การกำหนดค่าแบบเป็นโปรแกรม) ดังนั้นตัวเลือกของคุณคือ
1) เรียกใช้โพสต์ - โปรเซสเซอร์ที่ลบตัวแบ่งบรรทัด
2) เปลี่ยนการกำหนดค่าบันทึกและลบตัวแบ่งบรรทัดออก รีสตาร์ทแอปพลิเคชันของคุณ (เซิร์ฟเวอร์) และคุณควรจะดี