html java 设置 style,使用html / css设置异常堆栈跟踪样式的Java库(Java library to style exception stacktrace with html...

使用html / css设置异常堆栈跟踪样式的Java库(Java library to style exception stacktrace with html / css)

我将异常的主体转换为字符串,然后将该异常通过电子邮件发送到java中的给定地址。 我想用html格式化异常字符串,使其成为人类可读的格式,类似于它在堆栈溢出时的显示方式。 我想知道Java中是否有任何库可以执行此操作?

StringWriter stringWriter = new StringWriter();

PrintWriter printWriter = new PrintWriter(stringWriter);

t.printStackTrace(printWriter);

String body = stringWriter.toString();

//add html to body here

setMessageBody(body);

为了详细说明,我的意思是将行与行分开,用不同颜色的字体显示类名,使用不同颜色的字体显示行号。 这可以用一些正则表达式来完成,但是我想知道是否有一个库可以直接使用。

I am converting the body of my exception to a string, and then emailing that exception to a given address in java. I want to format my exception string with html to make it a human readable format, similar to how it is displayed on stack overflow. I was wondering if there are any libraries in Java which do this?

StringWriter stringWriter = new StringWriter();

PrintWriter printWriter = new PrintWriter(stringWriter);

t.printStackTrace(printWriter);

String body = stringWriter.toString();

//add html to body here

setMessageBody(body);

To elabarate, I mean things like separate the line with
, display class name names with a different coloured font, display line numbers with a different coloured font. This can be done with some regular expressions but I was wondering if there is a library that does it out of the box.

原文:https://stackoverflow.com/questions/10380938

更新时间:2020-02-28 00:06

最满意答案

是的,有一对夫妇,他们甚至带有一个“免费”邮件后端:使用像slf4j或log4j这样的Java日志记录框架。

所有这些框架都可以生成带有一些配置的HTML电子邮件。 一般的方法是这样的:

为所有异常创建记录器或每个类创建一个记录器。 前者更容易配置,后者给你更多的自由

在此记录器中添加一个appender,配置为发送电子邮件

如果你不想在你的应用中使用日志记录,你可以编写你自己的appender来重用现有的格式化器。

Yes, there are a couple and they even come with a "free" mail backend: Use a Java logging framework like slf4j or log4j.

All these frameworks can produce HTML emails with a bit of configuration. The general approach is like this:

Create a logger for all exceptions or one logger per class. The former is easier to configure, the latter gives you more freedom

add an appender to this logger which is configured to send emails

If you don't want to use logging in your app, you can write your own appender which reuses the existing formatters.

相关问答

您的网址无效,因此无法找到您的CSS文件。 将其更改为: kit.getStyleSheet().importStyleSheet(new URL("file:///D:/mycssfile.css"));

或者更好的是,不是使用URL,而是将css文件添加到类路径中,然后将其作为资源加载,如下所示: kit.getStyleSheet().importStyleSheet(MyClassName.class.getResource("mycssfile.css"));

Your URL is

...

尝试使用一些CSS解析器(如http://cssparser.sourceforge.net/ )和SAC 。 Try using a some CSS parser (like http://cssparser.sourceforge.net/) and SAC.

使用jsoup和NodeTraversor从所有元素中删除类和样式属性 Document doc = Jsoup.parse(input);

NodeTraversor traversor = new NodeTraversor(new NodeVisitor() {

@Override

public void tail(Node node, int depth) {

if (node instanceof Element) {

Element e = (E

...

Error.stack由V8生成,它对源映射一无所知,但是如果你做console.log(error) DevTools将使用源映射替换引用,但zone.js包装原始错误并将堆栈提取为字符串,因此DevTools不能更换引用。 您可以尝试使用像sourcemapped-stacktrace这样的堆栈跟踪映射器。 Error.stack is generated by V8, which knows nothing about source maps, however if you do conso

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值