自己写的java日志类和方法代码分享
时间:2021-08-18 19:34:13
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;
public class AndyLogger
{
//The defaulted root path of SSLVPN installation
private static String rootPath = "C:\\temp2";
//variable for creating new line
private final static String enter = System.getProperty("line.separator");
private static SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public static synchronized void log(String fileName, String logMessage)
{
try
{
File folder = new File(rootPath);
if(!folder.exists())
{
folder.mkdir();
}
File file = new File(rootPath + "\\" + fileName + ".log");
if(!file.exists())
{
file.createNewFile();
}
BufferedReader in = new BufferedReader(new FileReader(file));
String str = "";
String strToal = "";
while ((str = in.readLine()) != null)
{
strToal += (str + enter);
}
strToal = strToal + (sdf.format(new Date()) + " " + logMessage + enter);
in.close();
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(strToal);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static synchronized void log(String fileName, String[] logMessages)
{
try
{
File folder = new File(rootPath);
if(!folder.exists())
{
folder.mkdir();
}
File file = new File(rootPath + "\\" + fileName + ".log");
if(!file.exists())
{
file.createNewFile();
}
BufferedReader in = new BufferedReader(new FileReader(file));
String str = "";
String strToal = "";
while ((str = in.readLine()) != null)
{
strToal += (str + enter);
}
for (int i=0; i < logMessages.length ; i++)
{
String logMessage = logMessages[i];
strToal = strToal + (sdf.format(new Date()) + " " + logMessage + enter);
}
in.close();
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(strToal);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
AndyLogger.log("bug223", "timeisjjja");
String[] logMessages = {"111","222","333"};
AndyLogger.log("bug223", logMessages);
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot的@Value给静态变量注入application.properties属性值
详解Spring中的FactoryBean
SpringMVC bean加载控制的实现分析
![](https://img.aspxhome.com/file/2023/2/57912_0s.png)
Java面试题冲刺第二十三天--分布式
![](https://img.aspxhome.com/file/2023/8/58588_0s.png)
详解mybatis generator代码生成器的使用
![](https://img.aspxhome.com/file/2023/3/64943_0s.png)
java简单实现复制 粘贴 剪切功能代码分享
OpenCV和C++实现图像的翻转(镜像)、平移、旋转、仿射与透视变换
![](https://img.aspxhome.com/file/2023/3/95523_0s.jpg)
Java Idea高效率配置技巧实例解析
![](https://img.aspxhome.com/file/2023/7/61407_0s.jpg)
Java的Struts框架简介与环境配置教程
![](https://img.aspxhome.com/file/2023/8/58728_0s.jpg)
浅谈java中OO的概念和设计原则(必看)
Mybatis-Plus之ID自动增长的设置实现
![](https://img.aspxhome.com/file/2023/7/65767_0s.png)
SpringTask实现定时任务方法讲解
![](https://img.aspxhome.com/file/2023/1/62681_0s.png)
flutter日期选择器 flutter时间选择器
![](https://img.aspxhome.com/file/2023/8/90158_0s.jpg)
快速学习六大排序算法
![](https://img.aspxhome.com/file/2023/5/105175_0s.gif)
微信公众号 网页授权登录及code been used解决详解
![](https://img.aspxhome.com/file/2023/8/65768_0s.png)
Java版画板的实现方法
![](https://img.aspxhome.com/file/2023/8/64948_0s.jpg)
使用Feign扩展包实现微服务间文件上传
![](https://img.aspxhome.com/file/2023/5/63085_0s.jpg)
浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题
![](https://img.aspxhome.com/file/2023/9/64029_0s.jpg)