Monday, 22 June 2015

Create Custom Pattern using log4j2 in java.

Log4j2 custom pattern example in java .

1. Create custom pattern, first we need to create plugin. For example I need to print TransactionLogId in logs.



TransactionLogIdConverter.java


@Plugin(name="TransactionIdConverter", category = "Converter")
@ConverterKeys({"trnLogid","trscId"})
public class TransactionLogIdConverter extends LogEventPatternConverter{
    protected TransactionLogIdConverter(String name, String style) {
        super(name, style);
    }

    public static TransactionLogIdConverter newInstance(String[] options) {
        return new TransactionLogIdConverter("trnLogid",Thread.currentThread().getName());
    }

    @Override
    public void format(LogEvent event, StringBuilder toAppendTo) {
        toAppendTo.append(getTransactionId());
       
    }

    protected String getTransactionId() {
      
       String transacationId = TransactionLogIdGenerator.myTransactionId();
      
        return transacationId;
    }

}


2. Next create TransactionIdGenerator.java class for generating TransactionLogId.

TransactionIdGenerator.java


public class TransactionLogIdGenerator {

       public static String  myTransactionId(){
              return "1234-5678-0123";
             
       }
   }

3. Now we need add custom pattern in log4j.xml file using  %trnLogid. And we have to add custom pattern plugin package name in log4j2.xml file in configuration element.

      log4j2.xml

      <?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logging.apache.org/log4j/2.0/config"
       status="OFF" packages="com.custom.pattern">
       <Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
     <PatternLayout pattern="  Date:-%d  TransactionLogID:- %trnLogid   line Number:-%L  Location:-%l   classNmae: %C  Message:-%m%n"/>
   
    </Console>
  </Appenders>
  <loggers>
              <Logger name="org.apache.log4j.xml" level="all" />
              <root level="all">
                     <appender-ref ref="STDOUT" level="TRACE" />
                     </root>
  </loggers>
</configuration>

  For testing log4j2 custom pattern we have to create one Test.java class.


Test.java

public class Test {
        private static final Logger logger = LogManager.getLogger(Test.class);   
       public static void myTest(){
              logger.info("this is my info message");
              logger.debug("This is debug message");
             
       }
       public static void main(String[] args) {
              myTest();
       }

}


The output comes like below.

Date:-2015-06-22 16:26:50,612  TransactionLogID:- 1234-5678-0123   line Number:-12  Location:-com.custom.test.Test.myTest(Test.java:12)   classNmae: com.custom.test.Test  Message:-this is my info message

Date:-2015-06-22 16:26:50,616  TransactionLogID:- 1234-5678-0123   line Number:-13  Location:-com.custom.test.Test.myTest(Test.java:13)   classNmae: com.custom.test.Test  Message:-This is debug message





3 comments: