日志报错时发送邮件通知管理员

package com.email.service.impl;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

/**
 * 动态刷新nacos的检测日志路径
 */
@Configuration
@RefreshScope
@Data
public class FileNameConfig {
    @Value("${fileName}")
    private String fileName;
}

package com.email.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * nacos日志路径发送改变时重启程序
 */
@Slf4j
@Component
public class ScheduledLogUrl {

    @Autowired
    private FileNameConfig fileNameConfig;

    private String fileNameCompare;

    private Integer count=0;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private LogView logView2;

//    @Scheduled(cron = "0/1 * * * * ?")
    @Scheduled(fixedDelay = 1000)
    public void scheduledLogUrl(){
        try {
            if (count==0){//判断是否第一次启动
                //获取nacos配置的监听路径
                fileNameCompare=fileNameConfig.getFileName();
                count++;
                //启动程序
                logView2.Init();
            }else {
                //路径发送改变则重启程序
                if (!fileNameConfig.getFileName().equals(fileNameCompare)){
                    fileNameCompare=fileNameConfig.getFileName();
                    System.out.println("路径发生改变=================");
                    //发送关闭系统命令
                    stringRedisTemplate.opsForValue().set("isStatus","1");
                    //异步执行监听程序
                    ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,1, TimeUnit.HOURS,new SynchronousQueue<>(),new ThreadPoolExecutor.AbortPolicy());
                    executor.submit(new ThreadPoolExecutorDemo());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

package com.email.service.impl;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 异步启动程序
 */
@Component
public class ThreadPoolExecutorDemo implements Runnable{


    private StringRedisTemplate stringRedisTemplate;

    private LogView logView;

    @Override
    public void run() {
        try {
            while (true) {
                stringRedisTemplate=SpringContextUtils2.getApplicationContext().getBean(StringRedisTemplate.class);
                logView = SpringContextUtils2.getApplicationContext().getBean(LogView.class);
                String s = stringRedisTemplate.opsForValue().get("isStatus");
                if ("2".equals(s)){
                    //暂停一段时间,让程序停止
                    Thread.sleep(5000);
                    logView.Init();
                    System.out.println("监控系统启动中=======================");
                    return;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

package com.email.service.impl;

import com.email.dao.LogViewDao;
import com.email.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.cloud.context.config.annotation.RefreshScope;
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.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

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;

/**
 * 实时监测日志
 */
@RefreshScope
@Component
public class LogView {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private Map<String, LogViewDao> logViewMap;

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

    @Autowired
    private EmailService emailService;




    /**
     * 实时输出日志信息
     *
     * @param logFile 日志文件
     * @throws IOException
     */
    public void realtimeShowLog(File logFile,String mapKey) throws IOException {
        System.out.println("启动完成=======================");
        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) {
                            String s = new String(tmp.getBytes("ISO8859-1"));
                            System.out.println(s);
                            //发送邮件通知
                            emailService.sendEmail("程序异常警告",s);
                        }
                        String isStatus = stringRedisTemplate.opsForValue().get("isStatus");
                        if ("1".equals(isStatus)){
                            System.out.println("收到关闭命令=======================");
                            exec.shutdown();
                            randomFile.close();
                            stringRedisTemplate.opsForValue().set("isStatus","2");
                            return;
                        }
                    }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 (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        },1,10,TimeUnit.SECONDS);
    }
    //入口
    public void Init(){
        try {
            System.out.println("正在启动中=======================");
            String[] split = fileName.split(",");
            stringRedisTemplate.opsForValue().set("isStatus","0");
        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 (Exception e) {
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值