好用的日志输出工具类,java编写的

mac2024-06-16  36

最近在做数据的转换处理,过程需要查看处理过程,所以要记录日志,就闲着来写了个日志工具类练练手,觉得用着还可以,贴出来,有需要的小朋友,可以直接拿去用:

package com.project.handlesapdata.material.syncMaterial;

import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Date;

/**  *   * @author wangwuji  * 2019年10月31日   * description: 日志输出工具类  */ public class LogUtil {

    public static void main(String[] args) {         LogUtil logUtil = new LogUtil();         logUtil.setMaxLogsize(1);         while (logUtil.fileIndex<2) {             logUtil.info("yyyy-MM-dd-HH-mm-ss yyyy-MM-dd-HH-mm-ss yyyy-MM-dd-HH-mm-ss");         }          logUtil.setWriteTimeONConsole(true);         logUtil.refreshLogFileStorePathConfig("d:/logs/dd");         while (logUtil.fileIndex<10) {             logUtil.info("yyyy-MM-dd-HH-mm-ss yyyy-MM-dd-HH-mm-ss yyyy-MM-dd-HH-mm-ss");         }      }

    int fileIndex = 1;     private String formatStr = "yyyy-MM-dd HH-mm-ss";     private SimpleDateFormat sdfDirName = new SimpleDateFormat("yyyyMMdd HHmmss");     private SimpleDateFormat sdfConsoleOupt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");     private String logFileStorePath=null;     private String logPath=null;     private File logFile = null;     private String originFileName="log.txt";     private BufferedWriter bw = null;     private boolean isWriteTimeONConsole=false;     private boolean isCreateDirParentWithTime=true;     private long maxSize=1024*1024*5;//文件最大大小,单位字节,大于这个数字后生成第二个日志文件

    public LogUtil() {         initLogConfig();     }          public LogUtil(String logFileStorePath) {         this.logFileStorePath=logFileStorePath;         initLogConfig();     }          public LogUtil(String logFileStorePath,String originFileName) {         this(logFileStorePath);         this.originFileName=originFileName;         initLogConfig();     }          public LogUtil(String logFileStorePath,String originFileName,boolean isWriteTimeONConsole) {         this(logFileStorePath,originFileName);         this.isWriteTimeONConsole=isWriteTimeONConsole;         initLogConfig();     }     public LogUtil(String logFileStorePath,String originFileName,             boolean isWriteTimeONConsole,boolean isCreateDirParentWithTime) {         this(logFileStorePath,originFileName,isWriteTimeONConsole);         this.isCreateDirParentWithTime=isCreateDirParentWithTime;         initLogConfig();     }          public LogUtil(String logFileStorePath,String originFileName,             boolean isWriteTimeONConsole,boolean isCreateDirParentWithTime,long maxSize) {         this(logFileStorePath,originFileName,isWriteTimeONConsole,isCreateDirParentWithTime);         this.maxSize=maxSize;         initLogConfig();     }          private void initLogConfig() {         initFormatStr(this.formatStr);         initLogFileStorePath(this.logFileStorePath);         initLogFile();     }          

    private void initFormatStr(String formatStr){         sdfDirName = new SimpleDateFormat(formatStr);     }          private void initLogFileStorePath(String logFileStorePath){         if(logFileStorePath==null){             String currentAbsolutePath=this.getClass().getResource("").getPath();//当前class根路径             String curDiskRootPath=currentAbsolutePath.substring(1,4);//当前磁盘分区根路径             logFileStorePath = curDiskRootPath+"/logs";         }         if(isCreateDirParentWithTime){             logPath = logFileStorePath + "/" + sdfDirName.format(new Date());         }else{             logPath = logFileStorePath;         }     }     private void initLogFile(){         File parentDirOflogFile = new File(logPath);         if (!parentDirOflogFile.exists()) {             parentDirOflogFile.mkdirs();         }         logFile = new File(logPath + "/"+this.originFileName);         try {             if (!logFile.exists()) {                 logFile.createNewFile();             }             Writer out = new FileWriter(logFile, true);             bw = new BufferedWriter(out);         } catch (IOException e) {             e.printStackTrace();         }     }

    private void createNewFile() throws IOException {         String fileName=originFileName.substring(0,originFileName.lastIndexOf("."));         String subfix = originFileName.substring(originFileName.lastIndexOf("."), originFileName.length());         String newName = fileName + fileIndex + subfix;         logFile = new File(logPath + "/" + newName);         logFile.createNewFile();         Writer out = new FileWriter(logFile, true);         bw = new BufferedWriter(out);         fileIndex++;     }          public void refreshLogConfig() {         this.initLogConfig();     }          public void refreshDateFormatConfig(String formatStr) {         sdfDirName = new SimpleDateFormat(formatStr);     }          public void refreshLogFileStorePathConfig(String logFileStorePath) {         initLogFileStorePath(logFileStorePath);         initLogFile();     }

    private void writeToDoc(String str) {         try {             if (logFile.length() > maxSize) {                 createNewFile();             }             bw.write(str);             bw.newLine();             bw.flush();         } catch (IOException e) {             e.printStackTrace();         }     }          public void newLine() {         try {             bw.newLine();             bw.flush();         } catch (IOException e) {             e.printStackTrace();         }

    }          public String debug(String str) {         String outputInfo=null;         if(isWriteTimeONConsole){             String time = sdfConsoleOupt.format(new Date());             outputInfo=time + ": " + str;         }else{             outputInfo=str;         }         System.out.println(outputInfo);         return outputInfo;     }          public void info(String str) {         writeToDoc(debug(str));     }          private void setFormatStr(String formatStr){         this.formatStr=formatStr;     }

    public void setLogFileStorePath(String logFileStorePath) {         this.logFileStorePath = logFileStorePath;     }          public String getLogFileStorePath() {         return logPath;     }          public void setOriginFileName(String originFileName) {         this.originFileName=originFileName;     }          public String getOriginFileName() {         return  originFileName;     }          public void setMaxLogsize(int size){         maxSize=1024*1024*size;     }          public long getMaxLogsize(){         return maxSize;     }

    public boolean isCreateDirParentWithTime() {         return isCreateDirParentWithTime;     }

    public void setCreateDirParentWithTime(boolean isCreateDirParentWithTime) {         this.isCreateDirParentWithTime = isCreateDirParentWithTime;     }

    public boolean isWriteTimeONConsole() {         return isWriteTimeONConsole;     }

    public void setWriteTimeONConsole(boolean isWriteTimeONConsole) {         this.isWriteTimeONConsole = isWriteTimeONConsole;     } }  

 

代码中我封装了debug和info两个级别的输出,debug只输出到控制台,info输出到控制台同时输出到日志文件中。main方法中有调用的例子,需要的小伙伴,拿去用吧。有好的改进意见也可以提出来评论下,一块儿改进。

最新回复(0)