当前位置:   article > 正文

Java程序使用Logback最新版本日志框架_java.lang.string ch.qos.logback.core.util.envutil.

java.lang.string ch.qos.logback.core.util.envutil.logbackversion()

版本信息

框架版本号
logback
1.3.0-alpha4
slf4j
1.8.0-beta2

现在Logback框架最新版本为1.3.0-alpha4,而依赖的slf4j的版本也为最新的版本1.8.0-beta2.

 

Maven依赖

Java开发jar包依赖管理现在主流都是用Maven进行依赖管理,那么开发使用Logback日志框架,只是引入下面依赖即可

1. 在Maven项目pom.xml配置文件properties中添加下如版本信息

  1. <slf4j.version>1.8.0-beta2</slf4j.version>
  2. <logback.version>1.3.0-alpha4</logback.version>

2. 在Maven项目pom.xml配置文件repositories中添加依赖

  1. <dependency>
  2. <groupId>org.slf4j</groupId>
  3. <artifactId>slf4j-api</artifactId>
  4. <version>${slf4j.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>ch.qos.logback</groupId>
  8. <artifactId>logback-core</artifactId>
  9. <version>${logback.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>ch.qos.logback</groupId>
  13. <artifactId>logback-classic</artifactId>
  14. <version>${logback.version}</version>
  15. </dependency>

Logback配置文件

配置logout不仅仅需要依赖包,还需要在resources添加配置文件logback.xml,废话不多说,下面是比较完整的各级别的日志记录配置, 注意查看其中的注解

  1. <configuration>
  2. <!-- 日志文件存储在执行目录同级的logs目录下-->
  3. <property name="PROJECT_HOME" value="${user.dir}/../logs"/>
  4. <!-- 检查系统级别日志,输出到日志文件-->
  5. <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>
  6. <!-- 控制条输出,info以下级别的日志 -->
  7. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  8. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  9. <level>INFO</level>
  10. </filter>
  11. <encoder>
  12. <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
  13. </pattern>
  14. </encoder>
  15. </appender>
  16. <!-- 按照每天生成日志文件,info以下级别的日志 -->
  17. <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
  18. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  19. <level>INFO</level>
  20. </filter>
  21. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  22. <!--日志文件输出的文件名-->
  23. <FileNamePattern>${PROJECT_HOME}/info.%d{yyyy-MM-dd}.log</FileNamePattern>
  24. <!--日志文件保留天数-->
  25. <MaxHistory>30</MaxHistory>
  26. </rollingPolicy>
  27. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
  28. <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
  29. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{20} - %msg%n</pattern>
  30. </encoder>
  31. <!--日志文件最大的大小-->
  32. <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  33. <MaxFileSize>200MB</MaxFileSize>
  34. </triggeringPolicy>
  35. </appender>
  36. <!--debug以下级别的日志,按照文件大小存储-->
  37. <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
  38. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  39. <level>DEBUG</level>
  40. </filter>
  41. <file>${PROJECT_HOME}/debug.log</file>
  42. <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
  43. <fileNamePattern>${PROJECT_HOME}/debug.%i.log</fileNamePattern>
  44. <minIndex>1</minIndex>
  45. <maxIndex>100</maxIndex>
  46. </rollingPolicy>
  47. <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  48. <maxFileSize>200MB</maxFileSize>
  49. </triggeringPolicy>
  50. <encoder>
  51. <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{30} - %msg%n
  52. </pattern>
  53. </encoder>
  54. </appender>
  55. <!--仅存储warn级别日志,按照文件大小存储-->
  56. <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
  57. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  58. <level>WARN</level>
  59. <onMatch>ACCEPT</onMatch>
  60. <onMismatch>DENY</onMismatch>
  61. </filter>
  62. <file>${PROJECT_HOME}/warn.log</file>
  63. <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
  64. <fileNamePattern>${PROJECT_HOME}/warn.%i.log</fileNamePattern>
  65. <minIndex>1</minIndex>
  66. <maxIndex>100</maxIndex>
  67. </rollingPolicy>
  68. <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  69. <maxFileSize>50MB</maxFileSize>
  70. </triggeringPolicy>
  71. <encoder>
  72. <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{30} - %msg%n
  73. </pattern>
  74. </encoder>
  75. </appender>
  76. <!--仅存储error级别日志,按照文件大小存储-->
  77. <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
  78. <filter class="ch.qos.logback.classic.filter.LevelFilter">
  79. <level>ERROR</level>
  80. <onMatch>ACCEPT</onMatch>
  81. <onMismatch>DENY</onMismatch>
  82. </filter>
  83. <file>${PROJECT_HOME}/error.log</file>
  84. <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
  85. <fileNamePattern>${PROJECT_HOME}/error.%i.log</fileNamePattern>
  86. <minIndex>1</minIndex>
  87. <maxIndex>100</maxIndex>
  88. </rollingPolicy>
  89. <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  90. <maxFileSize>50MB</maxFileSize>
  91. </triggeringPolicy>
  92. <encoder>
  93. <pattern>%d{yyyy/MM/dd HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{30} - %msg%n
  94. </pattern>
  95. </encoder>
  96. </appender>
  97. <!-- 具体类debug日志存储到debug.log日志文件 -->
  98. <logger name="java.sql.Connection" additivity="false" level="DEBUG">
  99. <appender-ref ref="DEBUG"/>
  100. </logger>
  101. <logger name="java.sql.Statement" additivity="false" level="DEBUG">
  102. <appender-ref ref="DEBUG"/>
  103. </logger>
  104. <logger name="java.sql.PreparedStatement" additivity="false" level="DEBUG">
  105. <appender-ref ref="DEBUG"/>
  106. </logger>
  107. <logger name="org.apache.hive.jdbc.Utils" additivity="false" level="DEBUG">
  108. <appender-ref ref="DEBUG"/>
  109. </logger>
  110. <!-- 控制台系统异常日志输出到INFO、ERROR、DEBUG日志文件中-->
  111. <root level="ERROR">
  112. <appender-ref ref="STDOUT"/>
  113. <appender-ref ref="INFO"/>
  114. <appender-ref ref="ERROR"/>
  115. <appender-ref ref="DEBUG"/>
  116. </root>

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/595041
推荐阅读
相关标签
  

闽ICP备14008679号