Class CloudWatchAppender

java.lang.Object
org.apache.logging.log4j.core.AbstractLifeCycle
org.apache.logging.log4j.core.filter.AbstractFilterable
org.apache.logging.log4j.core.appender.AbstractAppender
com.example.blogs.app.logging.CloudWatchAppender
All Implemented Interfaces:
org.apache.logging.log4j.core.Appender, org.apache.logging.log4j.core.filter.Filterable, org.apache.logging.log4j.core.impl.LocationAware, org.apache.logging.log4j.core.LifeCycle, org.apache.logging.log4j.core.LifeCycle2

@Plugin(name="CloudWatch", category="Core", elementType="appender", printObject=true) public class CloudWatchAppender extends org.apache.logging.log4j.core.appender.AbstractAppender
Production-grade CloudWatch Logs appender for Log4j2. Features: - Batching for efficiency - Async processing with backpressure - Automatic retry with exponential backoff - Graceful shutdown with flush - LocalStack support - Proper error handling
  • Nested Class Summary

    Nested classes/interfaces inherited from class org.apache.logging.log4j.core.appender.AbstractAppender

    org.apache.logging.log4j.core.appender.AbstractAppender.Builder<B extends org.apache.logging.log4j.core.appender.AbstractAppender.Builder<B>>

    Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle

    org.apache.logging.log4j.core.LifeCycle.State
  • Field Summary

    Fields inherited from class org.apache.logging.log4j.core.AbstractLifeCycle

    DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGER

    Fields inherited from interface org.apache.logging.log4j.core.Appender

    ELEMENT_TYPE, EMPTY_ARRAY
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    CloudWatchAppender(String name, org.apache.logging.log4j.core.Filter filter, org.apache.logging.log4j.core.Layout<? extends Serializable> layout, boolean ignoreExceptions, org.apache.logging.log4j.core.config.Property[] properties, software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient cloudWatchClient, String logGroupName, String logStreamName, boolean createLogGroup, boolean createLogStream)
    Constructs a CloudWatch appender with the specified configuration and resources.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    append(org.apache.logging.log4j.core.LogEvent event)
    Appends a log event to the CloudWatch queue for asynchronous processing.
    createAppender(String name, String logGroup, String logStream, String region, String endpoint, String accessKeyId, String secretAccessKey, boolean createLogGroup, boolean createLogStream, org.apache.logging.log4j.core.Layout<? extends Serializable> layout, org.apache.logging.log4j.core.Filter filter)
    Factory method for creating CloudWatchAppender instances from Log4j2 configuration.
    void
    Stops the appender and gracefully shuts down background processing.

    Methods inherited from class org.apache.logging.log4j.core.appender.AbstractAppender

    error, error, error, getHandler, getLayout, getName, ignoreExceptions, parseInt, requiresLocation, setHandler, toSerializable, toString

    Methods inherited from class org.apache.logging.log4j.core.filter.AbstractFilterable

    addFilter, getFilter, getPropertyArray, hasFilter, isFiltered, removeFilter, start, stop, stop

    Methods inherited from class org.apache.logging.log4j.core.AbstractLifeCycle

    equalsImpl, getState, getStatusLogger, hashCodeImpl, initialize, isInitialized, isStarted, isStarting, isStopped, isStopping, setStarted, setStarting, setState, setStopped, setStopping, stop

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.apache.logging.log4j.core.LifeCycle

    getState, initialize, isStarted, isStopped, start
  • Constructor Details

    • CloudWatchAppender

      protected CloudWatchAppender(String name, org.apache.logging.log4j.core.Filter filter, org.apache.logging.log4j.core.Layout<? extends Serializable> layout, boolean ignoreExceptions, org.apache.logging.log4j.core.config.Property[] properties, software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient cloudWatchClient, String logGroupName, String logStreamName, boolean createLogGroup, boolean createLogStream)
      Constructs a CloudWatch appender with the specified configuration and resources.
      Parameters:
      name - the appender name
      filter - the filter to apply to log events
      layout - the layout for formatting log messages
      ignoreExceptions - whether to ignore exceptions during logging
      properties - additional properties for the appender
      cloudWatchClient - the AWS CloudWatch Logs client
      logGroupName - the CloudWatch log group name
      logStreamName - the CloudWatch log stream name
      createLogGroup - whether to automatically create the log group if it doesn't exist
      createLogStream - whether to automatically create the log stream if it doesn't exist
  • Method Details

    • append

      public void append(org.apache.logging.log4j.core.LogEvent event)
      Appends a log event to the CloudWatch queue for asynchronous processing.
      Parameters:
      event - the log event to append
    • stop

      public void stop()
      Stops the appender and gracefully shuts down background processing. Flushes remaining events and closes the CloudWatch client.
      Specified by:
      stop in interface org.apache.logging.log4j.core.LifeCycle
      Overrides:
      stop in class org.apache.logging.log4j.core.AbstractLifeCycle
    • createAppender

      @PluginFactory public static CloudWatchAppender createAppender(@PluginAttribute("name") String name, @PluginAttribute("logGroup") String logGroup, @PluginAttribute("logStream") String logStream, @PluginAttribute(value="region",defaultString="eu-central-1") String region, @PluginAttribute("endpoint") String endpoint, @PluginAttribute("accessKeyId") String accessKeyId, @PluginAttribute("secretAccessKey") String secretAccessKey, @PluginAttribute(value="createLogGroup",defaultBoolean=true) boolean createLogGroup, @PluginAttribute(value="createLogStream",defaultBoolean=true) boolean createLogStream, @PluginElement("Layout") org.apache.logging.log4j.core.Layout<? extends Serializable> layout, @PluginElement("Filter") org.apache.logging.log4j.core.Filter filter)
      Factory method for creating CloudWatchAppender instances from Log4j2 configuration.
      Parameters:
      name - the appender name
      logGroup - the CloudWatch log group name
      logStream - the CloudWatch log stream name
      region - the AWS region (default: eu-central-1)
      endpoint - optional custom endpoint URL for LocalStack or other AWS-compatible services
      accessKeyId - optional AWS access key ID
      secretAccessKey - optional AWS secret access key
      createLogGroup - whether to create the log group if it doesn't exist (default: true)
      createLogStream - whether to create the log stream if it doesn't exist (default: true)
      layout - the layout for formatting log messages
      filter - the filter to apply to log events
      Returns:
      configured CloudWatchAppender instance or null if configuration is invalid