我正在尝试将logback.xml用于我的项目,并且为了开发我正在使用Windows Env,用于部署,我正在使用Unix Env,因此我在XML文件下创建了Unix Env。
但是,当我的应用程序开始看起来像是试图验证指定的路径。最终出现以下错误。
如何使用logback?
实现此目标?java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[UNIX_FILE] - Failed to create parent directories for [C:asUserssatishknappsworkspaceslogscarats-api-2019-02-18.log]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[UNIX_FILE] - openFile(null,true) call failed. java.io.FileNotFoundException: asUserssatishknappsworkspaceslogscarats-api-2019-02-18.log (The system cannot find the path specified)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:169)
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="windows_log_dir" value="./logs/" />
<property name="unix_log_dir" value="/as/Users/satishkn/apps/workspaceslogs/" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n
</pattern>
</encoder>
</appender>
<appender name="WIN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${windows_log_dir}carats-api-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
</Pattern>
</encoder>
</appender>
<appender name="UNIX_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${unix_log_dir}carats-api-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
</Pattern>
</encoder>
</appender>
<springProfile name="default">
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="WIN_FILE" />
</root>
</springProfile>
<!-- <springProfile name="dev">
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="UNIX_FILE" />
</root>
</springProfile>
<springProfile name="prod">
<root level="info">
<appender-ref ref="UNIX_FILE" />
</root>
</springProfile> -->
</configuration>
使用您的配置,两个附录都将在UNIX和Windows Environnement中启动。您应该使用条件配置来满足您的要求。
我建议您以这种方式定义log_dir param:
<if condition='property("os.name").contains("win")'>
<then>
<property name="log_dir" value="./logs/" />
</then>
<else>
<property name="log_dir" value="/as/Users/satishkn/apps/workspaceslogs/" />
</else>
</if>
然后创建一个使用log_dir属性的单个appender。