Convert log4j 1.x to log4j 2.x custom layout appender

…衆ロ難τιáo~ 提交于 2019-12-12 05:06:35

问题


<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="${catalina.base}/logs/server.log" />
        <param name="Append" value="true" />
        <param name="DatePattern" value="'.'yyyy-MM-dd" />
        <layout class="com.mayank.base.logging.CustomPatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE}#%X{requestId} %R %-5p [%c{1}] - %m%n" />
        </layout>
    </appender>

I am facing trouble converting it to log4j 2. How i can addmy custom pattern layout.


回答1:


You should implement your CustomPatternLayout as a Layout plugin. You have a code example on (layout plugins manual).

In very general you should :

  • Implement the plugin class with proper annotations on the constructor
  • Implement the plugin factory method
  • Implement the desired formatting
  • Mention in log4j configuration your plugins package:

    <Configuration packages="com.mayank.base.logging">
    
  • Use in the configuration the name of your plugin class

You can see explanations and examples of plugins implementation on this blog post1, post2, post3. There is no layout plugin example, but the technique is very similar.




回答2:


The <RollingFile> tag should be present.

Here are couple of samples -

<RollingFile name="ROLLING" 
             fileName="f:/my_dir/logsroll.log"
             filePattern="f:/my_dir/logsroll-%i.log">

OR

<RollingFile name="MyFile" fileName="d:/log/bsi/admin/total/totalLog.log"
            filePattern="d:/log/totalLog-%d{MM-dd-yyyy}-%i.log">
            <PatternLayout>
                <Pattern>%d %p %c [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="2000"/>
        </RollingFile>

Referthis for more.



来源:https://stackoverflow.com/questions/39593947/convert-log4j-1-x-to-log4j-2-x-custom-layout-appender

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!