记录tomcat服务器开启关闭时间

news/2024/7/6 13:28:17 标签: java

1、IO流

package com.zy.exercise;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * Application Lifecycle Listener implementation class Mylistener
 *
 */
@WebListener
public class Mylistener implements ServletContextListener {
    public void contextDestroyed(ServletContextEvent sce)  { 
        
        DestroyedWrite();
        System.out.println("关闭日志写入成功");
        
         
    }

    public void contextInitialized(ServletContextEvent sce)  { 
        InitializedWrite();
        System.out.println("启动日志写入成功");
        
    }
    
    public void InitializedWrite(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss E");
        String time = sdf.format(date);
        String s=time+"\t执行操作:"+"启动服务器"+"\r\n";
        File file = new File("E:\\Tomcat关闭启动日志\\tomcat日志.txt");
        try {
            FileOutputStream fos = new FileOutputStream(file, true);
            byte[] b = s.getBytes();
            fos.write(b);
        } catch (FileNotFoundException e) {            
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }
    public void DestroyedWrite(){
        
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss E");
        String time = sdf.format(date);
        String s=time+"\t执行操作:"+"关闭服务器"+"\r\n";
        File file = new File("E:\\Tomcat关闭启动日志\\tomcat日志.txt");
        try {
            FileOutputStream fos = new FileOutputStream(file, true);
            byte[] b = s.getBytes();
            fos.write(b);
        } catch (FileNotFoundException e) {            
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

}

结果

2、使用log4j

log4j.properties文件

 

log4j.rootLogger=INFO, ServerDailyRollingFile, stdout
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=D://myLog.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss E} - %m%n
log4j.appender.ServerDailyRollingFile.Append=true

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss E} %p [%c] %m%n

 

过滤器

package com.zy.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.apache.log4j.Logger;

/**
 * Servlet Filter implementation class EncodingFile
 */
@WebFilter("/*")
public class EncodingFile implements Filter {
    Logger log = Logger.getLogger(this.getClass());
    
    public void destroy() {
        log.info("服务器关闭");
    }

    
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        /*     /*拦截所有请求,先执行该过滤器
         * */    
        chain.doFilter(request, response);//一定放行
    }

    
    public void init(FilterConfig fConfig) throws ServletException {
        log.info("服务器开启");
        
    }

}

结果

转载于:https://www.cnblogs.com/qfdy123/p/11180333.html


http://www.niftyadmin.cn/n/1493278.html

相关文章

graph embedding源码阅读

文章目录LINEstruc2vec计算结构距离计算每个结点的有序度序列遍历边表的顶点,每个都计算有序度序列LINE 看到 ge.models.line.LINE#_gen_sampling_table node_degree 表示出度 归一化后,得到顶点的分布norm_prob,然后算alias_table 构建al…

ORM到底如何使用了?

公司最近一个项目用到了实体类,借助其他成熟的框架实现了orm,可是在使用过程中却对于多表查询支持并不好。nbear看上去orm挺不错的,有空得研究下看看代码转载于:https://www.cnblogs.com/ocean2000/archive/2007/06/13/781755.html

Kubeflow调研

文章目录Kubeflow基本概念创建Kubeflow的Component通过Yaml定义在代码中创建在Jupyter中创建Kubeflow的架构TensorFlow Training 的支持 (TFJob)与 Jupyter Notebook 的整合KFServing以解决一个分子属性预测问题为例,阐述如何用kubeflow实现Kubeflow 的总结Kubeflow…

利用consul在spring boot中实现最简单的分布式锁

因为在项目实际过程中所采用的是微服务架构,考虑到承载量基本每个相同业务的服务都是多节点部署,所以针对某些资源的访问就不得不用到用到分布式锁了。 这里列举一个最简单的场景,假如有一个智能售货机,由于机器本身的原因不能同一…

Oracle常用结构或函数使用笔记一

With用法: The WITH query_name clause lets you assign a name to a subquery block. You can then reference the subquery block multiple places in the query by specifying the query name. Oracle optimizes the query by treating the query name as either…

*在scanf printf中的作用

在网上看到的&#xff0c;贴过来记录一下 http://www.cppleyuan.com/viewthread.php?tid9428 一个有趣的打印菱形的程序 1 #include <stdio.h>2 3 int line 1;4 int main()5 {6 printf("%*s\n",7 7-(line>4? line-4: 4-line),8 …

牛客算法题

字节题库 NC78 反转链表 NC78 反转链表 迭代法 class Solution:# 返回ListNodedef ReverseList(self, pHead):ppHeadpreNonewhile p:aftp.nextp.nextprepreppaftreturn pre递归法 class Solution:# 返回ListNodedef ReverseList(self, pHead):if not pHead or not pHead.n…

中台之上(六):如何为一个商业银行设计业务架构?

从实际操作的角度讲&#xff0c;企业级业务架构设计及其建模过程是一个充满可能性和争议的过程&#xff0c;并没有一个直观的量化标准能够用于判断一个架构方案的好坏&#xff0c;我们可以通过一个虚拟的例子体会一下。 假定我们为A商业银行设计企业级业务架构&#xff0c;为了…