0

We have a common logback.xml file that we would like to use across different web apps. It includes a RollinFileAppender which should see files named as the project artifactId. The logback.xml includes a property like so

<property name="LOG_FILE_NAME" value="$project.artifactId}"/>

Within our web project we would like to include a dependency e.g. logging-setup

<dependency>
    <groupId>our.group.id</groupId>
    <artifactId>logging-setup</artifactId>
</dependency>

How do we easily allow the maven-war-plugin to filter this file so that the ${project.artifactId} reference is replaced with the actual project.artifactId? I think it can be done using a combination of the maven-dependency-plugin and the maven-war-plugin something like below. However that would need to be included in every project POM. Is there an easier way?

Thanks, Paul

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>${maven-dependency-plugin.version}</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>our.group.id</groupId>
                                    <artifactId>logging-setup</artifactId>
                                    <version>${logging-setup.version}</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${basedir}/src/main/resources</outputDirectory>
                                    <includes>**/logback.xml</includes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/resources</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF/classes</targetPath>
                            <includes>
                                <include>logback.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                    <packagingExcludes>WEB-INF/lib/logging-setup-*.jar</packagingExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
2
  • Would it be ok to pass in the value as a system property or environment variable and let logback look up the value itself at runtime? Commented Aug 10, 2022 at 8:47
  • It is possible but would still require the setup for everything we did. We were hoping for something a bit simpler that meant everything we do gets the correct name in log files without much in the way of setup. Commented Aug 10, 2022 at 9:00

1 Answer 1

1

After this logback.xml file is put inside an artifact jar file, it cannot be changed easily anymore during build time.

I would suggest a slightly different approach:

  • rename this file to e.g. logback-YOURCOMPANY.xml and change it so it can be included.
  • create a tiny logback.xml file put in each project as-is which sets the property as you do, and then includes logback-YOURCOMPANY.xml
  • enable filtering on this tiny logback.xml file so the variable is expanded during Maven build.

See https://logback.qos.ch/manual/configuration.html#fileInclusion for details.

Sign up to request clarification or add additional context in comments.

1 Comment

This sounds reasonable. I'll give that a try. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.