Java – why Catalina home_ IS_ The undefined directory is generated by logback in the same project directory?
•
Java
I wrote the logback configuration file for my application, but when I was doing Maven clean install (MVN clean install), it generated a Catalina with log files in the project directory home_ IS_ Undefined directory
I don't want it to appear in my project directory
What can help solve this problem?
This is the configuration file
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{HH:mm:ss.SSS} %-5p [%t] %c{1} - %m%n</pattern>
</encoder>
</appender>
<appender name="MY_APP_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.home}/logs/myApplication.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${catalina.home}/logs/myApplication.%d{yyyy-MM-dd}.log</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%-5p %date{HH:mm:ss.SSS} [%t] %c{1} - %m%n</pattern>
</encoder>
<append>true</append>
</appender>
<logger name="org.springframework" level="WARN"/>
<root>
<priority value="INFO"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="MY_APP_LOG"/>
</root>
</configuration>
Solution
background
This property failed to load because it is only populated by Tomcat It is not populated by Maven compilation tasks
Solution 1
In logback XML
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<if condition='isDefined("catalina.home")'>
<then>
<property name="log.folder" value="${catalina.home}/logs"/>
</then>
<else>
<property name="log.folder" value="./target/logs"/>
</else>
</if>
<appender name="companyMyAppServiceAppender" class="ch.qos.logback.core.FileAppender">
<file>${log.folder}/company.myApp.log</file>
...
</appender>
...
</configuration>
This will create a log file in the target folder at compile time, which will be cleared
Solution 2
Override value:
<property name="log.folder" value="./target/logs"/>
<if condition='isDefined("catalina.home")'>
<then>
<property name="log.folder" value="${catalina.home}/logs"/>
</then>
</if>
NB!
And don't forget to import maven janino dependencies
<!-- The org.codehaus.janino:commons-compiler:2.7.8 dependency -->
<!-- will be automatically pulled in by Maven's transitivity rules -->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>2.7.8</version>
</dependency>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
