B
B
bigMOTOR2015-06-28 18:29:00
Java
bigMOTOR, 2015-06-28 18:29:00

Spring-Logback-Maven - how to move Logback settings to Spring application.properties?

Good afternoon!
Please help with Spring-Logback-Maven. I've already broken my head (((
So, Spring, Logback and Maven are used. I want to collect all the settings in Spring's application.properties, which will be outside the Jar collected by Maven.
I took out all the settings of interest from Logback.xml, now it's like this:

<configuration>

    <property resource="application.properties" />

    <timestamp key="byDate" datePattern="yyyyMMdd"/>

    <!-- Send messages to System.out - CONSOLE -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
        <withJansi>true</withJansi>
    </appender>

    <!-- Send messages to a file -->
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${logging.path}/${spring.application.name}-${byDate}.log</file>
        <append>true</append>
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="${logging.level}">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

Entered in application.properties:
#Spring settings
spring.application.name=MyApp
server.port=8087

# Logging settings
logging.level=INFO
logging.path=/Users/...some path.../logs/

Introduced in beans look for application.properties next:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:application.properties"/>
    </bean>

In pom file excluded application.properties from Jar:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <excludes>
        <exclude>path/to/application.properties</exclude>
      </excludes>
    </configuration>
  </plugin>

And added a simple copy alongside:
<plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <copy todir="target" overwrite="true">
                                    <fileset dir="src/main/resources/">
                                        <include name="*.properties"/>
                                    </fileset>
                                </copy>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

In the IDE, everything works fine - as it should. But after the assembly by Maven and the launch of the Jar, the file name template is lost (it just becomes Spring.log) and the logging level is not pulled up. At the same time, the path to the logs is pulled up, there are no errors.
I would be very, very grateful for your help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question