使用Java如何监控并输出日志文件

发布时间:2020-11-24 15:39:59 作者:Leah
来源:亿速云 阅读:204

使用Java如何监控并输出日志文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

代码1:日志产生类

package com.bill99.seashell.domain.svr; 
 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.Writer; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledExecutorService; 
import java.util.concurrent.TimeUnit; 
/** 
 *<p>title: 日志服务器</p> 
 *<p>Description: 模拟日志服务器</p> 
 *<p>CopyRight: CopyRight (c) 2010</p> 
 *<p>Company: 99bill.com</p> 
 *<p>Create date: 2010-6-18</P> 
 *@author Tank Zhang<tank.zhang@99bill.com> 
 *@version v0.1 2010-6-18 
 */ 
public class LogSvr { 
  
 private SimpleDateFormat dateFormat = 
  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
 
 /** 
  * 将信息记录到日志文件 
  * @param logFile 日志文件 
  * @param mesInfo 信息 
  * @throws IOException 
  */ 
 public void logMsg(File logFile,String mesInfo) throws IOException{ 
  if(logFile == null) { 
   throw new IllegalStateException("logFile can not be null!"); 
  } 
  Writer txtWriter = new FileWriter(logFile,true); 
  txtWriter.write(dateFormat.format(new Date()) +"\t"+mesInfo+"\n"); 
  txtWriter.flush(); 
 } 
  
 public static void main(String[] args) throws Exception{ 
   
  final LogSvr logSvr = new LogSvr(); 
  final File tmpLogFile = new File("mock.log"); 
  if(!tmpLogFile.exists()) { 
   tmpLogFile.createNewFile(); 
  } 
  //启动一个线程每5秒钟向日志文件写一次数据 
  ScheduledExecutorService exec = 
   Executors.newScheduledThreadPool(1); 
  exec.scheduleWithFixedDelay(new Runnable(){ 
   public void run() { 
    try { 
     logSvr.logMsg(tmpLogFile, " 99bill test !"); 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 
   } 
  }, 0, 5, TimeUnit.SECONDS); 
 } 
} 

 代码2:显示日志的类

package com.bill99.seashell.domain.client;  
 
import java.io.File;  
import java.io.IOException;  
import java.io.RandomAccessFile;  
import java.util.concurrent.Executors;  
import java.util.concurrent.ScheduledExecutorService;  
import java.util.concurrent.TimeUnit;  
 
public class LogView {  
 private long lastTimeFileSize = 0; //上次文件大小  
 /** 
  * 实时输出日志信息 
  * @param logFile 日志文件 
  * @throws IOException 
  */ 
 public void realtimeShowLog(File logFile) throws IOException{  
  //指定文件可读可写  
  final RandomAccessFile randomFile = new RandomAccessFile(logFile,"rw");  
  //启动一个线程每10秒钟读取新增的日志信息  
  ScheduledExecutorService exec =  
   Executors.newScheduledThreadPool(1);  
  exec.scheduleWithFixedDelay(new Runnable(){  
   public void run() {  
    try {  
     //获得变化部分的  
     randomFile.seek(lastTimeFileSize);  
     String tmp = "";  
     while( (tmp = randomFile.readLine())!= null) {  
      System.out.println(new String(tmp.getBytes("ISO8859-1")));  
     }  
     lastTimeFileSize = randomFile.length();  
    } catch (IOException e) {  
     throw new RuntimeException(e);  
    }  
   }  
  }, 0, 1, TimeUnit.SECONDS);  
 }  
   
 public static void main(String[] args) throws Exception {  
  LogView view = new LogView();  
  final File tmpLogFile = new File("mock.log");  
  view.realtimeShowLog(tmpLogFile);  
 }  
 
} 

执行LogSvr类,LogSvr类会启动一个线程,每5秒钟向mock.log日志文件写一次数据,然后再执行LogView类,LogView每隔1秒钟读一次,如果数据有变化则输出变化的部分.

结果输出:

2010-06-19 17:25:54 99bill test !
2010-06-19 17:25:59 99bill test !
2010-06-19 17:26:04 99bill test !
2010-06-19 17:26:09 99bill test !
2010-06-19 17:26:14 99bill test !
2010-06-19 17:26:19 99bill test !

PS:

代码修改过, 有朋友下载了我的代码,说如果是中文会乱码,将日志输出类的第30行的代码 System.out.println(tmp)改成 System.out.println(new String(tmp.getBytes("ISO8859-1"))) ,就会正常显示中文.

看完上述内容,你们掌握使用Java如何监控并输出日志文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Python中写入训练日志文件并控制台输出的示例分析
  2. 怎么在Node.js中使用console输出日志文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java ava

上一篇:Android开发中怎么自定义一个视频录制功能

下一篇:如何在Android开发中使用DatePicker与TimePicke监听时间

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》