2013-03-14 37 views
9

Trong bộ kiểm tra dự án của tôi có sử dụng lớn củaRedirect System.out.println để đăng nhập

Tôi đang cố gắng để chuyển hướng các đầu ra để đăng nhập tập tin (thông qua cấu hình hoặc từ điểm duy nhất mà không cần refactoring toàn bộ dự án) để có thể tắt khi cần thiết để cải thiện hiệu suất. Tôi đang sử dụng log4j để đăng nhập. Có ai biết điều này có thể xảy ra không? nếu vậy làm thế nào để làm điều đó? Cảm ơn trước.

+1

trang này giải thích nó rất detailled: https://examples.javacodegeeks.com/core-java/lang/system/out/logging-system-println-results-log-file-example/ –

Trả lời

1

Sử dụng vỏ chuyển hướng. Tìm ra lời kêu gọi "java" cho dự án của bạn, nếu bạn đang sử dụng các hệ thống giống như UNIX, ps aux | grep java sẽ giúp.

Sau đó, chỉ cần chạy lệnh này bằng>/path/to/logfile. Ví dụ:

java -jar myjar.jar -cp path/to/lib.jar:path/to/otherlib.jar com.bigcorp.myproject.Main > /var/log/myproject.log 
+1

Không phải là câu hỏi. – peterh

2

Tôi nghĩ bạn có thể sử dụng System.setOut(PrintStream) để đặt đầu ra của bạn thành luồng đầu ra tệp. Sau đó, bạn có thể đặt dòng này trong phương thức BeforeClass của bạn. Tôi thích sử dụng một lớp BaseTest và đặt dòng mã này vào phương thức beforeclass của lớp đó. Sau đó, làm cho tất cả các trường hợp thử nghiệm mở rộng lớp này.

0
public class RecursiveLogging { 
public static void main(String[] args) { 
    System.setOut(new PrintStream(new CustomOutputStream())); 

    TestMyException.testPrint(); 
} 

} 

class CustomOutputStream extends OutputStream { 
private Logger logger = Logger.getLogger(this.getClass()); 

@Override 
public final void write(int b) throws IOException { 
    // the correct way of doing this would be using a buffer 
    // to store characters until a newline is encountered, 
    // this implementation is for illustration only 
    logger.info((char) b); 
} 

@Override 
public void write(byte[] b, int off, int len) throws IOException { 
    if (b == null) { 
     throw new NullPointerException(); 
    } else if ((off < 0) || (off > b.length) || (len < 0) || 
       ((off + len) > b.length) || ((off + len) < 0)) { 
     throw new IndexOutOfBoundsException(); 
    } else if (len == 0) { 
     return; 
    } 
    byte[] pb = new byte[len]; 
    for (int i = 0 ; i < len ; i++) { 
     pb[i] = (b[off + i]); 
    } 
    String str = new String(pb); 
    logger.info(str); 
} 
} 
3

Vì nó thay thế tốt hơn System.out.println(), đôi khi chúng tôi không có sự lựa chọn. Dù sao tôi đã thực hiện một ít tiện ích cho điều đó:

SystemOutToSlf4j.enableForClass(MyClass.class) 

Sau đó tất cả println có nguồn gốc từ MyClass sẽ được chuyển hướng đến trình ghi nhật ký. See this post for more details...

public class SystemOutToSlf4j extends PrintStream { 

    private static final PrintStream originalSystemOut = System.out; 
    private static SystemOutToSlf4j systemOutToLogger; 

    /** 
    * Enable forwarding System.out.println calls to the logger if the stacktrace contains the class parameter 
    * @param clazz 
    */ 
    public static void enableForClass(Class clazz) { 
    systemOutToLogger = new SystemOutToSlf4j(originalSystemOut, clazz.getName()); 
    System.setOut(systemOutToLogger); 
    } 


    /** 
    * Enable forwarding System.out.println calls to the logger if the stacktrace contains the package parameter 
    * @param packageToLog 
    */ 
    public static void enableForPackage(String packageToLog) { 
    systemOutToLogger = new SystemOutToSlf4j(originalSystemOut, packageToLog); 
    System.setOut(systemOutToLogger); 
    } 

    /** 
    * Disable forwarding to the logger resetting the standard output to the console 
    */ 
    public static void disable() { 
    System.setOut(originalSystemOut); 
    systemOutToLogger = null; 
    } 

    private String packageOrClassToLog; 

    private SystemOutToSlf4j(PrintStream original, String packageOrClassToLog) { 
    super(original); 
    this.packageOrClassToLog = packageOrClassToLog; 
    } 

    @Override 
    public void println(String line) { 
    StackTraceElement[] stack = Thread.currentThread().getStackTrace(); 
    StackTraceElement caller = findCallerToLog(stack); 
    if (caller == null) { 
     super.println(line); 
     return; 
    } 

    org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(caller.getClass()); 
    log.info(line); 
    } 

    public StackTraceElement findCallerToLog(StackTraceElement[] stack) { 
    for (StackTraceElement element : stack) { 
     if (element.getClassName().startsWith(packageOrClassToLog)) 
     return element; 
    } 

    return null; 
    } 

} 
Các vấn đề liên quan