问题描述
当我使用log4j时,我可以创建一个扩展org.apache.log4j.HTMLLayout的新类,并将其覆盖以创建新的日志格式.使用log4j2时,我只能使用布局,而不能覆盖它.在我的log4j2.xml中写
When I use log4j, I can create a new class extends org.apache.log4j.HTMLLayout and override it to make a new format of the log. When using log4j2, I can only use the layout but not override it. In my log4j2.xml writing
<Appenders>
<File name="log" fileName="D:\log.html" append="false">
<HTMLLayout/>
</File>
</Appenders>
在log4j中,我可以使用layout class ="log.FormatHTMLLayout"(log.FormatHTMLLayout是我的新类,它扩展了HTMLLayout),但是现在我只能使用HTMLLayout.有什么方法可以覆盖HTMLayout?我需要做很多事情,例如更改输出表,标题等.
in log4j I may use layout class="log.FormatHTMLLayout" (log.FormatHTMLLayout is my new class which extends the HTMLLayout), but now I can only use HTMLLayout.Is there any way to override the HTMLayout? I need to do a lot of things, like changing the output table, the title and so on.
推荐答案
这就是我所做的,我需要添加< img>由log4j2生成的日志中的HTML标签以及默认情况下的HTML元素(如<,>,)将被转义字符(如lt;,gt;,quot)替换.(参考: https://issues.apache.org/jira/browse/LOG4J2-439 )
Here is what i did,I needed to add <img> HTML tags in logs generated by log4j2 and by default HTML elements like <, >, " are replaced by escape characters like lt; , gt; , quot;.(Ref:https://issues.apache.org/jira/browse/LOG4J2-439)
所以我只是从源代码中复制了整个HTMLLayout类,可从以下网站获得 log4j-core/src/main/java/org/apache/logging/log4j/core/layout
So i just copied whole HTMLLayout class from source, which is available atlog4j-core / src / main / java / org / apache / logging / log4j / core / layout
并将其名称更改为"CustomHTMLLayout",并在需要时进行更新(您可以选择任何名称),现在您的自定义布局类与HTMLLayout类一样好.
and changed its name to "CustomHTMLLayout" and updated it wherever required (you can choose any name), now your custom layout class is as good as HTMLLayout class.
有一种名为 toSerializable 的方法,其中包含每个记录的实际格式,因此您可以根据需要进行操作.
there is method called toSerializable which contains actual formatting of each record, so you can manipulate it as per your need.
一旦类被修改,您需要在log4j2.html中提供新的布局如下:
once class is modified, you need to provide the new layout in your log4j2.htmlas following:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.redknee.bssauto.helpers">
<Appenders>
<RollingFile name="Rolling-default" fileName="logs/bssauto.html" filePattern="logs/$${date:yyyy-MM}/bssauto-%d{MM-dd-yyyy}-%i.log.gz">
<CustomHTMLLayout charset="UTF-8" title="BSSAuto Logs" locationInfo="true" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Rolling-default"/>
</Root>
</Loggers>
</Configuration>
注意事项
<Configuration packages="com.bssauto.helpers">
此处,程序包应包含所有包含布局自定义类的程序包.因此,这里 com.bssauto.helpers 是程序包,在该程序包下我具有CustomHTMLLayout类.
here packages should have all packages containing custom class for layouts. so here com.bssauto.helpers is package under which i have CustomHTMLLayout class.
<CustomHTMLLayout charset="UTF-8" title="BSSAuto Logs" locationInfo="true" />
和 CustomHTMLLayout 是通过扩展AbstractStringLayout
and CustomHTMLLayout is custom layout class created by extending AbstractStringLayout
确保您使用的是最新的log4j2版本,我使用的是 log4j 2.2
Make sure you are using latest log4j2 version, I used log4j 2.2
这篇关于如何在log4j2中更改htmllayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!