如何解析原始日志文件转储并提取相关信息

时间:2013-02-06 07:44:41

标签: java logging

解析原始日志文件转储并提取相关信息。

以下是两个事件的日志文件摘录。每个活动都由 * * 分隔,如下所示:

01/23/13 17:29:25 |-| *****************************************************************
01/23/13 17:29:25 |-| MTCLPrint: processCodesoftPrintRequest() [-WEB--JU-LBCH-Wed Jan 23 17:29:25 CST 2013]...
01/23/13 17:29:25 |-|   ==> CS Label: [JU , LBCH , 70005023489 , JU Filtrete UPC0 3 Up Label , JU Filtrete UPC0 Labels 3 up , JUCHIZ01 , Qty: 1100]
01/23/13 17:29:26 |-| TrkgNbr: [-WEB--JU-LBCH-Wed Jan 23 17:29:25 CST 2013] ,CSPid: 5372
01/23/13 17:29:27 |-| Sending print job to -\\JUFP01\JUCHIZ01 [-WEB--JU-LBCH-Wed Jan 23 17:29:25 CST 2013]...
01/23/13 17:29:29 |-| Cannot delete file in temp dir (mergeAndPrint) - P786406707_67724_818342796.prn
01/23/13 17:29:29 |-| MTCLPrint: processCodesoftPrintRequest() [-WEB--JU-LBCH-Wed Jan 23 17:29:25 CST 2013]...complete.
01/23/13 17:29:29 |-| ~~~~ MTCL Print Web Service is terminated. ~~~~
01/23/13 17:29:56 |-| ~~~~ MTCL Print Web Service is started. ~~~~
01/23/13 17:29:56 |-| *****************************************************************
01/23/13 17:29:56 |-| MTCLPrint: processCodesoftPrintRequest() [-WEB--SDL-P1-Wed Jan 23 17:29:56 CST 2013]...
01/23/13 17:29:56 |-|   ==> CS Label: [SDL , P1 , 70000437403 , SDL GenericShip.CS7Z170 10-2006 REV , Medina HD Two Part Inter. Label , Z170 Packer , Qty: 1]
01/23/13 17:29:56 |-| TrkgNbr: [-WEB--SDL-P1-Wed Jan 23 17:29:56 CST 2013] ,CSPid: 8840
01/23/13 17:29:58 |-| Sending print job to -\\SPPRT10\SDL-PR-Zebra03 [-WEB--SDL-P1-Wed Jan 23 17:29:56 CST 2013]...
01/23/13 17:29:58 |-| Cannot delete file in temp dir (mergeAndPrint) - P1905794774_98669_986327948.prn
01/23/13 17:29:58 |-| MTCLPrint: processCodesoftPrintRequest() [-WEB--SDL-P1-Wed Jan 23 17:29:56 CST 2013]...complete.
01/23/13 17:29:58 |-| ~~~~ MTCL Print Web Service is terminated. ~~~~
01/23/13 17:30:11 |-| ~~~~ MTCL Print Web Service is started. ~~~~

应用程序日志会转储一系列类似的重复日志。我需要解析这个文件并通过Java程序在MS Excel中输入它。

以下是我需要从原始数据转储上方获取的相关内容:

01/23/13 17:29:25 |-|   ==> CS Label: [JU , LBCH , 70005023489 , JU Filtrete UPC0 3 Up Label , JU Filtrete UPC0 Labels 3 up , JUCHIZ01 , Qty: 1100]

01/23/13 17:29:56 |-|   ==> CS Label: [SDL , P1 , 70000437403 , SDL GenericShip.CS7Z170 10-2006 REV , Medina HD Two Part Inter. Label , Z170 Packer , Qty: 1]

此原始数据始终以“CS Label:”开头,后跟“[”,然后有7个以逗号分隔的字段。我需要将这七个字段提取到excel表中的列中。什么是最有效的方法来实现这一目标?

2 个答案:

答案 0 :(得分:1)

我看到以下算法:

  1. 一行一行地读取文件。
  2. 如果行包含CS Label字符串,则处理它(否则丢弃)。
  3. 提取方括号之间的所有内容(您可以使用正则表达式,或只使用普通String方法)。
  4. 在逗号上修剪此字符串,修剪空格。
  5. 将此信息添加到某个结构并发布到Excel。

答案 1 :(得分:1)

你走了:

  package one.tusk.stush.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.company.stush.R;

import java.util.List;

import one.tusk.stush.connect.Post;
import one.tusk.stush.views.PostListItem;

public class TimelineAdapter extends ArrayAdapter<Post> {

    private final LayoutInflater mInflater;


    public TimelineAdapter(Context context) {

        super(context, R.layout.list_item_post);
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



    public void setData(List<Post> data) {
        setNotifyOnChange(true);
        clear();
        if (data != null) {
            addAll(data);
            notifyDataSetChanged();
        }
        notifyDataSetChanged();
    }


    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) {

        Post post = getItem(position);
        PostListItem view;
        if (convertView == null) {
            view =  (PostListItem) mInflater.inflate(R.layout.list_item_post, parent, false);


        } else {
            view = (PostListItem) convertView;

        }
        view.setPost(post);
        notifyDataSetChanged();
        return view;
    }
}

然后格式化日志方法可以将日志放在Excel工作表中。

public class DateFunctions {
    // Date related Logic
    private Date startDate;
    private Integer delta;

    @SuppressWarnings("resource")
    public DateFunctions() {
        // ApplicationContext ctx = new
        // ClassPathXmlApplicationContext("date-properties.xml");
        ApplicationContext ctx = new FileSystemXmlApplicationContext(
                "c:\\logAnalyzer\\date-properties.xml");
        // Initializing date related data from spring configuration file
        startDate = (Date) ctx.getBean("startDate");
        delta = (Integer) ctx.getBean("delta");
    }

    public boolean isTimeDiffAcceptable(Date logDate) {

        DateTime dt1, dt2;
        Integer minutesDiff;

        dt1 = new DateTime(logDate);
        dt2 = new DateTime(startDate);

        int daysDiff = Days.daysBetween(dt1, dt2).getDays();

        if (daysDiff == 0) {
            minutesDiff = dt1.getMinuteOfDay() - dt2.getMinuteOfDay();
        } else {
            minutesDiff = 0;
        }

        // System.out.println("minutesDiff : " + minutesDiff);
        // System.out.println("delta : " + delta);

        if (minutesDiff < 0 && (-1 * minutesDiff) < delta) {
            return true;
        } else {
            return false;
        }
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Integer getDelta() {
        return delta;
    }

    public void setDelta(Integer delta) {
        this.delta = delta;
    }

}