使用Java如何监控并输出日志文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。代码1:日志产生类package com.bill99.seashell.domain.svr;
使用Java如何监控并输出日志文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
代码1:日志产生类
package com.bill99.seashell.domain.svr; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFORMat; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class LogSvr { private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public void logMsg(File logFile,String mesInfo) throws IOException{ if(logFile == null) { throw new IllegalStateException("logFile can not be null!"); } Writer txtWriter = new FileWriter(logFile,true); txtWriter.write(dateFormat.format(new Date()) +"\t"+mesInfo+"\n"); txtWriter.flush(); } public static void main(String[] args) throws Exception{ final LogSvr logSvr = new LogSvr(); final File tmpLogFile = new File("mock.log"); if(!tmpLogFile.exists()) { tmpLogFile.createNewFile(); } //启动一个线程每5秒钟向日志文件写一次数据 ScheduledExecutorService exec = Executors.newScheduledThreadPool(1); exec.scheduleWithFixedDelay(new Runnable(){ public void run() { try { logSvr.logMsg(tmpLogFile, " 99bill test !"); } catch (IOException e) { throw new RuntimeException(e); } } }, 0, 5, TimeUnit.SECONDS); } }
--结束END--
本文标题: 使用Java如何监控并输出日志文件
本文链接: https://www.lsjlt.com/news/225420.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0