Spring boot中log4j2配置yml方式并发送邮件

(1) 引包

(注:当排除默认logback日志时,放spring-boot-starter-web下不能把logback清理干净,报Class path contains multiple SLF4J bindings.)

<!-- 发邮件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- log related -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions><!-- 去掉默认log配置 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- 引入log4j2依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 支持识别yml配置 -->
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
    <version>2.9.7</version>
</dependency>
<!-- 解析yml配置 -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.7</version>
</dependency>
<!-- end of log related -->

(2) 在resource下创建log4j2.yml文件

Configuration:
  appenders:
    Console:
      name: Console
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
    RollingFile:
      -
        name: ROLLING_FILE
        ignoreExceptions: false
        fileName: ../logs/log4j2.log
        filePattern: "../logs/log4j2-%d{yyyy-MM-dd}-%i.log"
        PatternLayout:
          Pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
        Policies:
          TimeBasedTriggeringPolicy:
            modulate: true
            interval: 1
          SizeBasedTriggeringPolicy:
            size: 2MB
        DefaultRolloverStrategy:
          max: 20
    SMTP:
      name: Mailer
      subject: ERROR LOG
      to: tiantaoyin@foxmail.com # 多收件人(以逗号隔开 ****@qq.com,****@163.com)
      from:*****@qq.com
      smtpHost: smtp.qq.com
      smtpPort: 25
      smtpProtocol: smtp
      smtpUsername: *****@qq.com
      smtpPassword: yourpassword
      smtpDebug: false    # 是否开启发邮件调试模式
      bufferSize: 1024
#      Async:
#        name: AsyncMailer
#        appender-ref: Mailer
  Loggers:
    root:
      level: INFO
      appender-ref:
        -
          ref: Console
        -
          ref: ROLLING_FILE
    Logger: #单独设置某些包的输出级别
      -
        name: org.springframework.web
        additivity: false #去除重复的log
        level: error
        appender-ref:
          -
            ref: Console
          -
            ref: ROLLING_FILE
      -
        name: org.springboot.sample
        additivity: false
        level: error
        appender-ref:
          -
            ref: Console
          -
            ref: ROLLING_FILE
      -
        name: com.alibaba.druid
        additivity: false
        level: error
        appender-ref:
          -
            ref: Console
          -
            ref: ROLLING_FILE
      -
        name: AsyncMailerLogger
        additivity: false
        level: error
        appender-ref:
          -
            ref: Mailer

(3) 使用方式:【注:application.yml中就不必设置logger.config了】

private static final Logger LOGGER_MAIL = LoggerFactory.getLogger("AsyncMailerLogger");
LOGGER_MAIL.error("你好!");

总结:

       1. 当把使用方式放spring boot的mian方法下使用时,会直接运行被拦截死,因为bean创建都未完成,直接调用肯定失败。

       2. log4j2与logback的使用:lombok 提供了@Slf4j注解 @Log4j2注解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值