Skip to content

Commit

Permalink
Added log4j2 support to Docker images (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
azagniotov authored Apr 4, 2021
1 parent 68792fe commit a95ba36
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 107 deletions.
57 changes: 0 additions & 57 deletions build-for-docker.gradle

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ repositories {
mavenCentral()
}

apply from: "$rootDir/conf/gradle/default-dependencies.gradle"
apply from: "$rootDir/conf/gradle/dependencies.gradle"
apply from: "$rootDir/conf/gradle/ide.gradle"
apply from: "$rootDir/conf/gradle/multi-test-source-sets.gradle"
apply from: "$rootDir/conf/gradle/jacoco.gradle"
Expand Down
21 changes: 0 additions & 21 deletions conf/gradle/dependencies-with-log4j2.gradle

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ dependencies {
api "org.xmlunit:xmlunit-core:${xmlUnitVersion}"
api "org.xmlunit:xmlunit-placeholders:${xmlUnitVersion}"
api "io.github.azagniotov:collection-type-safe-converter:1.0.1"
api "org.slf4j:slf4j-api:1.7.30"

if (project.hasProperty("log4j")) {
println "> Building with log4j included in default dependencies"
api "org.apache.logging.log4j:log4j-api:${log4j2Version}"
api "org.apache.logging.log4j:log4j-core:${log4j2Version}"
api "org.apache.logging.log4j:log4j-slf4j-impl:${log4j2Version}"
} else {
println "> Building with default dependencies"
api "org.slf4j:slf4j-api:1.7.30"
}

testImplementation "junit:junit:4.13.1"
testImplementation "org.mockito:mockito-core:3.3.3"
Expand Down
20 changes: 13 additions & 7 deletions docker/jdk11/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RUN git clone https://github.com/azagniotov/stubby4j.git && \
cd stubby4j && \
git fetch -f --tags && \
git checkout $REVISION && \
gradle clean jar
gradle -Plog4j clean jar

########################################################################################
# Stage 2 : create the Docker final image
Expand All @@ -44,28 +44,34 @@ ENV YAML_CONFIG="main.yaml"
ENV STUBBY4J_USER_HOME=/home/stubby4j

# Users & permissions, docs: https://wiki.alpinelinux.org/wiki/Setting_up_a_new_user
RUN addgroup --system --gid 1007 stubby4j && \
adduser --system --uid 1007 stubby4j --shell /bin/bash --home "$STUBBY4J_USER_HOME" && \
RUN addgroup --system --gid 1000 stubby4j && \
adduser --system --uid 1000 stubby4j --shell /bin/bash --home "$STUBBY4J_USER_HOME" && \
chown --recursive stubby4j:stubby4j "$STUBBY4J_USER_HOME"

# Mark the 'data' directory as volume
VOLUME "$STUBBY4J_USER_HOME/data"
WORKDIR "$STUBBY4J_USER_HOME"

COPY --from=BUILD_JAR_STAGE /home/gradle/stubby4j/build/libs/stubby4j*SNAPSHOT.jar ./stubby4j.jar
RUN chown stubby4j:stubby4j stubby4j.jar && java -version && ls -al
COPY --from=BUILD_JAR_STAGE /home/gradle/stubby4j/docker/log4j2-for-docker.xml ./log4j2.xml
RUN chown stubby4j:stubby4j stubby4j.jar && \
chown stubby4j:stubby4j log4j2.xml && \
java -version && \
ls -al

# Set the UID and GID to stubby4j for the ENTRYPOINT instructions
USER stubby4j:stubby4j

# Mark the 'data' directory as volume
VOLUME "$STUBBY4J_USER_HOME/data"

# Expose the three stubby4j ports and run the JAR
EXPOSE $ADMIN_PORT $STUBS_PORT $STUBS_TLS_PORT
ENTRYPOINT java -jar stubby4j.jar \
ENTRYPOINT java -Dlog4j2.configurationFile=log4j2.xml -jar stubby4j.jar \
--location ${LOCATION} \
--admin ${ADMIN_PORT} \
--stubs ${STUBS_PORT} \
--tls ${STUBS_TLS_PORT} \
--data data/${YAML_CONFIG} \
--mute \
${WITH_STUB_CACHE_DISABLED} \
${WITH_DEBUG} \
${WITH_WATCH}
20 changes: 13 additions & 7 deletions docker/jdk15/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RUN git clone https://github.com/azagniotov/stubby4j.git && \
cd stubby4j && \
git fetch -f --tags && \
git checkout $REVISION && \
gradle clean jar
gradle -Plog4j clean jar

########################################################################################
# Stage 2 : create the Docker final image
Expand All @@ -44,28 +44,34 @@ ENV YAML_CONFIG="main.yaml"
ENV STUBBY4J_USER_HOME=/home/stubby4j

# Users & permissions, docs: https://wiki.alpinelinux.org/wiki/Setting_up_a_new_user
RUN addgroup --system --gid 1007 stubby4j && \
adduser --system --uid 1007 stubby4j --shell /bin/bash --home "$STUBBY4J_USER_HOME" && \
RUN addgroup --system --gid 1000 stubby4j && \
adduser --system --uid 1000 stubby4j --shell /bin/bash --home "$STUBBY4J_USER_HOME" && \
chown --recursive stubby4j:stubby4j "$STUBBY4J_USER_HOME"

# Mark the 'data' directory as volume
VOLUME "$STUBBY4J_USER_HOME/data"
WORKDIR "$STUBBY4J_USER_HOME"

COPY --from=BUILD_JAR_STAGE /home/gradle/stubby4j/build/libs/stubby4j*SNAPSHOT.jar ./stubby4j.jar
RUN chown stubby4j:stubby4j stubby4j.jar && java -version && ls -al
COPY --from=BUILD_JAR_STAGE /home/gradle/stubby4j/docker/log4j2-for-docker.xml ./log4j2.xml
RUN chown stubby4j:stubby4j stubby4j.jar && \
chown stubby4j:stubby4j log4j2.xml && \
java -version && \
ls -al

# Set the UID and GID to stubby4j for the ENTRYPOINT instructions
USER stubby4j:stubby4j

# Mark the 'data' directory as volume
VOLUME "$STUBBY4J_USER_HOME/data"

# Expose the three stubby4j ports and run the JAR
EXPOSE $ADMIN_PORT $STUBS_PORT $STUBS_TLS_PORT
ENTRYPOINT java -jar stubby4j.jar \
ENTRYPOINT java -Dlog4j2.configurationFile=log4j2.xml -jar stubby4j.jar \
--location ${LOCATION} \
--admin ${ADMIN_PORT} \
--stubs ${STUBS_PORT} \
--tls ${STUBS_TLS_PORT} \
--data data/${YAML_CONFIG} \
--mute \
${WITH_STUB_CACHE_DISABLED} \
${WITH_DEBUG} \
${WITH_WATCH}
20 changes: 13 additions & 7 deletions docker/jdk8/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RUN git clone https://github.com/azagniotov/stubby4j.git && \
cd stubby4j && \
git fetch -f --tags && \
git checkout $REVISION && \
gradle clean jar
gradle -Plog4j clean jar

########################################################################################
# Stage 2 : create the Docker final image
Expand All @@ -44,28 +44,34 @@ ENV YAML_CONFIG="main.yaml"
ENV STUBBY4J_USER_HOME=/home/stubby4j

# Users & permissions, docs: https://wiki.alpinelinux.org/wiki/Setting_up_a_new_user
RUN addgroup --system --gid 1007 stubby4j && \
adduser --system --uid 1007 stubby4j --shell /bin/bash --home "$STUBBY4J_USER_HOME" && \
RUN addgroup --system --gid 1000 stubby4j && \
adduser --system --uid 1000 stubby4j --shell /bin/bash --home "$STUBBY4J_USER_HOME" && \
chown --recursive stubby4j:stubby4j "$STUBBY4J_USER_HOME"

# Mark the 'data' directory as volume
VOLUME "$STUBBY4J_USER_HOME/data"
WORKDIR "$STUBBY4J_USER_HOME"

COPY --from=BUILD_JAR_STAGE /home/gradle/stubby4j/build/libs/stubby4j*SNAPSHOT.jar ./stubby4j.jar
RUN chown stubby4j:stubby4j stubby4j.jar && java -version && ls -al
COPY --from=BUILD_JAR_STAGE /home/gradle/stubby4j/docker/log4j2-for-docker.xml ./log4j2.xml
RUN chown stubby4j:stubby4j stubby4j.jar && \
chown stubby4j:stubby4j log4j2.xml && \
java -version && \
ls -al

# Set the UID and GID to stubby4j for the ENTRYPOINT instructions
USER stubby4j:stubby4j

# Mark the 'data' directory as volume
VOLUME "$STUBBY4J_USER_HOME/data"

# Expose the three stubby4j ports and run the JAR
EXPOSE $ADMIN_PORT $STUBS_PORT $STUBS_TLS_PORT
ENTRYPOINT java -jar stubby4j.jar \
ENTRYPOINT java -Dlog4j2.configurationFile=log4j2.xml -jar stubby4j.jar \
--location ${LOCATION} \
--admin ${ADMIN_PORT} \
--stubs ${STUBS_PORT} \
--tls ${STUBS_TLS_PORT} \
--data data/${YAML_CONFIG} \
--mute \
${WITH_STUB_CACHE_DISABLED} \
${WITH_DEBUG} \
${WITH_WATCH}
42 changes: 42 additions & 0 deletions docker/log4j2-for-docker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">

<!-- Logging Properties -->
<Properties>
<Property name="LOG_PATTERN">%d{ISO8601_OFFSET_DATE_TIME_HHCMM} %p %c{1.} %m%n</Property>
<!-- Docker images mount 'data' as volume -->
<Property name="APP_LOG_ROOT">data/logs</Property>
<Property name="LOG_NAME">stubby4j</Property>
</Properties>

<Appenders>

<!-- Console Appender -->
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>

<!-- Rolling Random Access File Appender -->
<RollingRandomAccessFile name="LogToRollingFile" fileName="${APP_LOG_ROOT}/${LOG_NAME}.log"
filePattern="${APP_LOG_ROOT}/${LOG_NAME}-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10240KB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingRandomAccessFile>

</Appenders>

<Loggers>
<!-- Avoid duplicated logs with additivity=false -->
<Logger name="io.github.azagniotov.stubby4j" level="DEBUG" additivity="false">
<AppenderRef ref="LogToRollingFile"/>
<AppenderRef ref="LogToConsole"/>
</Logger>

<Root level="INFO"/>
</Loggers>

</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import static io.github.azagniotov.stubby4j.utils.FileUtils.BR;
Expand All @@ -22,7 +24,10 @@ public final class ConsoleUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleUtils.class);
private static final String DEBUG_INCOMING_ASSERTING_HTTP_REQUEST_DUMP = " ***** [DEBUG INCOMING ASSERTING HTTP REQUEST DUMP] ***** ";
private static final String DEBUG_INCOMING_ASSERTING_HTTP_REQUEST_DUMP_END = " ***** [/DEBUG INCOMING ASSERTING HTTP REQUEST DUMP] ***** ";

private static final String DEBUG_INCOMING_RAW_HTTP_REQUEST_DUMP = " ***** [DEBUG INCOMING RAW HTTP REQUEST DUMP] ***** ";
private static final String DEBUG_INCOMING_RAW_HTTP_REQUEST_DUMP_END = " ***** [/DEBUG INCOMING RAW HTTP REQUEST DUMP] ***** ";

private static boolean debug = false;

Expand All @@ -48,10 +53,11 @@ public static void logIncomingRequestError(final HttpServletRequest request, fin
private static void logRawIncomingRequest(final HttpServletRequest request) {
ANSITerminal.warn(DEBUG_INCOMING_RAW_HTTP_REQUEST_DUMP);
ANSITerminal.info(HttpRequestUtils.dump(request));
ANSITerminal.warn(DEBUG_INCOMING_RAW_HTTP_REQUEST_DUMP + BR);
ANSITerminal.warn(DEBUG_INCOMING_RAW_HTTP_REQUEST_DUMP_END + BR);

LOGGER.debug(DEBUG_INCOMING_RAW_HTTP_REQUEST_DUMP);
LOGGER.debug(HttpRequestUtils.dump(request));
LOGGER.debug(DEBUG_INCOMING_RAW_HTTP_REQUEST_DUMP);
LOGGER.debug(DEBUG_INCOMING_RAW_HTTP_REQUEST_DUMP_END);
}


Expand All @@ -63,8 +69,22 @@ public static void logIncomingRequest(final HttpServletRequest request) {
request.getRequestURI()
);
ANSITerminal.incoming(logMessage);
LOGGER.info(logMessage);

if (debug) {

final List<String> skipUriPrefix = new LinkedList<String>() {{
add("/ajax");
add("/status");
add("/favicon");
}};

for (final String prefix : skipUriPrefix) {
if (request.getRequestURI().startsWith(prefix)) {
return;
}
}

ConsoleUtils.logRawIncomingRequest(request);
}
}
Expand All @@ -74,10 +94,11 @@ public static void logAssertingRequest(final StubRequest assertingStubRequest) {
if (debug) {
ANSITerminal.warn(DEBUG_INCOMING_ASSERTING_HTTP_REQUEST_DUMP);
ANSITerminal.info(assertingStubRequest.toString());
ANSITerminal.warn(DEBUG_INCOMING_ASSERTING_HTTP_REQUEST_DUMP + BR);
ANSITerminal.warn(DEBUG_INCOMING_ASSERTING_HTTP_REQUEST_DUMP_END + BR);

LOGGER.debug(DEBUG_INCOMING_ASSERTING_HTTP_REQUEST_DUMP);
LOGGER.debug("{}", assertingStubRequest);
LOGGER.debug(DEBUG_INCOMING_ASSERTING_HTTP_REQUEST_DUMP);
LOGGER.debug(DEBUG_INCOMING_ASSERTING_HTTP_REQUEST_DUMP_END);
}
}

Expand Down Expand Up @@ -117,7 +138,9 @@ public static void logUnmarshalledProxyConfig(final StubProxyConfig stubProxyCon
loadedMsgBuilder.append(String.format(" [%s]", stubProxyConfig.getDescription()));
}

ANSITerminal.loaded(loadedMsgBuilder.toString());
final String logMessage = loadedMsgBuilder.toString();
ANSITerminal.loaded(logMessage);
LOGGER.info(logMessage);
}

public static void logUnmarshalledStub(final StubHttpLifecycle lifecycle) {
Expand All @@ -131,7 +154,9 @@ public static void logUnmarshalledStub(final StubHttpLifecycle lifecycle) {
loadedMsgBuilder.append(String.format(" [%s]", lifecycle.getDescription()));
}

ANSITerminal.loaded(loadedMsgBuilder.toString());
final String logMessage = loadedMsgBuilder.toString();
ANSITerminal.loaded(logMessage);
LOGGER.info(logMessage);
}


Expand Down

0 comments on commit a95ba36

Please sign in to comment.