• springboot Logback 不同环境,配置不同的日志输出路径


    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下

    
        
    

    完整的logback-spring.xm

    1. "1.0" encoding="UTF-8"?>
    2. <configuration debug="false">
    3. <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    4. <springProperty scope="context" name="appName" source="spring.application.name"
    5. defaultValue="onemap-quality-manage-service"/>
    6. <springProperty scope="context" name="ip" source="spring.cloud.client.ip-address"/>
    7. <property name="CONSOLE_LOG_PATTERN"
    8. value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [${ip} ${appName}] [%(%-5level) %logger{80}:%line]:%msg%n"/>
    9. <property name="LOG_HOME" value="/data/logs/${appName}/${hostname}/"/>
    10. <springProfile name="local">
    11. <property name="LOG_HOME" value="./data/logs/${appName}/${hostname}/"/>
    12. springProfile>
    13. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    14. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    15. <pattern>${CONSOLE_LOG_PATTERN}pattern>
    16. <charset>UTF-8charset>
    17. encoder>
    18. appender>
    19. <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    20. <file>${LOG_HOME}/${appName}-${ip}-info.logfile>
    21. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    22. <FileNamePattern>${LOG_HOME}/${appName}-${ip}-info-%d{yyyy-MM-dd}.logFileNamePattern>
    23. <MaxHistory>30MaxHistory>
    24. rollingPolicy>
    25. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    26. <pattern>${CONSOLE_LOG_PATTERN}pattern>
    27. <charset>UTF-8charset>
    28. encoder>
    29. appender>
    30. <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
    31. <file>${LOG_HOME}/${appName}-${ip}-error.logfile>
    32. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    33. <FileNamePattern>${LOG_HOME}/${appName}-${ip}-error-%d{yyyy-MM-dd}.logFileNamePattern>
    34. <MaxHistory>30MaxHistory>
    35. rollingPolicy>
    36. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    37. <pattern>${CONSOLE_LOG_PATTERN}pattern>
    38. <charset>UTF-8charset>
    39. encoder>
    40. <filter class="ch.qos.logback.classic.filter.LevelFilter">
    41. <level>ERRORlevel>
    42. <onMatch>ACCEPTonMatch>
    43. <onMismatch>DENYonMismatch>
    44. filter>
    45. appender>
    46. <logger name="org.springframework" level="INFO"/>
    47. <logger name="org.apache" level="WARN"/>
    48. <root level="INFO">
    49. <appender-ref ref="STDOUT"/>
    50. <appender-ref ref="FILE"/>
    51. <appender-ref ref="ERROR"/>
    52. root>
    53. configuration>

    3.profiles.active 指定配置的local

    日志写到当前项目下的/data/目录

  • 相关阅读:
    升级ios16后iphone无法识别SIM?一招解决这个问题!
    DVWA-弱会话IDS
    直播预告 | 这是一场你绝对不能错过的电商风控实操课程
    【ubuntu】 Linux(ubuntu)创建python的虚拟环境
    Mysql 读写分离
    视觉slam十四讲CH5 ---相机与图像
    (Python高级编程)第一章:Python面向对象深刻认识
    ClearML入门:简化机器学习解决方案的开发和管理
    023-普通内部类与静态内部类
    使用Amazon Elasticache构建你的专属内存缓存服务集群
  • 原文地址:https://blog.csdn.net/qiaobing1226/article/details/137862957