分享

SpringBoot应用与原理之集成Log4j2

 quasiceo 2018-08-19

一 本章概述

SpringBoot应用替换默认的Logback日之框架,采用Log4j2实现

二 SpringBoot应用集成Log4j2

1 替换默认的logback

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-logging</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2 加入基于SpringBoot的Log4j2 Starter

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-log4j2</artifactId>
  4. </dependency>
  • 1
  • 2
  • 3
  • 4

3 配置log4j2.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--设置log4j2的自身log级别为warn -->
  3. <configuration status="warn">
  4. <properties>

  5. <Property name="app_name">springboot-web</Property>
  6. <Property name="log_path">logs/${app_name}</Property>

  7. </properties>
  8. <appenders>
  9. <console name="Console" target="SYSTEM_OUT">
  10. <PatternLayout pattern="[%d][%t][%p][%l] %m%n" />
  11. </console>

  12. <RollingFile name="RollingFileInfo" fileName="${log_path}/info.log"
  13. filePattern="${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz">
  14. <Filters>
  15. <ThresholdFilter level="INFO" />
  16. <ThresholdFilter level="WARN" onMatch="DENY"
  17. onMismatch="NEUTRAL" />
  18. </Filters>
  19. <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
  20. <Policies>
  21. <!-- 归档每天的文件 -->
  22. <TimeBasedTriggeringPolicy interval="1" modulate="true" />
  23. <!-- 限制单个文件大小 -->
  24. <SizeBasedTriggeringPolicy size="2 MB" />
  25. </Policies>
  26. <!-- 限制每天文件个数 -->
  27. <DefaultRolloverStrategy compressionLevel="0" max="10"/>
  28. </RollingFile>

  29. <RollingFile name="RollingFileWarn" fileName="${log_path}/warn.log"
  30. filePattern="${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz">
  31. <Filters>
  32. <ThresholdFilter level="WARN" />
  33. <ThresholdFilter level="ERROR" onMatch="DENY"
  34. onMismatch="NEUTRAL" />
  35. </Filters>
  36. <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
  37. <Policies>
  38. <!-- 归档每天的文件 -->
  39. <TimeBasedTriggeringPolicy interval="1" modulate="true" />
  40. <!-- 限制单个文件大小 -->
  41. <SizeBasedTriggeringPolicy size="2 MB" />
  42. </Policies>
  43. <!-- 限制每天文件个数 -->
  44. <DefaultRolloverStrategy compressionLevel="0" max="10"/>
  45. </RollingFile>

  46. <RollingFile name="RollingFileError" fileName="${log_path}/error.log"
  47. filePattern="${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz">
  48. <ThresholdFilter level="ERROR" />
  49. <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
  50. <Policies>
  51. <!-- 归档每天的文件 -->
  52. <TimeBasedTriggeringPolicy interval="1" modulate="true" />
  53. <!-- 限制单个文件大小 -->
  54. <SizeBasedTriggeringPolicy size="2 MB" />
  55. </Policies>
  56. <!-- 限制每天文件个数 -->
  57. <DefaultRolloverStrategy compressionLevel="0" max="10"/>
  58. </RollingFile>

  59. <!-- 配置mongdb appender -->
  60. </appenders>

  61. <loggers>
  62. <!--过滤掉spring和hibernate的一些无用的debug信息 -->

  63. <root level="info">
  64. <appender-ref ref="Console" />
  65. <appender-ref ref="RollingFileInfo" />
  66. <appender-ref ref="RollingFileWarn" />
  67. <appender-ref ref="RollingFileError" />
  68. <!-- 输出日志到mongodb -->
  69. </root>

  70. </loggers>

  71. </configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

4 配置appliaction.properties

logging.config=classpath:log4j2.xml
  • 1

三 Log4j2的应用

  1. package com.ekeyfund.framework.springboot.web.controller;

  2. import org.apache.logging.log4j.LogManager;
  3. import org.apache.logging.log4j.Logger;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;

  6. /**
  7. * IndexController
  8. *
  9. * @author tony 18601767221@163.com
  10. * @create 2017-09-14-下午4:51
  11. * @see
  12. * @since JDK1.8u141
  13. */
  14. @RestController
  15. public class IndexController {


  16. private static final Logger logger = LogManager.getLogger(IndexController.class);

  17. @GetMapping("/index")
  18. public String index(){

  19. for(int i=0;i<10_0000;i++){
  20. logger.info("info execute index method");
  21. logger.warn("warn execute index method");
  22. logger.error("error execute index method");

  23. }


  24. return "My First SpringBoot Application";
  25. }
  26. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多