重定向输出流实现程序日志

public String delete() throws Exception{
  try {
   PrintStream out = System.out;//保存原输出流
   PrintStream ps = new PrintStream("E:/log.txt");//创建文件输出流 地址不正确则没办法存入
   System.setOut(ps);//设置使用新的输出流
   
   edocEntryService.delete(ede.getId());
   
   Date now = new Date();
   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
   String time = dateFormat.format( now );
   System.out.println(time);

   System.out.println("此操作删除了id为:" + ede.getId() + "的数据");
   System.setOut(out);//恢复原有输出流
   System.out.println("程序输出完毕,请查看日志文件");
   
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "del";
 }

发布了3 篇原创文章 · 获赞 8 · 访问量 8962

猜你喜欢

转载自blog.csdn.net/u011429828/article/details/40424831