服务器之家

服务器之家 > 正文

自己写的java日志类和方法代码分享

时间:2019-11-04 14:16     来源/作者: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);
    }

}

 

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
返回顶部