1
<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.

Innovation
  • 1,514
  • 17
  • 32

2 Answers2

0

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.

Community
  • 1
  • 1
Techidiot
  • 1,921
  • 1
  • 15
  • 28
0

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.

asch
  • 1,923
  • 10
  • 21