
com.kdgregory.logback.aws.SNSAppender Maven / Gradle / Ivy
Show all versions of logback-aws-appenders Show documentation
// Copyright (c) Keith D Gregory
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.kdgregory.logback.aws;
import java.util.Date;
import com.kdgregory.logback.aws.internal.AbstractAppender;
import com.kdgregory.logging.aws.common.Substitutions;
import com.kdgregory.logging.aws.sns.SNSWriterStatistics;
import com.kdgregory.logging.aws.sns.SNSWriterStatisticsMXBean;
import com.kdgregory.logging.common.util.DefaultThreadFactory;
import com.kdgregory.logging.aws.sns.SNSWriterConfig;
import com.kdgregory.logging.aws.sns.SNSWriterFactory;
/**
* An appender that writes to an SNS topic.
*
* This appender supports the following configuration parameters:
*
*
*
* topicName
* The name of the destination SNS topic; substitutions are allowed.
*
* Must refer to a topic in the current region; if not, and you do not
* enable auto-create, initialization fails.
*
* If you specify both topicName
and topicArn
,
* the latter takes precedence.
*
*
* topicArn
* The ARN of the destination SNS topic; substitutions are allowed.
*
* Must refer to a topic in the current region; if not, initialization
* fails.
*
* If you specify both topicName
and topicArn
,
* the latter takes precedence.
*
*
* autoCreate
* If true, and the topic is specified by name, the appender will create
* the topic if it does not already exist. If false, a missing topic
* will be reported as an error and the appender will be disabled.
*
* Default is false
.
*
*
* subject
* (optional) The subject for messages that are delivered via email. This
* is constrained by the SNS API to be less than 100 characters, ASCII
* only, and not start with whitespace.
*
*
* truncateOversizeMessages
* If true
(the default), oversize messages are truncated to
* the maximum length permitted by SNS. If false
, they are
* discarded. In either case, the oversized message is reported to the
* Log4J debug log.
*
*
* discardThreshold
* The number of unsent messages that will trigger message discard. A
* high value is useful when network connectivity is intermittent and/or
* overall AWS communication is causing throttling. However, a value that
* is too high may cause out-of-memory errors.
*
* The default, 10,000, is based on the assumptions that (1) each message
* will be 1k or less, and (2) any app that uses remote logging can afford
* 10MB.
*
*
* discardAction
* The action to take when the number of unsent messages exceeds the
* discard threshold. Values are "none" (retain all messages), "oldest"
* (discard oldest messages), and "newest" (discard most recent messages).
*
* The default is "oldest". Attempting to set an incorrect value will throw
* a configuration error.
*
*
* assumedRole
* Specifies role name or ARN that will be assumed by this appender. Useful
* for cross-account logging. If the appender does not have permission to
* assume this role, initialization will fail.
*
*
* clientFactory
* The fully-qualified name of a static method to create the correct AWS
* client, which will be called instead of the writer's internal client
* factory. This is useful if you need non-default configuration, such as
* using a proxy server.
*
* The passed string is of the form com.example.Classname.methodName
.
* If this does not reference a class/method on the classpath then writer
* initialization will fail.
*
*
* clientRegion
* Specifies a non-default service region. This setting is ignored if you
* use a client factory.
*
*
* clientEndpoint
* Specifies a non-default service endpoint. Typically used when running in
* a VPC, when the normal endpoint is not available.
*
*
* initializationTimeout
* Milliseconds to wait for appender to initialize. If this timeout expires,
* the appender will shut down its writer thread and discard any future log
* events. The only reason to change this is if you're deploying to a high-
* contention environment (and even then, the default of 60 seconds should be
* more than enough).
*
*
* useShutdownHook
* Controls whether the appender uses a shutdown hook to attempt to process
* outstanding messages when the JVM exits. This is true by default; set to
* false to disable.
*
*
* @see Appender documentation
*/
public class SNSAppender
extends AbstractAppender
<
SNSWriterConfig,
SNSWriterStatistics,
SNSWriterStatisticsMXBean,
LogbackEventType
>
{
public SNSAppender()
{
super(new SNSWriterConfig(),
new DefaultThreadFactory("log4j-sns"),
new SNSWriterFactory(),
new SNSWriterStatistics(),
SNSWriterStatisticsMXBean.class);
super.setDiscardThreshold(1000);
super.setBatchDelay(1);
}
//----------------------------------------------------------------------------
// Configuration
//----------------------------------------------------------------------------
/**
* Sets the topicName
configuration property.
*/
public void setTopicName(String value)
{
appenderConfig.setTopicName(value);
}
/**
* Returns the topicName
configuration property, null
* if the appender was configured via ARN.
*/
public String getTopicName()
{
return appenderConfig.getTopicName();
}
/**
* Sets the topicArn
configuration property.
*/
public void setTopicArn(String value)
{
appenderConfig.setTopicArn(value);
}
/**
* Returns the topicArn
configuration property, null
* if the appender was configured via name.
*/
public String getTopicArn()
{
return appenderConfig.getTopicArn();
}
/**
* Sets the autoCreate
configuration property.
*/
public void setAutoCreate(boolean value)
{
appenderConfig.setAutoCreate(value);
}
/**
* Returns the autoCreate
configuration property.
*/
public boolean getAutoCreate()
{
return appenderConfig.getAutoCreate();
}
/**
* Sets the subject
configuration property.
*/
public void setSubject(String value)
{
appenderConfig.setSubject(value);
}
/**
* Returns the subject
configuration property.
*/
public String getSubject()
{
return appenderConfig.getSubject();
}
/**
* Any configured batch delay will be ignored; the appender attempts to send
* all messages as soon as they are appended.
*/
@Override
public void setBatchDelay(long value)
{
super.setBatchDelay(1);
}
//----------------------------------------------------------------------------
// AbstractAppender
//----------------------------------------------------------------------------
@Override
protected SNSWriterConfig generateWriterConfig()
{
Substitutions subs = new Substitutions(new Date(), 0);
String actualTopicName = subs.perform(appenderConfig.getTopicName());
String actualTopicArn = subs.perform(appenderConfig.getTopicArn());
String actualSubject = subs.perform(appenderConfig.getSubject());
return ((SNSWriterConfig)appenderConfig.clone())
.setTopicName(actualTopicName)
.setTopicArn(actualTopicArn)
.setSubject(actualSubject);
}
}