Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
RT-36 - updating to use new auth library
  • Loading branch information
darinhoward committed Aug 15, 2017
commit df9c89064178de88423674e432ede9b8fd97c989
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<dependency>
<groupId>com.stackify</groupId>
<artifactId>stackify-api-java</artifactId>
<version>3.0.0</version>
<version>3.0.1-SNAPSHOT</version>
</dependency>

<!-- Runtime dependencies -->
Expand Down
332 changes: 136 additions & 196 deletions src/main/java/com/stackify/log/log4j12/StackifyLogAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
*/
package com.stackify.log.log4j12;

import com.stackify.api.common.mask.Masker;
import lombok.Getter;
import lombok.Setter;
import org.apache.log4j.spi.LoggingEvent;

import com.stackify.api.common.ApiClients;
import com.stackify.api.common.ApiConfiguration;
import com.stackify.api.common.ApiConfigurations;
import com.stackify.api.common.log.LogAppender;
import com.stackify.api.common.mask.Masker;
import lombok.Getter;
import lombok.Setter;
import org.apache.log4j.spi.LoggingEvent;

/**
* Log4j 1.2 logger appender for sending logs to Stackify.
*
* <p>
* <p>
* Example appender configuration (*.properties file):
* <pre>
Expand All @@ -38,7 +37,7 @@
* log4j.appender.STACKIFY.environment=YOUR_ENVIRONMENT
* }
* </pre>
*
* <p>
* <p>
* Example appender configuration (*.xml file):
* <pre>
Expand All @@ -50,7 +49,7 @@
* </appender>
* }
* </pre>
*
* <p>
* <p>
* Be sure to shutdown Log4j to flush this appender of any logs and shutdown the background thread:
* <pre>
Expand All @@ -59,193 +58,134 @@
*
* @author Eric Martin
*/
@Setter
@Getter
public class StackifyLogAppender extends NonReentrantAppender {

/**
* API URL (Appender configuration parameter)
*/
private String apiUrl = "https://api.stackify.com";

/**
* API Key (Appender configuration parameter)
*/
private String apiKey = null;

/**
* Application name (Appender configuration parameter)
*/
private String application = null;

/**
* Environment (Appender configuration parameter)
*/
private String environment = null;

/**
* Generic log appender
*/
private LogAppender<LoggingEvent> logAppender;

@Setter
@Getter
private String maskEnabled;

@Setter
@Getter
private String maskCreditCard;

@Setter
@Getter
private String maskSSN;

@Setter
@Getter
private String maskIP;

@Setter
@Getter
private String maskCustom;

/**
* @return the apiUrl
*/
public String getApiUrl() {
return apiUrl;
}

/**
* @param apiUrl the apiUrl to set
*/
public void setApiUrl(String apiUrl) {
this.apiUrl = apiUrl;
}

/**
* @return the apiKey
*/
public String getApiKey() {
return apiKey;
}

/**
* @param apiKey the apiKey to set
*/
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

/**
* @return the application
*/
public String getApplication() {
return application;
}

/**
* @param application the application to set
*/
public void setApplication(String application) {
this.application = application;
}

/**
* @return the environment
*/
public String getEnvironment() {
return environment;
}

/**
* @param environment the environment to set
*/
public void setEnvironment(String environment) {
this.environment = environment;
}

/**
* @see org.apache.log4j.AppenderSkeleton#activateOptions()
*/
@Override
public void activateOptions() {
super.activateOptions();

// build the api config

ApiConfiguration apiConfig = ApiConfigurations.fromPropertiesWithOverrides(apiUrl, apiKey, application, environment);

// get the client project name with version

String clientName = ApiClients.getApiClient(StackifyLogAppender.class, "/stackify-log-log4j12.properties", "stackify-log-log4j12");

// build the log appender

try {

// setup masker

Masker masker = new Masker();
if (maskEnabled != null && Boolean.parseBoolean(maskEnabled)) {

// set default masks
masker.addMask(Masker.MASK_CREDITCARD);
masker.addMask(Masker.MASK_SSN);

if (maskCreditCard != null && !Boolean.parseBoolean(maskCreditCard)) {
masker.removeMask(Masker.MASK_CREDITCARD);
}

if (maskSSN != null && !Boolean.parseBoolean(maskSSN)) {
masker.removeMask(Masker.MASK_SSN);
}

if (maskIP != null && Boolean.parseBoolean(maskIP)) {
masker.addMask(Masker.MASK_IP);
}

if (maskCustom != null) {
masker.addMask(maskCustom);
}

} else {
masker.clearMasks();
}

this.logAppender = new LogAppender<LoggingEvent>(clientName, new LoggingEventAdapter(apiConfig.getEnvDetail()), masker);
this.logAppender.activate(apiConfig);
} catch (Exception e) {
errorHandler.error("Exception starting the Stackify_LogBackgroundService", e, 0);
}
}

/**
* @see com.stackify.log.log4j12.NonReentrantAppender#subAppend(org.apache.log4j.spi.LoggingEvent)
*/
@Override
protected void subAppend(final LoggingEvent event) {
try {
this.logAppender.append(event);
} catch (Exception e) {
errorHandler.error("Exception appending event to Stackify Log Appender", e, 0);
}
}

/**
* @see org.apache.log4j.Appender#close()
*/
@Override
public void close() {
try {
this.logAppender.close();
} catch (Exception e) {
errorHandler.error("Exception closing Stackify Log Appender", e, 0);
}
}

/**
* @see org.apache.log4j.Appender#requiresLayout()
*/
@Override
public boolean requiresLayout() {
return false;
}

/**
* API URL (Appender configuration parameter)
*/
private String apiUrl = "https://api.stackify.com";

/**
* Auth API URL
*/
private String authUrl = "https://auth.stackify.net";

/**
* API Key (Appender configuration parameter)
*/
private String apiKey = null;

/**
* Application name (Appender configuration parameter)
*/
private String application = null;

/**
* Environment (Appender configuration parameter)
*/
private String environment = null;

/**
* Generic log appender
*/
private LogAppender<LoggingEvent> logAppender;

private String maskEnabled;

private String maskCreditCard;

private String maskSSN;

private String maskIP;

private String maskCustom;

/**
* @see org.apache.log4j.AppenderSkeleton#activateOptions()
*/
@Override
public void activateOptions() {
super.activateOptions();

// build the api config

ApiConfiguration apiConfig = ApiConfigurations.fromPropertiesWithOverrides(apiUrl, authUrl, apiKey, application, environment);

// get the client project name with version

String clientName = ApiClients.getApiClient(StackifyLogAppender.class, "/stackify-log-log4j12.properties", "stackify-log-log4j12");

// build the log appender

try {

// setup masker

Masker masker = new Masker();
if (maskEnabled != null && Boolean.parseBoolean(maskEnabled)) {

// set default masks
masker.addMask(Masker.MASK_CREDITCARD);
masker.addMask(Masker.MASK_SSN);

if (maskCreditCard != null && !Boolean.parseBoolean(maskCreditCard)) {
masker.removeMask(Masker.MASK_CREDITCARD);
}

if (maskSSN != null && !Boolean.parseBoolean(maskSSN)) {
masker.removeMask(Masker.MASK_SSN);
}

if (maskIP != null && Boolean.parseBoolean(maskIP)) {
masker.addMask(Masker.MASK_IP);
}

if (maskCustom != null) {
masker.addMask(maskCustom);
}

} else {
masker.clearMasks();
}

this.logAppender = new LogAppender<LoggingEvent>(clientName, new LoggingEventAdapter(apiConfig.getEnvDetail()), masker);
this.logAppender.activate(apiConfig);
} catch (Exception e) {
errorHandler.error("Exception starting the Stackify_LogBackgroundService", e, 0);
}
}

/**
* @see com.stackify.log.log4j12.NonReentrantAppender#subAppend(org.apache.log4j.spi.LoggingEvent)
*/
@Override
protected void subAppend(final LoggingEvent event) {
try {
this.logAppender.append(event);
} catch (Exception e) {
errorHandler.error("Exception appending event to Stackify Log Appender", e, 0);
}
}

/**
* @see org.apache.log4j.Appender#close()
*/
@Override
public void close() {
try {
this.logAppender.close();
} catch (Exception e) {
errorHandler.error("Exception closing Stackify Log Appender", e, 0);
}
}

/**
* @see org.apache.log4j.Appender#requiresLayout()
*/
@Override
public boolean requiresLayout() {
return false;
}
}