在Java中读取日志文件 [英] reading log file in java

查看:2302
本文介绍了在Java中读取日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Java读取日志文件(abc.log)?

Is it possible to read log files (abc.log) using java?

我要从我的日志文件中输入特定的字符串.

I want a specific string from my log file.

假设这是我的日志文件的内容.我只想要时间戳记(例如:05:08:37),然后将其打印在控制台上.

suppose this is the content of my logfile. I want the time stamp only (eg: 05:08:37) and print it the console.

2012-12-16 05:08:37,905 [Thread-1] INFO  com.submit.SubmitService - Wait time 500

2012-12-16 05:08:38,444 [Thread-1] INFO  com.submit.SubmitService - NO OF  RECORDS TILL NOW 3755    TOTAL TIME -- << 539

2012-12-16 05:08:38,668 [Thread-1] INFO  com.submit.SubmitService - Active Connection:: -69076

2012-12-16 05:08:38,670 [Thread-1] INFO  com.submit.SubmitService - Active Connection:: -65764

推荐答案

您可以将日志文件"读取为普通文件.

You can read your "log-file" as a normal file.

然后,您可以使用正则表达式来获取您需要的字符串部分:

Then you can use, for instance, regular expression, to obtain the part of the string that you need:

try{
   FileInputStream fstream = new FileInputStream("abc.log");
   BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
   String strLine;
   /* read log line by line */
   while ((strLine = br.readLine()) != null)   {
     /* parse strLine to obtain what you want */
     System.out.println (strLine);
   }
   fstream.close();
} catch (Exception e) {
     System.err.println("Error: " + e.getMessage());
}

这篇关于在Java中读取日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆