Class Log4JSegmentListener

java.lang.Object
com.amazonaws.xray.log4j.Log4JSegmentListener
All Implemented Interfaces:
SegmentListener

public class Log4JSegmentListener
extends java.lang.Object
implements SegmentListener
Implementation of SegmentListener that's used for Trace ID injection into logs that are recorded using the Log4J frontend API. To inject the X-Ray trace ID into your logging events, insert %X{AWS-XRAY-TRACE-ID} wherever you feel comfortable in the layout pattern of your logging events. This is can be done wherever you configure Log4J for your project. Refer to the Log4J configuration documentation for more details.
  • Constructor Summary

    Constructors 
    Constructor Description
    Log4JSegmentListener()
    Returns a default Log4JSegmentListener using AWS-XRAY-TRACE-ID as the prefix for trace ID injections
    Log4JSegmentListener​(java.lang.String prefix)
    Returns a Log4JSegmentListener using the provided prefix for trace ID injections
  • Method Summary

    Modifier and Type Method Description
    java.lang.String getPrefix()  
    void onClearEntity​(Entity entity)
    Removes the AWS-XRAY-TRACE-ID key from the ThreadContext upon the completion of each segment.
    void onSetEntity​(Entity oldEntity, Entity newEntity)
    Maps the AWS-XRAY-TRACE-ID key to the formatted ID of the entity that's just been created in the Log4J ThreadContext.
    void setPrefix​(java.lang.String prefix)
    Sets the trace ID injection prefix to provided value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Log4JSegmentListener

      public Log4JSegmentListener()
      Returns a default Log4JSegmentListener using AWS-XRAY-TRACE-ID as the prefix for trace ID injections
    • Log4JSegmentListener

      public Log4JSegmentListener​(java.lang.String prefix)
      Returns a Log4JSegmentListener using the provided prefix for trace ID injections
      Parameters:
      prefix -
  • Method Details

    • getPrefix

      public java.lang.String getPrefix()
    • setPrefix

      public void setPrefix​(java.lang.String prefix)
      Sets the trace ID injection prefix to provided value. A colon and space separate the prefix and trace ID, unless the prefix is null or blank.
      Parameters:
      prefix -
    • onSetEntity

      public void onSetEntity​(Entity oldEntity, Entity newEntity)
      Maps the AWS-XRAY-TRACE-ID key to the formatted ID of the entity that's just been created in the Log4J ThreadContext. Does not perform injection if entity is not available or not sampled, since then the given entity would not be displayed in X-Ray console.
      Specified by:
      onSetEntity in interface SegmentListener
      Parameters:
      oldEntity - the previous entity or null
      newEntity - the new entity, either a subsegment or segment
    • onClearEntity

      public void onClearEntity​(Entity entity)
      Removes the AWS-XRAY-TRACE-ID key from the ThreadContext upon the completion of each segment.
      Specified by:
      onClearEntity in interface SegmentListener
      Parameters:
      entity - The segment that has just ended