• Java 项目 服务器 日志配置



    最近 在搞一个0-1的项目 就想把 服务器日志配置 记录一下
    我们使用的是 单体微服务项目
    首先你需要一个xml

    1. "1.0" encoding="UTF-8"?>
    2. <configuration>
    3. <springProperty scope="context" name="logPath" source="log.path" defaultValue="logs"/>
    4. <springProperty scope="context" name="logMaxFileSize" source="log.maxFileSize" defaultValue="50MB"/>
    5. <springProperty scope="context" name="logMaxHistory" source="log.maxHistory" defaultValue="45"/>
    6. <springProperty scope="context" name="totalLogSizeCap" source="log.totalLogSizeCap" defaultValue="20GB"/>
    7. <springProperty scope="context" name="errorLogSizeCap" source="log.errorLogSizeCap" defaultValue="5GB"/>
    8. <springProfile name="dev | uat | ksh | ysh">
    9. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    10. <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    11. <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
    12. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{tid}] [%thread] %-5level %logger Line:%-3L - %msg%npattern>
    13. layout>
    14. encoder>
    15. appender>
    16. <root level="info">
    17. <appender-ref ref="STDOUT"/>
    18. root>
    19. springProfile>
    20. <springProfile name="dev">
    21. <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
    22. <file>${logPath}/log_error.logfile>
    23. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    24. <fileNamePattern>${logPath}/error/log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
    25. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
    26. <maxFileSize>${logMaxFileSize}maxFileSize>
    27. timeBasedFileNamingAndTriggeringPolicy>
    28. <maxHistory>${logMaxHistory}maxHistory>
    29. <totalSizeCap>${errorLogSizeCap}totalSizeCap>
    30. <cleanHistoryOnStart>truecleanHistoryOnStart>
    31. rollingPolicy>
    32. <append>trueappend>
    33. <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    34. <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
    35. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger Line:%-3L - %msg%npattern>
    36. layout>
    37. encoder>
    38. <filter class="ch.qos.logback.classic.filter.LevelFilter">
    39. <level>errorlevel>
    40. <onMatch>ACCEPTonMatch>
    41. <onMismatch>DENYonMismatch>
    42. filter>
    43. appender>
    44. <appender name="FILE_ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
    45. <file>${logPath}/log_total.logfile>
    46. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    47. <fileNamePattern>${logPath}/total/log-total-%d{yyyy-MM-dd}.%i.logfileNamePattern>
    48. <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
    49. <maxFileSize>${logMaxFileSize}maxFileSize>
    50. timeBasedFileNamingAndTriggeringPolicy>
    51. <maxHistory>${logMaxHistory}maxHistory>
    52. <totalSizeCap>${totalLogSizeCap}totalSizeCap>
    53. <cleanHistoryOnStart>truecleanHistoryOnStart>
    54. rollingPolicy>
    55. <append>trueappend>
    56. <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    57. <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
    58. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger Line:%-3L - %msg%npattern>
    59. layout>
    60. encoder>
    61. appender>
    62. <root level="info">
    63. <appender-ref ref="FILE_ERROR"/>
    64. <appender-ref ref="FILE_ALL"/>
    65. root>
    66. springProfile>
    67. configuration>

    然后就是 pom文件里面添加上对应的依赖

    1. <exclusions>
    2. <exclusion>
    3. <groupId>org.slf4jgroupId>
    4. <artifactId>slf4j-simpleartifactId>
    5. exclusion>
    6. <exclusion>
    7. <groupId>org.slf4jgroupId>
    8. <artifactId>slf4j-apiartifactId>
    9. exclusion>
    10. <exclusion>
    11. <groupId>org.slf4jgroupId>
    12. <artifactId>slf4j-nopartifactId>
    13. exclusion>
    14. <exclusion>
    15. <groupId>org.slf4jgroupId>
    16. <artifactId>slf4j-log4j12artifactId>
    17. exclusion>
    18. <exclusion>
    19. <groupId>org.slf4jgroupId>
    20. <artifactId>slf4j-jdk14artifactId>
    21. exclusion>
    22. <exclusion>
    23. <groupId>ch.qos.logbackgroupId>
    24. <artifactId>logback-classicartifactId>
    25. exclusion>
    26. <exclusion>
    27. <groupId>org.springframework.bootgroupId>
    28. <artifactId>spring-boot-starter-loggingartifactId>
    29. exclusion>
    30. exclusions>

    然后 yml 里面

    logging.config=classpath:logback-spring.xml
    logging.level.root=info
    logging.level.com=debug
    logging.level.org.springframework.boot=ERROR
    
    log.path=/Users/meiyifan/code/logs
    log.maxFileSize=50MB
    log.maxHistory=45
    
    log.totalLogSizeCap=20GB
    log.errorLogSizeCap=5GB
    systemLog.enable=true
    systemLogretentiondays=30
    

    然后再服务器对应的创建 存储日志的文件 logs

     

  • 相关阅读:
    xshell 判断文件超出指定大小则清空文件
    ES6 class 类
    Flutter 精品项目大全之 侧边栏银行管理完成App(教程含源码)
    HTML学生个人网站作业设计:基于HTML+CSS+JavaScript设计多用途的图文展示博客HTML模板(16页)
    video caption with frame selection【论文阅读】
    MySQL 索引
    Python3学习
    【数据结构】830+848真题易错题汇总(10-23)
    Matlab实现SUSAN角点检测
    每日三题 7.5
  • 原文地址:https://blog.csdn.net/Fristm/article/details/134116791