我们如何在java中将行号打印到日志中 [英] How can we print line numbers to the log in java

查看:188
本文介绍了我们如何在java中将行号打印到日志中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将行号打印到日志中。假设在向日志输出一些信息时,我还想打印输出在源代码中的行号。正如我们在堆栈跟踪中看到的,它显示发生异常的行号。异常对象上有堆栈跟踪。

How to print line numbers to the log. Say when outputting some information to the log, I also want to print the line number where that output is in the source code. As we can see in the stack trace, it displays the line number where the exception has occurred. Stack trace is available on the exception object.

其他替代方法可能就像打印到日志时手动包含行号一样。还有其他办法吗?

Other alternative could be like manually including the line number when printing to the log. Is there any other way?

推荐答案

来自 Angsuman Chakraborty

From Angsuman Chakraborty:

/** Get the current line number.
 * @return int - Current line number.
 */
public static int getLineNumber() {
    return Thread.currentThread().getStackTrace()[2].getLineNumber();
}

这篇关于我们如何在java中将行号打印到日志中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆