java日志记录错误的文件_方法_行号_报错信息

1、java日志记录错误的文件、方法、行号、报错信息

 StackTraceElement s= e.getStackTrace()[0];

1.1、记录保存的文件s.getFileName()

1.2、记录保存的方法s.getMethodName()

1.3、记录报错的行号 s.getLineNumber()

1.4、记录报错的信息(不全面) e.getMessage()

1.5、互利报错的类名字 e.getClassName()

1.6、打印详细的堆栈信息 logger.error("错误堆栈", e);

获取堆栈信息,如果直接打印e,则会将它toString


/**
     * 获取堆栈信息
      * @param throwable
     * @return
     */
    public static String getStackTrace(Throwable throwable){
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try
        {
            throwable.printStackTrace(pw);
            return sw.toString();
        } finally
        {
            pw.close();
        }
    }

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Desc: 异常打印日志 ,提供给打印非正常异常
 * @Author HealerJean
 * @Date 2018/8/21  下午6:41.
 */

    public static void log(Throwable e,Class c){
        Logger logger = LoggerFactory.getLogger(c);
//        logger.error("错误堆栈", e);

        StackTraceElement s= e.getStackTrace()[0];//数组长度为 1
        logger.error("\n\n-----------------"+
                    "\n报错文件名:"+s.getFileName()+
                    "\n报错的类:"+s.getClassName()+
                    "\n报错方法::"+s.getMethodName()+
                    "\n报错的行:"+ s.getLineNumber()+
                    "\n报错的message:"+ e.getMessage()+
                    "\n错误堆栈:\n"+getStackTrace(e)+
                    "\n------------------\n\n");
      }


1.2、使用


    public static void main(String[] args) {

        try {
            int i =1/0 ;
        }catch (Exception e){
            log(e,ExceptionLogUtils.class );
        }


    }
    
    
11:40:53.732 [main] ERROR com.duodian.youhui.admin.utils.ExceptionLogUtils - 

-----------------
报错文件名:ExceptionLogUtils.java
报错的类:com.duodian.youhui.admin.utils.ExceptionLogUtils
报错方法::main
报错的行:68
报错的message:/ by zero
错误堆栈:
java.lang.ArithmeticException: / by zero
	at com.duodian.youhui.admin.utils.ExceptionLogUtils.main(ExceptionLogUtils.java:68)

------------------

2、打印变量日志

log.info("cacheMsg RECUR_FIGHT A:  roomId = {}, userId = {}, toUid = {}", room.getRoomId(), ctx.getUserId(), toUid);

3、info打印更多的信息

3.1、打印方法


   public static void logInfo(String msg,Class c){
        Logger logger = LoggerFactory.getLogger(c);
        String location="";
        StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
        System.out.println(stacks.length); //长度为3
        for(StackTraceElement stackTraceElement:stacks){
            logger.info("\n\n**************"+
                    "\n打印文件名:"+stackTraceElement.getFileName() +
                    "\n打印类名:"+ stackTraceElement.getClassName() +
                    "\n方法名:" +  stackTraceElement.getMethodName() +
                    "\n行号:"  +  stackTraceElement.getLineNumber() +
                    "\n打印内容:"+msg+
                    "\n**************\n\n");
            System.out.println(location);

        }

    }
    
    

3.2、测试

1、是当前线程,2、当前打印所在方法,3、是调用该方法的类,那么我们需要的就是第三个


    public static void main(String[] args) {
        logInfo("HealerJean",ExceptionLogUtils.class);
    }
    
    
3
11:44:47.685 [main] INFO com.duodian.youhui.admin.utils.ExceptionLogUtils - 

**************
打印文件名:Thread.java
打印类名:java.lang.Thread
方法名:getStackTrace
行号:1559
打印内容:HealerJean
**************



11:44:47.689 [main] INFO com.duodian.youhui.admin.utils.ExceptionLogUtils - 

**************
打印文件名:ExceptionLogUtils.java
打印类名:com.duodian.youhui.admin.utils.ExceptionLogUtils
方法名:logInfo
行号:31
打印内容:HealerJean
**************



11:44:47.689 [main] INFO com.duodian.youhui.admin.utils.ExceptionLogUtils - 

**************
打印文件名:ExceptionLogUtils.java
打印类名:com.duodian.youhui.admin.utils.ExceptionLogUtils
方法名:main
行号:49
打印内容:HealerJean
**************

1和3总结


package com.duodian.youhui.admin.utils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @Desc: 异常打印日志 ,提供给打印非正常异常
 * @Author HealerJean
 * @Date 2018/8/21  下午6:41.
 */
@Slf4j
public class ExceptionLogUtils {



    public static void log(Throwable e,Class c){
        Logger logger = LoggerFactory.getLogger(c);
//        logger.error("错误堆栈", e);

        StackTraceElement s= e.getStackTrace()[0];//数组长度为 1
        logger.error("\n\n-----------------"+
                    "\n报错文件名:"+s.getFileName()+
                    "\n报错的类:"+s.getClassName()+
                    "\n报错方法::"+s.getMethodName()+
                    "\n报错的行:"+ s.getLineNumber()+
                    "\n报错的message:"+ e.getMessage()+
                    "\n错误堆栈:\n"+getStackTrace(e)+
                    "\n------------------\n\n");
      }

    /**
     * 获取堆栈信息
      * @param throwable
     * @return
     */
    public static String getStackTrace(Throwable throwable){
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try
        {
            throwable.printStackTrace(pw);
            return sw.toString();
        } finally
        {
            pw.close();
        }
    }


    public static void logInfo(String msg,Class c){
        Logger logger = LoggerFactory.getLogger(c);
        StackTraceElement[] stacks = Thread.currentThread().getStackTrace();//数组长度为 3
        logger.info("\n\n**************"+
                "\n打印文件名:"+stacks[2].getFileName() +
                "\n打印类名:"+ stacks[2].getClassName() +
                "\n方法名:" +  stacks[2].getMethodName() +
                "\n行号:"  +  stacks[2].getLineNumber() +
                "\n打印内容:"+msg+
                "\n**************\n\n");
    }

}




4、打印变量


log.info("admin[{}],[{}],[{}];请求地址:[{}];访问ip:[{}]", CasConfig.RemoteUserUtil.getRemoteUserId(),CasConfig.RemoteUserUtil.getRemoteUserAccount(),CasConfig.RemoteUserUtil.getRemoteUserName(),request.getRequestURL(), IpUtil.getIp());

ContactAuthor

你可能感兴趣的:(Log)