Springboot 使用logback直接将日志写入Elasticsearch,再通过kibana查看

Springboot 使用logback直接将日志写入Elasticsearch,再通过kibana查看

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

1. 导入依赖

            
            <dependency>
                <groupId>com.internetitemgroupId>
                <artifactId>logback-elasticsearch-appenderartifactId>
                <version>1.6version>
            dependency>
            <dependency>
                <groupId>net.logstash.logbackgroupId>
                <artifactId>logstash-logback-encoderartifactId>
                <version>4.11version>
            dependency>

2. 配置logback-spring.xml

如果没有则添加logback-spring.xml到resource目录的根目录下


<configuration debug="false">
    
    <property name="LOG_HOME" value="./logs"/>
	 
    <springProperty scope="context" name="active" source="spring.profiles.active" defaultValue="dev"/>

    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{50}:%L) - %msg%npattern>
        encoder>
    appender>

    
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            <FileNamePattern>${LOG_HOME}/jeecgboot-%d{yyyy-MM-dd}.%i.logFileNamePattern>
            
            <MaxHistory>30MaxHistory>
            <maxFileSize>10MBmaxFileSize>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%npattern>
        encoder>
    appender>

    <appender name="ELASTIC" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
        <url>http://es账号:es密码@esip:9200/_bulkurl>
        <index>xxx-log-${active}index>   
        <type>${active}type>
        <connectTimeout>30000connectTimeout> 
        <errorsToStderr>falseerrorsToStderr> 
        <includeCallerData>falseincludeCallerData> 
        <logsToStderr>falselogsToStderr> 
        <maxQueueSize>104857600maxQueueSize> 
        <maxRetries>3maxRetries> 
        <readTimeout>30000readTimeout> 
        <sleepTime>250sleepTime> 
        <rawJsonMessage>falserawJsonMessage> 
        <includeMdc>falseincludeMdc> 
        <maxMessageSize>-1maxMessageSize> 
        <authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication"/> 
        <properties>
            <property>
                <name>hostname>
                <value>${HOSTNAME}value>
                <allowEmpty>falseallowEmpty>
            property>
            <property>
                <name>levelname>
                <value>%levelvalue>
            property>
            <property>
                <name>threadname>
                <value>%threadvalue>
            property>
            <property>
                <name>stacktracename>
                <value>%exvalue>
            property>
            <property>
                <name>loggername>
                <value>%loggervalue>
            property>
            <property>
                <name>envname>
                <value>${active}value>
            property>
        properties>
        <headers>
            <header>
                <name>Content-Typename>
                <value>application/jsonvalue>
            header>
        headers>
    appender>

    
    <logger name="com.apache.ibatis" level="DEBUG"/>
    <logger name="java.sql.Connection" level="DEBUG"/>
    <logger name="java.sql.Statement" level="DEBUG"/>
    <logger name="java.sql.PreparedStatement" level="DEBUG"/>

    <springProfile name="dev">
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
        root>
    springProfile>
    <springProfile name="test">
        <root level="INFO">
            <appender-ref ref="FILE"/>
            <appender-ref ref="ELASTIC"/>
        root>
    springProfile>
    <springProfile name="uat,prod">
        <root level="INFO">
            <appender-ref ref="ELASTIC"/>
        root>
    springProfile>
configuration>

3. 定义日志等级

logging:
  level:
    root: info

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

@RestController
@RequestMapping("log")
public class EsLoggerController {
    private Logger logger = LoggerFactory.getLogger("es-logger");

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

    }
}

4. 启动程序测试

可以自动创建索引,不用手动创建

Springboot 使用logback直接将日志写入Elasticsearch,再通过kibana查看_第1张图片

5. kibana创建索引模式

Springboot 使用logback直接将日志写入Elasticsearch,再通过kibana查看_第2张图片

以上步骤完成后即可在kibana的discover菜单查找到我们的日志索引,在这里进行可视化的查找日志

Springboot 使用logback直接将日志写入Elasticsearch,再通过kibana查看_第3张图片

你可能感兴趣的:(elasticsearch,spring,boot,kibana,日志,logback)