自己写的java日志类和方法代码分享
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);
}
}
相关文章
Java编程之jdk1.4,jdk1.5和jdk1.6的区别分析(经典)
这篇文章主要介绍了Java编程之jdk1.4,jdk1.5和jdk1.6的区别分析,结合实例形式较为详细的分析说明了jdk1.4,jdk1.5和jdk1.6版本的使用区别,需要的朋友可以参考下2015-12-12SpringBoot+Mybatis实现Mapper接口与Sql绑定几种姿势
通常我们在使用Mybatis进行开发时,会选择xml文件来写对应的sql,然后将Mapper接口与sql的xml文件建立绑定关系,然后在项目中调用mapper接口就可以执行对应的sql,感兴趣的可以学习一下2021-09-09Spring中的@RestControllerAdvice注解使用解析
这篇文章主要介绍了Spring中的@RestControllerAdvice注解使用解析,@RestControllerAdvice 是 Spring 框架中一个用于统一处理控制器异常和返回结果的注解,它可以被用来定义全局异常处理程序和全局响应结果处理程序,需要的朋友可以参考下2024-01-01
最新评论