zoukankan      html  css  js  c++  java
  • 自己写的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);
        }

    }

  • 相关阅读:
    Mac电脑maven安装与配置
    解决Mac OS X中IDEA启动慢以及debug卡死问题
    如何在Mac上启用root用户或更改root密码
    mac文本编辑器软件,五大适用于Mac修订的文本编辑器,nodepad++替代软件
    mac系统到10.14以上,navicat无法打开,一直显示已损坏解决办法
    mac苹果电脑AppleID注册或者登录appstore时提示:您没有完整填写表格,请输入您的出生年份的解决方法
    mac苹果电脑使用相关,开发环境配置指南(持续更新)
    bitmap to base64
    Multiple actions were found that match the request in Web Api
    vue get attribute value
  • 原文地址:https://www.cnblogs.com/kungfupanda/p/2605023.html
Copyright © 2011-2022 走看看