日志收集并写入硬盘文件


1.作用

  以我个人认为日志的作用就是用于收集操作信息,便于后续的继续操作,就像虚拟机的挂起,来保存虚拟机各项任务的状态,能够准确的输出错误的时间,位置,便于程序员解决问题,查看状况。


2.准备

   2.1:创建本地文件写入工具类

public class FileUtils {

    //把指定的内容写入指定的路径

    /**
     *
     * @param path 比如  c:\\logs\\
     * @param content 要保存内容
     */
    public static void writeFile(String path,String content){
        Date now = new Date();
        SimpleDateFormat dateFormatMinute= new SimpleDateFormat("yyyy-MM-dd");
        //年月日
        String  ymd = dateFormatMinute.format( now );

        SimpleDateFormat dateFormatSecond= new SimpleDateFormat("yyyy-MM-dd-HH-mm");
        //小时分钟
        String  minute = dateFormatSecond.format( now );
        //目录
        File dir = new File(path+ymd+"\\");
        //具体文件
        File file = new File(path+ymd+"\\"+minute+".log");
        //判断日期文件夹是否存在,不存在的话创建
        if (!dir.isDirectory()) {
            dir.mkdir();
        }
        //判断文件是否存在,不存在的话创建
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            out.write(content);
            out.newLine();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

   2.2:创建注解类,指定其作用位置以及作用时间

@Target(ElementType.METHOD)//作用在方法上
@Retention(RetentionPolicy.RUNTIME)//运行期间执行
public @interface MyAnn {
    String value() default "";
}

   2.3:创建一个切面类

//这里我并没有进行用户的session存储,所以等以后再进行补充
@Component//被spring扫描到 @Aspect//切面类 public class MyAop { @Autowired KafkaTemplate kafkaTemplate; @Autowired HttpSession session; @Around(value = "@annotation(com.zcb.utils.MyAnn)") public Object sendMessage(ProceedingJoinPoint pjp) throws Throwable { //调用目标方法 Object proceed = pjp.proceed(); //1.调用的类名 String name = pjp.getTarget().getClass().getName(); //2.调用的方法名 String menthodName = pjp.getSignature().getName(); Date date = new Date(); //3.时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); //4.任务 String msg = "用户:游客调用"+name+"方法"+menthodName+"时间"+simpleDateFormat.format(date); System.out.println(msg); kafkaTemplate.sendDefault("logs",msg); return proceed; } }

   2.4:创建消费者接收发送的消息

//消费者监听器
public class MyMessageListener implements AcknowledgingMessageListener {

    /**
     * @author: zcb
     * @description: TODO
     * @date: 2021-12-16 16:34
     * @param data
     * @param acknowledgment
     * @return void
     */
    @Override
    public void onMessage(ConsumerRecord data, Acknowledgment acknowledgment) {
        String key = data.key();
        String value = data.value();
        if(key.equals("logs")){
        //写入d盘此路径下,要先创建此文件夹 FileUtils.writeFile("d:\\logs\\",value);
        //手动确定消息 acknowledgment.acknowledge(); } } }

3.配置文件

如果你的注解用在了service层需要加入


   

同样的,写在controller也需要springmvc.xml文件中有此配置

  实例:

@MyAnn
    @RequestMapping("/update")
    public String update(Menu menu){
        kafkaTemplate.sendDefault("Update", JSON.toJSONString(menu));
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "redirect:list";
    }