Skip to content

Commit

Permalink
Version 0.2.0 Fix Logging Error
Browse files Browse the repository at this point in the history
Fixed the logging error in previous version. Part of the fix involves
upgrading to log4j2 which means you will want to update that in your
build.gradle file.
  • Loading branch information
pfreitag committed Jul 1, 2020
1 parent c1615c4 commit 50bbbef
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 34 deletions.
4 changes: 2 additions & 2 deletions java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ dependencies {
'com.amazonaws.serverless:aws-serverless-java-container-core:1.5',
'javax.servlet.jsp:javax.servlet.jsp-api:2.3.1',
'javax.el:javax.el-api:3.0.0',
'com.amazonaws:aws-lambda-java-log4j:1.0.1',
'com.amazonaws:aws-xray-recorder-sdk-core:2.5.0'
'com.amazonaws:aws-xray-recorder-sdk-core:2.5.0',
'com.amazonaws:aws-lambda-java-log4j:1.0.1'
)
compile fileTree(dir: 'jars', include: '*.jar')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Subsegment;
import org.apache.log4j.Logger;


import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
Expand All @@ -22,8 +22,7 @@
public class CFMLLambdaContainerHandler<RequestType, ResponseType>
extends AwsLambdaServletContainerHandler<RequestType, ResponseType, HttpServletRequest, AwsHttpServletResponse> {

private static final Logger LOG = Logger.getLogger(CFMLLambdaContainerHandler.class);


/**
* Returns a new instance of an CFMLLambdaContainerHandler initialized to work with <code>AwsProxyRequest</code>
* and <code>AwsProxyResponse</code> objects.
Expand Down Expand Up @@ -99,18 +98,15 @@ protected void handleRequest(HttpServletRequest httpServletRequest, AwsHttpServl
((Subsegment)seg).putHttp("request", requestAttributes);

}
LOG.debug("CFMLLambdaContainerHandler handleRequest: " + req.getRequestURI());
//LOG.debug("CFMLLambdaContainerHandler handleRequest: " + req.getRequestURI());
StreamLambdaHandler.getCFMLServlet().service(req, httpServletResponse);


} catch (Throwable t) {
t.printStackTrace();

LOG.error("CFMLLambdaContainerHandler Servlet Request Threw Exception: ");
LOG.error(t);
for (StackTraceElement st: t.getStackTrace()) {
LOG.error("STE:" + st.toString());
}
StreamLambdaHandler.log("CFMLLambdaContainerHandler Servlet Request Threw Exception: ", t);

if (seg != null) {
((Subsegment)seg).addException(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
import java.util.EventListener;
import java.util.Map;

import org.apache.log4j.Logger;

public class ServletContextWrapper implements ServletContext {

private ServletContext servletContext;

private static final Logger LOG = Logger.getLogger(ServletContextWrapper.class);

public ServletContextWrapper(ServletContext servletContext) {
this.servletContext = servletContext;
Expand Down Expand Up @@ -133,7 +131,7 @@ public String getRealPath(String path) {

@Override
public String getServerInfo() {
return this.servletContext.getServerInfo() + "; Foundeo FuseLess v0.1.0";
return this.servletContext.getServerInfo() + "; Foundeo FuseLess v0.2.0";
}

@Override
Expand Down
58 changes: 47 additions & 11 deletions java/src/main/java/com/foundeo/fuseless/StreamLambdaHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import lucee.loader.servlet.CFMLServlet;

import org.apache.log4j.Logger;
import com.amazonaws.services.lambda.runtime.LambdaRuntime;
import com.amazonaws.services.lambda.runtime.LambdaLogger;

import java.lang.StringBuilder;


import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import java.io.*;

public class StreamLambdaHandler implements RequestStreamHandler {
private static final Logger LOG = Logger.getLogger(StreamLambdaHandler.class);

private static final LambdaLogger logger = LambdaRuntime.getLogger();
private static CFMLLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

private static HttpServlet cfmlServlet = null;
Expand All @@ -24,16 +28,27 @@ public class StreamLambdaHandler implements RequestStreamHandler {

static {
try {
logger.log("FuseLess: StreamLambdaHandler initializing");

//load Log4j


if (System.getenv("FUSELESS_ENABLE_XRAY") != null && System.getenv("FUSELESS_ENABLE_XRAY").equals("true")) {
ENABLE_XRAY = true;
}
LOG.info("StreamLambdaHandler initializing");






handler = CFMLLambdaContainerHandler.getAwsProxyHandler();


// we use the onStartup method of the handler to register our custom filter
handler.onStartup(servletContext -> {
long startTime = System.currentTimeMillis();
LOG.info("StreamLambdaHandler onStartup");
log("StreamLambdaHandler onStartup");
PrintStream systemOUT = System.out;
try {
//redirecting System.out to a file because lucee logs a bunch of stuff with it
Expand All @@ -45,7 +60,7 @@ public void write(int b) {
}
}));
} catch (Exception e) {
LOG.error("Error redirecting system.out", e);
log("FuseLess: Error redirecting system.out", e);
}

System.setProperty("lucee.web.dir", "/tmp/lucee/web/");
Expand Down Expand Up @@ -77,34 +92,55 @@ public void write(int b) {
System.setProperty("felix.cache.bufsize", felix_cache_bufsize);
}
try {

new File("/tmp/lucee/web/").mkdirs();
StreamLambdaHandler.cfmlServlet = new CFMLServlet();
ServletConfig servletConfig = new CFMLServletConfig(new ServletContextWrapper(servletContext));

cfmlServlet.init(servletConfig);
//CFMLEngine cfmlEngine = CFMLEngineFactory.getInstance(servletConfig);
} catch (Throwable t) {
LOG.error("StreamLambdaHandler onStartup exception", t);
log("onStartup exception", t);
}
long startupComplete = System.currentTimeMillis();
try {
//put System.out back to default PrintStream
System.setOut(systemOUT);
} catch (Exception e) {
LOG.error("Error putting back system.out", e);
log("Error putting back system.out: ", e);
}
LOG.info("StreamLambdaHandler onStartup complete: " + (startupComplete-startTime));
log("onStartup complete: " + (startupComplete-startTime));
});
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
log("ContainerInitializationException: ", e);
e.printStackTrace();

throw new RuntimeException("StreamLambdaHandler Could not initialize the container", e);
throw new RuntimeException("Could not initialize the container", e);
} finally {

}
}

public static final void log(String msg) {
StreamLambdaHandler.log(msg, null);
}

public static final void log(String msg, Throwable t) {
StringBuilder sb = new StringBuilder();
sb.append("FuseLess: StreamLambdaHandler: ");
sb.append(msg);
if (t != null) {
sb.append(" :: ");
sb.append(t.getMessage());
for (StackTraceElement e : t.getStackTrace()) {
sb.append(e.toString());
}
}
sb.append(10);
logger.log(sb.toString());

}

public static final HttpServlet getCFMLServlet() {
return StreamLambdaHandler.cfmlServlet;
}
Expand Down
6 changes: 0 additions & 6 deletions java/src/main/resources/log4j.properties

This file was deleted.

16 changes: 16 additions & 0 deletions java/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Configuration status="WARN">
<Appenders>
<Lambda name="Lambda">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1} - %m%n</pattern>
</PatternLayout>
</Lambda>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Lambda"/>
</Root>
<Logger name="software.amazon.awssdk" level="WARN" />
<Logger name="software.amazon.awssdk.request" level="DEBUG" />
</Loggers>
</Configuration>
2 changes: 1 addition & 1 deletion test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies {
'javax.servlet.jsp:javax.servlet.jsp-api:2.3.1',
'javax.el:javax.el-api:3.0.0',
'com.amazonaws:aws-lambda-java-core:1.2.1',
'com.amazonaws:aws-lambda-java-log4j:1.0.1'
'com.amazonaws:aws-lambda-java-log4j2:1.2.0'
)
compile fileTree(dir: 'jars', include: '*.jar')
}
Expand Down
4 changes: 2 additions & 2 deletions test/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Resources:
Properties:
Handler: com.foundeo.fuseless.StreamLambdaHandler
CodeUri: ./build/distributions/test.zip
Runtime: java8
Runtime: java11
Timeout: 100
MemorySize: 512
Events:
Expand All @@ -24,7 +24,7 @@ Resources:
Properties:
Handler: com.foundeo.fuseless.EventLambdaHandler::handleRequest
CodeUri: ./build/distributions/test.zip
Runtime: java8
Runtime: java11
Timeout: 100
MemorySize: 512
Environment:
Expand Down

0 comments on commit 50bbbef

Please sign in to comment.