2016-10-01 205 views
0

我正在尝试使用Java Logger类将所有控制台输出写入日志文件。将日志控制台输出到日志文件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(); 
    } 

如何让这个需要一切已输出到控制台,并将其写入一个文件有效,而不是将logger.info(“不管是什么事情,我想输出“);无处不在?

回答

相关问题