java 日志

149 阅读2分钟

package top.to3.www;

import java.io.ByteArrayOutputStream; import java.io.Closeable;

import java.io.File;

import java.io.IOException;

import java.io.PrintStream;

import java.io.RandomAccessFile;

import java.text.SimpleDateFormat;

import java.util.Date;

/**

  • 日志输出工具

  • 可以输出到控制台和指定的文件中, 分为4个级别, 由低到高分别为: debug, info, warn, error

  • 输出级别:

  • <li> debug: 输出 debug, info, warn, error </li>
    
  • <li> info: 输出 info, warn, error </li>
    
  • <li> warn: 输出 warn, error </li>
    
  • <li> error: 输出 error </li>
    
  • 默认为 info 输出级别

  • Demo:

  • // (可选) 设置日志输出级别, 默认为 INFO 级别
    
  • Log.setLogOutLevel(Log.Level.DEBUG);
    
  • // (可选) 设置日志输出文件(追加到文件尾部)
    
  • Log.setLogOutFile(new File("MyLog.log"));
    
  • // (可选) 设置日志输出位置(是否输出到控制台 和 是否输出到文件), 默认只输出到控制台, 不输出到文件
    
  • Log.setLogOutTarget(true, true);
    
  • // 输出日志
    
  • Log.debug("TAG", "The debug log.");
    
  • Log.info("TAG", "The info log.");
    
  • Log.warn("TAG", "The warn log.");
    
  • Log.error("TAG", "The error log.");
    
  • @author KC

*/

public class KCLog {

/** 每条 Log 的 message 输出的最大长度, 超过部分将被截断 */

private static final int MESSAGE_MAX_LENGTH = 1024;

/** 日期前缀格式化 */

private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss.SSS");

/** 日志当前的输出级别, 默认为 INFO 级别 */

private static Level logOutLevel = Level.INFO;

/** 是否输出到控制台, 默认输出 */

private static boolean isOutToConsole = true;

/** 是否输出到文件 */

private static boolean isOutToFile = false;

/** 日志输出文件, 追加到文件尾 */

private static File logOutFile;

/** 日志文件输出流, 追加到文件尾  */

private static RandomAccessFile logOutFileStream;



public static void main(String[] args) throws Exception {

    // (可选) 设置日志输出级别, 默认为 INFO 级别

	KCLog.setLogOutLevel(KCLog.Level.DEBUG);

    // (可选) 设置日志输出文件(追加到文件尾部)

	KCLog.setLogOutFile(new File("MyLog.log"));

    // (可选) 设置日志输出位置(是否输出到控制台 和 是否输出到文件), 默认只输出到控制台, 不输出到文件

	KCLog.setLogOutTarget(true, true);

    // 输出日志

	KCLog.debug("The debug log.");

	KCLog.info("The info log.");

	KCLog.warn("The warn log.");

	KCLog.error("The error log.");

}



public static void setLogOutLevel(Level currentLevel) {

    if (currentLevel == null) {

        currentLevel = Level.INFO;

    }

    KCLog.logOutLevel = currentLevel;

}



public static synchronized void setLogOutFile(File logOutFile) throws IOException {

	KCLog.logOutFile = logOutFile;

    if (logOutFileStream != null) {

        closeStream(logOutFileStream);

        logOutFileStream = null;

    }   

    if (KCLog.logOutFile != null) {

        try {

            logOutFileStream = new RandomAccessFile(KCLog.logOutFile, "rw");

            logOutFileStream.seek(KCLog.logOutFile.length());

        } catch (IOException e) {

            closeStream(logOutFileStream);

            logOutFileStream = null;

            throw e;

        }

    }

}



public static void setLogOutTarget(boolean isOutToConsole, boolean isOutToFile) {

	KCLog.isOutToConsole = isOutToConsole;

	KCLog.isOutToFile = isOutToFile;

}



public static void debug(String message) {

    printLog(Level.DEBUG, message, false);

}



public static void info(String message) {

    printLog(Level.INFO,message, false);

}



public static void warn(String message) {

    printLog(Level.WARN, message, false);

}



public static void error(String message) {

    printLog(Level.ERROR, message, true);

}



public static void error(Exception e) {

    if (e == null) {

        error((String) null);

        return;

    }

    PrintStream printOut = null;

    try {

        ByteArrayOutputStream bytesBufOut = new ByteArrayOutputStream();

        printOut = new PrintStream(bytesBufOut);

        e.printStackTrace(printOut);

        printOut.flush();

        error(new String(bytesBufOut.toByteArray(), "UTF-8"));

    } catch (Exception e1) {

        e1.printStackTrace();

    } finally {

        closeStream(printOut);

    }

}



private static void printLog(Level level, String message, boolean isOutToErr) {

    if (level.getLevelValue() >= logOutLevel.getLevelValue()) {