2016-10-01 12 views
0

Java Loggerクラスを使用して、すべてのコンソール出力をログファイルに書き込もうとしています。それはコンソールに出力され、効率的ではなくlogger.infoを置くよりも、それをファイルに書き込みされていますすべてをとるように私はそれを作るにはどうすればよいコンソール出力をログファイルに記録するJavaロガー

public static Logger logger = Logger.getLogger("Log"); 
public static FileHandler fh; 
public static ConsoleHandler handler = new ConsoleHandler(); 
public static DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); 
public static Calendar cal = Calendar.getInstance(); 

/** 
* Launch the application. 
* @param args 
* @throws IOException 
*/ 

/* -- START MAIN -- */ 
public static void main(String[] args) throws IOException { 
    Settings.settingsRead(); 

    try { 
     // This block configure the logger with handler and formatter 
     fh = new FileHandler("logs/hudedit_log_" + dateFormat.format(cal.getTime()) + ".log"); 
     logger.setLevel(Level.ALL); 
     handler.setFormatter(new SimpleFormatter()); 
     logger.addHandler(handler); 
     logger.info("hello world"); 
     logger.addHandler(fh); 
     SimpleFormatter formatter = new SimpleFormatter(); 
     fh.setFormatter(formatter); 

     logger.info("Test"); 

    } catch (SecurityException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

(「私は出力したいものは何でもの事");どこでも?

答えて

0

私はLog4jを使うべきだと思います。

http://logging.apache.org/log4j/2.x/

それは、このルーチンのために非常にplexibleです。

+0

入力していただきありがとうございます。やってみます。 – Xyfurion

関連する問題