java解析日志文件,转为json数组,new Throwable() .getStackTrace()[0].getClassName()使用

此类包括获取上一天日志方法, 关闭文件流方法

技术点: 

JSONObject.fromObject('字符串');

JSONArray.add(JSONObject.fromObject('字符串'));

package com.log;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by macbook-pro-msw on 19/9/17.
 */
public class ReadTest {


    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private static final Logger logger = Logger.getLogger(new Throwable()
.getStackTrace()[0].getClassName());

    public static void main(String[] args) {
        System.out.println(sdf.format(getTime()));
        JSONArray jsonArray = resolveLogFile();
        System.out.println(jsonArray);

    }


        private static JSONArray resolveLogFile() {
            JSONArray array = new JSONArray();
            File file = new File("/Users/macbook-pro-msw/Documents/tomcat/apache-tomcat-8.5.45"
                    + "/logs/sys-optLog_" + sdf.format(getTime()) + ".log");
            if (file.exists()) {
                InputStreamReader read = null;
                BufferedReader bufferedReader = null;
                try {
                    read = new InputStreamReader(new FileInputStream(file));
                    bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while ((lineTxt = bufferedReader.readLine()) != null) {
                        if (StringUtils.isBlank(lineTxt)) {
                            continue;
                        }
                        byte[] bytes = lineTxt.getBytes("UTF-8");
                        lineTxt = new String(bytes, "UTF-8");
                        lineTxt = lineTxt.substring(lineTxt.indexOf("{"));
                        try {
                            array.add(JSONObject.fromObject(lineTxt));
                        } catch (Exception e) {
                            logger.info("解析optlog文件出错!" + e);
                        }
                    }
                } catch (Exception e) {
                    logger.info("解析optlog文件出错!" + e);
                } finally {
                    closeReaderStream(bufferedReader, read);
                }
            } else {
                logger.info("当日optlog文件不存在!");
            }
            return array;
        }
    private static Date getTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, -1);
        return cal.getTime();
    }

    private  static void closeReaderStream(BufferedReader bufferedReader,
                                   InputStreamReader read) {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                logger.info("解析optlog文件出错!" + e);
            }
        }
        if (read != null) {
            try {
                read.close();
            } catch (IOException e) {
                logger.info("解析optlog文件出错!" + e);
            }
        }
    }
}

日志文件: 

sys-optLog_2019-09-17.log 如下

{"name":

"张三","sex":"男","age":22} 

  

    a {"name":"张三","性别":"男","年龄":22}

你可能感兴趣的:(JAVA,mac,idea)