有人可以告诉我,如何为FileAppender配置自定义布局?
有谁能告诉我,如何为FileAppender配置自定义布局? 我创建了HTMLLayout的副本并在那里进行了一些更改(它不能扩展,因为它是最终的类)现在我想使用这个布局,但我不知道如何:(
以下列配置显示此错误: 错误文件包含无效的元素或属性“ibtrader.log4j2.MYHTMLLayout”
这是我的log4j2.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration strict="true" monitorInterval="30">
<appenders>
<appender name="Console" type="Console" target="SYSTEM_OUT">
<layout type="PatternLayout" pattern="%highlight{%d{ISO8601} [%t] %-5level %logger{36} - %msg%n}" />
</appender>
<appender name="DEBUG_FILE" type="File" fileName="logs/errors.txt" >
<layout type="PatternLayout" pattern="%d{ISO8601} [%t] %-5level %logger{36} - %msg%n}" />
</appender>
<appender name="HTMLAppender" type="File" fileName="logs/mainlog.html">
<layout type="ibtrader.log4j2.MYHTMLLayout" charset="UTF-8" title="IBTRader logs" locationInfo="true" />
</appender>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="Console"/>
<appender-ref ref="DEBUG_FILE" level="WARN" />
<appender-ref ref="HTMLAppender" />
</root>
</loggers>
</configuration>
感谢您的帮助!
答案 0 :(得分:0)
评论提到这已经修复,但为了完整性,使用自定义MYHTMLLayout插件的(简化)配置可能如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!-- the packages attribute contains a comma-separated list
of the packages that Log4J2 will scan for custom plugins. -->
<configuration packages="ibtrader.log4j2">
<appenders>
<appender name="HTMLAppender" type="File" fileName="logs/mainlog.html">
<!-- Each plugin has a *name*, declared with annotation on the
plugin implementation class.
The plugin name does not need to match the class name.
The plugin name needs to match the *type* attribute in the config.
For example:
package ibtrader.log4j2;
import ...;
@Plugin(name="MYHTMLLayout", category="Core",
elementType="layout", printObject=true)
public class MyHTMLLayoutImpl extends AbstractStringLayout {...
-->
<layout type="MYHTMLLayout" charset="UTF-8" title="IBTRader logs" locationInfo="true" />
</appender>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="HTMLAppender" />
</root>
</loggers>
</configuration>
答案 1 :(得分:0)
你必须扩展AbstractStringLayout,使用category =“Core”设置@Plugin属性,elementType =“layout”和name =“应该从配置文件中引用的类本身的名称”(即:“ExtHtmlLayout” “)
您可以从源中复制整个类HtmlLayout并更改您想要的任何内容,例如,以毫秒为单位的时间到以小时为单位的时间。
在“Configuration”标签中定义package属性,以将包用于您创建的扩展类。
Finally, just call the new layout from the configuration file:
<ExtHtmlLayout/>