Springboot 使用logback直接将日志写入Elasticsearch

java1234

共 2535字,需浏览 6分钟

 ·

2020-12-18 15:24

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达

  作者 |  inkanyway

来源 |  urlify.cn/BbEzay

66套java从入门到精通实战课程分享

正常情况下,一般组合为elk 即日志会通过logstash写入es,但本文主要为轻量级项目直接利用appender写入es

首先需要引入包

    
            com.internetitem
            logback-elasticsearch-appender
            1.6
        

添加logback-spring.xml到resource目录的根目录下

"1.0" encoding="UTF-8"?>


    "LOGS" value="./logs"/>

    "Console"
              class="ch.qos.logback.core.ConsoleAppender">
        "ch.qos.logback.classic.PatternLayout">
            
                %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
            

        
    

    "RollingFile"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        ${LOGS}/spring-boot-logger.log
                        class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            %d %p %C{1.} [%t] %m%n
        

                        class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            ${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
            

                                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                10MB
            
        
    
    "ELASTIC" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
        ES连接字符串
        linkanyway-logs-%date{yyyy-MM-dd}
        <type>testertype>
        linkanyway-logger 
        linkanyway-error-logger 
        30000 
        false 
        false 
        false 
        104857600 
        3 
        30000 
        250 
        false 
        false 
        100 
        "com.internetitem.logback.elasticsearch.config.BasicAuthentication"/> 
        
            
                host
                ${HOSTNAME}
                false
            

            
                severity
                %level
            

            
                thread
                %thread
            

            
                stacktrace
                %ex
            

            
                logger
                %logger
            

        

        
            

                Content-Type
                application/json
            

        

    

    
    "info">
        "RollingFile"/>
        "Console"/>
      
        "ELASTIC"/>
    

    
    "com.baeldung" level="trace" additivity="false">
        "RollingFile"/>
        "ELASTIC"/>
    

   
    "es-logger" level="INFO" additivity="false">
        "ELASTIC"/>
    

在application.yml中设定日志level

logging:
  level:
    root: info

另外我们也可以在root部分不适用es记录日志而在需要的代码中用如下方式记录日志

package com.linkanyway.portal.controllers;

import com.linkanyway.sdk.exception.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author linkanyway
 * @version 1.0
 * @name EsLoggerController
 * @description TODO
 * @date 2020/12/13 13:14
 */
@RestController
@RequestMapping("log")
public class EsLoggerController {
    private Logger logger = LoggerFactory.getLogger("es-logger");

    @GetMapping("log")
    public ApiResult log()
    {
        logger.warn ("eslogger");
        return ApiResult.success ("done!");

    }
}







粉丝福利:Java从入门到入土学习路线图

???

?长按上方微信二维码 2 秒


感谢点赞支持下哈 

浏览 57
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报