Giter VIP home page Giter VIP logo

Comments (8)

looly avatar looly commented on June 10, 2024

Hutool的log使用第三方Log框架,并根据引入包自动识别。

请检查第三方日志配置的info和error是否打开。

from hutool.

Mr-LiuDC avatar Mr-LiuDC commented on June 10, 2024

Hutool的log使用第三方Log框架,并根据引入包自动识别。

请检查第三方日志配置的info和error是否打开。

我截图里面是能够正常打印各个级别的日志,我问的是为什么没有异常堆栈信息,throw 的异常或者其他未捕获的未知异常都没有打印异常堆栈信息。

from hutool.

looly avatar looly commented on June 10, 2024

@Mr-LiuDC sorry,理解错了。

要打印堆栈,需要传入Exception对象:

log.error(e, msg);

from hutool.

Mr-LiuDC avatar Mr-LiuDC commented on June 10, 2024

你说的传入 Exception 对象是没问题的,下面这行没有打印堆栈是什么问题呢?

throw new RuntimeException(msg);

from hutool.

Mr-LiuDC avatar Mr-LiuDC commented on June 10, 2024

我举个例子,我在 Action 里面用 DBUtil 进行数据库的读写操作,当报了异常,如果没有手动进行 catch 异常并手动打印异常,控制台居然没有打印任何异常信息。

from hutool.

looly avatar looly commented on June 10, 2024

@Mr-LiuDC 终于明白你的意思了。

在Action中抛出异常是会被吃掉的。

这个原因是 com. sun. net. httpserverHttpHandler的创建处理机制并不抛出异常,此时需要你自定义com.sun.net.httpserver.Filtercn.hutool.http.server.filter.HttpFilter

/**
 * 异常处理过滤器
 *
 * @author looly
 */
public abstract class ExceptionFilter implements HttpFilter {

	@Override
	public void doFilter(final HttpServerRequest req, final HttpServerResponse res, final Filter.Chain chain) {
		try {
			chain.doFilter(req.getHttpExchange());
		} catch (final Throwable e) {
			afterException(req, res, e);
		}
	}

	/**
	 * 异常之后的处理逻辑
	 *
	 * @param req {@link HttpServerRequest}
	 * @param res {@link HttpServerResponse}
	 * @param e   异常
	 */
	public abstract void afterException(final HttpServerRequest req, final HttpServerResponse res, final Throwable e);
}

from hutool.

looly avatar looly commented on June 10, 2024

这块6.x做的比较完善,5.8.8会增加ExceptionFilterDefaultExceptionFilter

这样有了异常会打印在页面中:

public class ExceptionServerTest {
	public static void main(final String[] args) {
		HttpUtil.createServer(8888)
			.addFilter(new DefaultExceptionFilter())
			.addAction("/", (req, res) -> {
				throw new RuntimeException("Test Exception");
			})
			.start();
	}
}

image

from hutool.

Mr-LiuDC avatar Mr-LiuDC commented on June 10, 2024

@looly 非常感谢你的耐心解答。

from hutool.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.