简单好用日志封装

时间:2024-01-26 20:40:31
上代码,简单好用!

package com.wonders.superbatch.utils;

import org.apache.log4j.Logger;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;

/**
* @Author: zwj
* @Date: 2020/4/21 16:22
* @Version 1.0
*
* 日志记录类
*/
public class LogUtils implements Serializable {

private static Logger log = Logger.getLogger(LogUtils.class);

/**
* 是否开启Debug
*/
public static boolean isDebug = log.isDebugEnabled();
public static boolean isinfo = log.isInfoEnabled();

private static String debug = "debug";
private static String info = "info";
private static String error = "error";
private static String warn = "warn";



/*** 获取输出信息的代码的位置 ***/
private static String location = "";

public LogUtils(Class<? extends Object> clazz) {

}

/**
* 获取行号和传入类的信息
*/
private static StackTraceElement[] stacks = Thread.currentThread().getStackTrace();



/**
* Debug 输出
* @param
* @param
*/
public static void debug(Object object){
if(!isDebug)
return ;
try {
String location = getLocation(object,debug);
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
}


/**
* warn 输出
*/
public static void warn(Object object){

try {
String location = getLocation(object,warn);
}catch (Exception e){
// TODO: handle exception
e.printStackTrace();
}
}

/**
* 异常填充值输出
*/
public static void info(Object object){

if(!isinfo)
return ;
try {
String location = getLocation(object,info);

}catch (Exception e){

// TODO: handle exception
e.printStackTrace();
}
}

/**
* 异常填充值输出
*/
public static void error(Object object){

try {

String location = getLocation(object,error);

}catch (Exception e){

// TODO: handle exception
e.printStackTrace();
}

}

/**
* 异常填充输出
*/
public static void fmtError(Object object) {

}


public static String getLocation(Object object,String type){


location = stacks[2].getClassName() + "." + stacks[2].getMethodName