Search code examples
xmljenkins-pluginslogbackkarate

Masking Log Output Value for Dynamic File Name in Logback XML


I have a value I need masked in log output that is used to generate the Cucumber Reports for my Jenkins jobs. The only way this seems to be possible is modifying the logback.xml file for the repo.

I've tried:

<appender name="REPORT" class="ch.qos.logback.core.FileAppender">
    <immediateFlush>false</immediateFlush>
    <file>target/surefire-reports/*.html</file>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %replace(%msg){'(?&lt;=api-key:).*', 'xxxx'}%n</pattern>
    </encoder>
</appender>

But I get an incorrect filename error:

16:58:44,485 |-ERROR in ch.qos.logback.core.FileAppender[REPORT] - openFile(target/surefire-reports/*.html,true) call failed. java.io.FileNotFoundException: target\surefire-reports\*.html (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileNotFoundException: target\surefire-reports\*.html (The filename, directory name, or volume label syntax is incorrect)

Solution

  • And of course as soon as I ask, I figure it out...

    <pattern>%-5level - %replace(%msg){'(?&lt;=api-key:).*', 'xxxx'}%n</pattern>

    Output: 1 > api-key:xxxx

    Got the format from logback documentation: https://logback.qos.ch/manual/layouts.html#cwOptions <pattern>%-5level - %replace(%msg){'\d{14,16}', 'XXXX'}%n</pattern>

    And the regex is the one I'm using to mask Jenkins log output, replacing the left angle bracket with html &lt; due to it being in an xml file.