新版本检测日志---------------

package com.email.service.impl;

import com.email.dao.LogViewDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 实时显示日志
 */
@Component
public class LogView2 {


    private Map<String, LogViewDao> logViewMap;

    @Value("${fileName}")
    private String fileName;



    /**
     * 实时输出日志信息
     *
     * @param logFile 日志文件
     * @throws IOException
     */
    public void realtimeShowLog(File logFile,String mapKey) throws IOException {
        LogViewDao logViewMaps1 = logViewMap.get(mapKey);
        //给文件增加读写权限
        final RandomAccessFile randomFile = new RandomAccessFile(logFile, "rw");
        //启动一个线程每5秒钟读取新增的日志信息
        ScheduledExecutorService exec = Executors.newScheduledThreadPool(2);
        exec.scheduleWithFixedDelay(new Runnable() {
            public void run() {
                try {
                    //不是第一次启动则报警
                    if (logViewMaps1.getCount()!=0){
                        //获得变化部分的
                        randomFile.seek(logViewMaps1.getLastTimeFileSize());
                        String tmp = "";
                        while ((tmp = randomFile.readLine()) != null) {
                            System.out.println(new String(tmp.getBytes("ISO8859-1")));
                        }
                    }else {
                        logViewMaps1.setCount(1);
                    }
                    //记录第一次启动程序时读取的文件长度
                    logViewMaps1.setLastTimeFileSize(randomFile.length());
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }, 0, 5, TimeUnit.SECONDS);

        //启动线程自动写入文件(模拟系统日志报错)
        exec.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    randomFile.seek(logViewMaps1.getLastTimeFileSize());
                    String msg=format.format(new Date())+"-----------------系统报错---------------"+mapKey;
                    //最后一行追加文件
                    randomFile.write(msg.getBytes());
                    //换行
                    randomFile.write("\r\n".getBytes());
                }catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        },1,10,TimeUnit.SECONDS);
    }
    //入口
    @PostConstruct
    public void Init(){
        try {
        String[] split = fileName.split(",");
        for (int i=0;i<split.length;i++) {
            logViewMap=new HashMap<>();
            logViewMap.put("log"+i,new LogViewDao());
            File tmpLogFile = new File(split[i]);
            realtimeShowLog(tmpLogFile,"log"+i);
        } } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值