Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

how to change htmllayout in log4j2

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>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

So i just copied whole HTMLLayout class from source, which is available at log4j-core / src / main / java / org / apache / logging / log4j / core / layout

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.

there is method called toSerializable which contains actual formatting of each record, so you can manipulate it as per your need.

once class is modified, you need to provide the new layout in your log4j2.html as 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>

Notice following

<Configuration packages="com.bssauto.helpers">

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" />

and CustomHTMLLayout is custom layout class created by extending AbstractStringLayout

Make sure you are using latest log4j2 version, I used log4j 2.2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...