spring cloud sleuth json文件


spring cloud sleuth  json文件

应用:将日志以json的格式输出,后续可通过filebeat、logstash输出到elasticsearch,可方便地对日志进行分析

****************************

示例

***********************

导入 jar 包

        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>6.3</version>
        </dependency>

***********************

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>

    <springProperty scope="context" name="springApplicationName" source="spring.application.name"/>

    <property name="log.name" value="e:/hhhh/${springApplicationName}"/>
    <property name="console.pattern" value="%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
    <property name="logstash.pattern" value="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${console.pattern}</pattern>
        </encoder>
    </appender>

    <appender name="logstash" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.name}.%d{yyyy-MM-dd}.json</fileNamePattern>
        </rollingPolicy>
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>UTC</timestamp>
                <pattern>
                    <pattern>
                        {
                        "severity": "%level",
                        "service": "${springApplicationName:-}",
                        "traceId": "%X{X-B3-TraceId:-}",
                        "spanId": "%X{X-B3-SpanId:-}",
                        "exportable": "%X{X-Span-Export:-}",
                        "pid": "${PID:-}",
                        "thread": "%thread",
                        "class": "%logger{40}",
                        "msg": "%message"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="console"/>
        <appender-ref ref="logstash"/>
    </root>
</configuration>

注意:文件名称需设置为logback-spring.xml,如果设置为logback.xml,读取属性文件的值会出错

***********************

controller 层

HelloController

@RestController
public class HelloController {

    private Logger logger= LoggerFactory.getLogger(HelloController.class);

    @RequestMapping("/hello")
    public String hello(){
        for(int i=0;i<10;i++){
            logger.info("hello controller: {}","hello world"+i);
        }

        return "success";
    }
}

**********************

使用测试

localhost:8080/hello:对应目录下声称日志文件 hello-service.2020-03-11.json

{"@timestamp":"2020-03-11T16:28:50.168+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"com.example.demo.DemoApplication","msg":"No active profile set, falling back to default profiles: default"}
{"@timestamp":"2020-03-11T16:28:50.802+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.s.cloud.context.scope.GenericScope","msg":"BeanFactory id=0d08539d-6d63-3aa9-9fbb-10600d7520d3"}
{"@timestamp":"2020-03-11T16:28:51.214+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.s.b.w.embedded.tomcat.TomcatWebServer","msg":"Tomcat initialized with port(s): 8080 (http)"}
{"@timestamp":"2020-03-11T16:28:51.221+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.apache.coyote.http11.Http11NioProtocol","msg":"Initializing ProtocolHandler [\"http-nio-8080\"]"}
{"@timestamp":"2020-03-11T16:28:51.221+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"org.apache.catalina.core.StandardService","msg":"Starting service [Tomcat]"}
{"@timestamp":"2020-03-11T16:28:51.222+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"org.apache.catalina.core.StandardEngine","msg":"Starting Servlet engine: [Apache Tomcat/9.0.31]"}
{"@timestamp":"2020-03-11T16:28:51.344+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.a.c.c.C.[Tomcat].[localhost].[/]","msg":"Initializing Spring embedded WebApplicationContext"}
{"@timestamp":"2020-03-11T16:28:51.345+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.s.web.context.ContextLoader","msg":"Root WebApplicationContext: initialization completed in 1162 ms"}
{"@timestamp":"2020-03-11T16:28:52.479+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.s.s.concurrent.ThreadPoolTaskExecutor","msg":"Initializing ExecutorService 'applicationTaskExecutor'"}
{"@timestamp":"2020-03-11T16:28:54.503+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.s.cloud.commons.util.InetUtils","msg":"Cannot determine local hostname"}
{"@timestamp":"2020-03-11T16:28:54.574+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.s.b.a.e.web.EndpointLinksResolver","msg":"Exposing 2 endpoint(s) beneath base path '/actuator'"}
{"@timestamp":"2020-03-11T16:28:54.639+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.apache.coyote.http11.Http11NioProtocol","msg":"Starting ProtocolHandler [\"http-nio-8080\"]"}
{"@timestamp":"2020-03-11T16:28:55.224+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.s.b.w.embedded.tomcat.TomcatWebServer","msg":"Tomcat started on port(s): 8080 (http) with context path ''"}
{"@timestamp":"2020-03-11T16:28:57.010+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"o.s.cloud.commons.util.InetUtils","msg":"Cannot determine local hostname"}
{"@timestamp":"2020-03-11T16:28:57.012+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"main","class":"com.example.demo.DemoApplication","msg":"Started DemoApplication in 11.087 seconds (JVM running for 12.651)"}
{"@timestamp":"2020-03-11T16:28:58.613+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"http-nio-8080-exec-1","class":"o.a.c.c.C.[Tomcat].[localhost].[/]","msg":"Initializing Spring DispatcherServlet 'dispatcherServlet'"}
{"@timestamp":"2020-03-11T16:28:58.614+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"http-nio-8080-exec-1","class":"o.s.web.servlet.DispatcherServlet","msg":"Initializing Servlet 'dispatcherServlet'"}
{"@timestamp":"2020-03-11T16:28:58.626+08:00","severity":"INFO","service":"hello-service","traceId":"","spanId":"","exportable":"","pid":"16360","thread":"http-nio-8080-exec-1","class":"o.s.web.servlet.DispatcherServlet","msg":"Completed initialization in 12 ms"}
{"@timestamp":"2020-03-11T16:28:59.427+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 0"}
{"@timestamp":"2020-03-11T16:28:59.429+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 1"}
{"@timestamp":"2020-03-11T16:28:59.429+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 2"}
{"@timestamp":"2020-03-11T16:28:59.429+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 3"}
{"@timestamp":"2020-03-11T16:28:59.429+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 4"}
{"@timestamp":"2020-03-11T16:28:59.435+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 5"}
{"@timestamp":"2020-03-11T16:28:59.435+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 6"}
{"@timestamp":"2020-03-11T16:28:59.436+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 7"}
{"@timestamp":"2020-03-11T16:28:59.436+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 8"}
{"@timestamp":"2020-03-11T16:28:59.437+08:00","severity":"INFO","service":"hello-service","traceId":"3b114098353b0c60","spanId":"3b114098353b0c60","exportable":"false","pid":"16360","thread":"http-nio-8080-exec-3","class":"c.e.demo.controller.HelloController","msg":"hello controller: hello world 9"}
发布了387 篇原创文章 · 获赞 98 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_43931625/article/details/104776102