1.背景:
mac 笔记本开发,日志文件写到/data/logs/下,控制台报出:Failed to create parent directories for [/data/logs/...........
再去手动在命令窗口创建文件夹data,报Read-only file system
2.修改logback-spring.xml文件:
resource 目录下的配置文件列表:
在logback-spring.xml添加配置, 只有local写到当前项目下的data目录,其他环境写到系统根目录/data下
<springProfile name="local"> <property name="LOG_HOME" value="./data/logs/${appName}/${hostname}/"/> </springProfile>
完整的logback-spring.xm
XML
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<springProperty scope="context" name="appName" source="spring.application.name"
defaultValue="onemap-quality-manage-service"/>
<springProperty scope="context" name="ip" source="spring.cloud.client.ip-address"/>
<property name="CONSOLE_LOG_PATTERN"
value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [${ip} ${appName}] [%(%-5level) %logger{80}:%line]:%msg%n"/>
<property name="LOG_HOME" value="/data/logs/${appName}/${hostname}/"/>
<springProfile name="local">
<property name="LOG_HOME" value="./data/logs/${appName}/${hostname}/"/>
</springProfile>
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 日志文件:按照每天生成 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${appName}-${ip}-info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件地址和名称-->
<FileNamePattern>${LOG_HOME}/${appName}-${ip}-info-%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- error日志 -->
<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${appName}-${ip}-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件地址和名称-->
<FileNamePattern>${LOG_HOME}/${appName}-${ip}-error-%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level><!-- 只接收错误级别的日志 -->
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--设置框架的日志-->
<logger name="org.springframework" level="INFO"/>
<logger name="org.apache" level="WARN"/>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
<appender-ref ref="ERROR"/>
</root>
</configuration>
3.profiles.active 指定配置的local
日志写到当前项目下的/data/目录