Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-3529

Log4j Custom Layout always use the Attribute value of the last one created.

    XMLWordPrintableJSON

Details

    • Question
    • Status: Resolved
    • Major
    • Resolution: Not A Problem
    • None
    • None
    • Plugins
    • None

    Description

      Hi Log4j Team,

      I got one problem, I defined custom layout plugin like the following, and defined Appender to set the layout with different application value in log4j2.xml, but I found it always use the application value of the last Appender instead of different value for different appender.  any problem with my code ? how to resolve this kind of issue?

      @Plugin(name = "UnifiedLoggingFormat", category = Node.CATEGORY,
              elementType = Layout.ELEMENT_TYPE, printObject = true)
      public class UnifiedLoggingFormat extends AbstractStringLayout
       
          protected UnifiedLoggingFormat(String aInApplication, Charset aInCharset)
          {
              super(aInCharset);
              application = aInApplication;
          }
          @PluginFactory
          public static UnifiedLoggingFormat createLayout(
                  @PluginAttribute(value="Application",
                          defaultString = DEFAULT_APPLICATION)
                          String aInApplication,
                  @PluginAttribute(value = "charset", defaultString = "UTF-8")
                          Charset aInCharset)
          {
              return new UnifiedLoggingFormat(aInApplication, aInCharset);
           }
      

      log4j2.xml like the following:

       

       

             <RollingFile name="ServerFileAppender"
                           fileName="/opt/tpa/logs/Server.log"
                           filePattern="/opt/tpa/logs/Server/Server.%i.log.gz">
                  <UnifiedLoggingFormat/>
                  <AddMDCFilter/>
                  <Policies>
                      <SizeBasedTriggeringPolicy size="30 MB"/>
                  </Policies>
                  <DefaultRolloverStrategy fileIndex="min" min="1" max="10">
            
                  </DefaultRolloverStrategy>
              </RollingFile>
              <Socket name="Socket_ServerFileAppender"
                      host="localhost"
                      port="8514"
                      reconnectionDelayMillis="10000">
                  <UnifiedLoggingFormat Application="SERVER"/>
              </Socket>
              <Socket name="Socket_RSArmingFileAppender"
                      host="localhost"
                      port="8514"
                      reconnectionDelayMillis="10000">
                  <UnifiedLoggingFormat Application="ARMING"/>
              </Socket>
       
      

         

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            xzhao1 XIAOMING ZHAO
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: