简体   繁体   中英

Configure Wiremock to use log4j with a Custom Transformer

I'm running Wiremock as a Standalone process (v2.5.1). I've created a Java custom transformer by extending: com.github.tomakehurst.wiremock.extension.ResponseTransformer

My custom transformer then uses some other common code that uses Log4J for logging. With code like:

import org.apache.log4j.Logger; private static Logger logger = Logger.getLogger(CommonCode.class); ... logger.error("This is some error");

Is there anyway I can configure Wiremock to output this custom logging? I've tried putting a log4j.xml and log4j.properties file in the classpath. Here's an example of a properties file:

log4j.appender.CUSTOMAPPENDER=org.apache.log4j.RollingFileAppender
log4j.appender.CUSTOMAPPENDER.File=c:/WireMock/logs/custom.log
log4j.appender.CUSTOMAPPENDER.layout=org.apache.log4j.PatternLayout

log4j.logger.com.myorg=DEBUG, CUSTOMAPPENDER

The equivalent log4j.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="CUSTOMAPPENDER" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="C:/WireMock/logs/custom.log"/>
        <param name="datePattern" value="'.'yyyy-MM-dd"/>
        <param name="append" value="true"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{MMM dd HH:mm:ss}] [%r] %5p [%t] (%F:%L) - %m%n"/>
        </layout>
    </appender>
    <logger name="com.myorg" additivity="false">
        <level value="DEBUG"/>
        <appender-ref ref="CUSTOMAPPENDER"/>
    </logger>   
</log4j:configuration>

I also have the following JARs in my classpath:

  • log4j-1.2.17.jar
  • slf4j-api-1.7.2.jar
  • slf4j-log4j12-1.7.2.jar

I'd like this custom logging to go to a separate log file from the Default Wiremock verbose logging. Any help would be appreciated.

Once I realised it was simply that log4j could not find the log4j.properties or log4j.xml file I was able to resolve the issue. Basically the log4j properties file must be in the classpath for the Wiremock Java process.

I was running the Wiremock Server via a batch script which looked like this:

"C:/Java/jdk1.8.0_102/bin/java" -Dfile.encoding=UTF-8 -cp "C:/WireMock/lib/wiremock-standalone-2.5.1.jar;C:/WireMock/lib/*" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --port="9091" --extensions "com.myorg.CustomTransformer" --root-dir="C:/WireMock" --verbose > "C:/WireMock/logs/wiremock.log"

My log4j.xml file was in my C:/WireMock/lib directory. I assumed that because I had a classpath entry of cp "C:/WireMock/lib/wiremock-standalone-2.5.1.jar;C:/WireMock/lib/*" which included C:/WireMock/lib/* that it would pick up my log4j properties. I also explicitly added the properties file to this classpath. Neither of these worked (hence this question). My Custom transformer class com.myorg.CustomTransformer is present in a jar also in the lib directory (which is picked up).

However after some research I found 2 ways to get the log4j properties file to be picked up.

  1. Specify the log4j properties location as another -D parameter in the batch script. If I added -Dlog4j.configuration=file://c:/WireMock/lib/log4j.xml
  2. Update my custom jar build script to include the log4j.xml file itself in the jar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM