java project datadog logs

1、Add dependence on pom.xml

<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.2.9</version>
</dependency>
<dependency>
  <groupId>net.logstash.logback</groupId>
  <artifactId>logstash-logback-encoder</artifactId>
  <version>6.6</version>
</dependency>

2、Add or modify the logback.xml of your service’s code.

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="1 seconds">
    <contextName>logback</contextName>
    <!--ConsoleAppender Used to output logs on the screen-->
    <appender name="console_out" class="ch.qos.logback.core.ConsoleAppender">
        <!--A filter is defined, the log output under LEVEL will not be printed-->
        <!--INFO is defined here, that is, the console will not output
            a log smaller than the INFO level-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <!--Define the console output format-->
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <!--The following is a new configuration
                to solve Chinese garbled characters, \u2028 is the line separator-->
            <jsonFactoryDecorator class="net.logstash.logback.decorate.CharacterEscapesJsonFactoryDecorator">
                <escape>
                    <targetCharacterCode>10</targetCharacterCode>
                    <escapeSequence>\u2028</escapeSequence>
                </escape>
            </jsonFactoryDecorator>
            <providers>
                <pattern>
                    <pattern>
                        {"dd": {"trace_id": "%X{dd.trace_id}",
                        "span_id": "%X{dd.span_id}"
                        },
                        "level":"%-5level",
                        "message":"[%date{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %-5level env:${dd.env} service:${dd.service} class:%logger{80}:%line - %message %exception{full}"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>

    <!-- ASYNC OUTPUT -->
    <appender name="console_async" class="ch.qos.logback.classic.AsyncAppender">
        <!-- Do not lose logs. By default, if 80% of the
         queue is full, logs of TRACT, DEBUG, and INFO levels will be discarded -->
        <discardingThreshold>0</discardingThreshold>
        <!-- Change the default queue depth, this value will affect performance. The default value is 256 -->
        <queueSize>2048</queueSize>
        <!-- Print stack information -->
        <includeCallerData>true</includeCallerData>
        <appender-ref ref="console_out"/>
    </appender>
    <root level="INFO">
        <appender-ref ref="console_async"/>
        <!--<appender-ref ref="file_async"/>-->
    </root>
</configuration>

3、re-deploy the api’s service

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值