记录tomcat服务器开启关闭时间
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("服务器开启"); } }
结果