Skip to content

Instantly share code, notes, and snippets.

@lonelyleaf
Created August 23, 2019 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lonelyleaf/0473fe9e68442eaf8fc5e2af9101f4cc to your computer and use it in GitHub Desktop.
Save lonelyleaf/0473fe9e68442eaf8fc5e2af9101f4cc to your computer and use it in GitHub Desktop.
spring结合logback,将日志导入es的配置示例
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<contextName>logback</contextName>
<!--从spring environment中注入的变量-->
<springProperty scope="context" name="app-name" source="spring.application.name"
defaultValue="unknown"/>
<springProperty scope="context" name="spring.profiles" source="spring.profiles"
defaultValue="unknown"/>
<!--只有当生产环境时,下面的配置不会生效-->
<springProfile name="prod,test">
<appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!--<destination>elk-logstash:4560</destination>-->
<destination>10.1.31.121:31560</destination>
<destination>10.1.31.122:31560</destination>
<destination>10.1.31.123:31560</destination>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"app-name":"${app-name}","spring.profiles":"${spring.profiles}"}</customFields>
<!--减少抛出异常中,不必要的信息-->
<throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
<maxDepthPerThrowable>30</maxDepthPerThrowable>
<maxLength>8192</maxLength>
<shortenedClassNameLength>20</shortenedClassNameLength>
<exclude>sun\.reflect\..*\.invoke.*</exclude>
<exclude>net\.sf\.cglib\.proxy\.MethodProxy\.invoke</exclude>
<!-- generated class names -->
<exclude>\$\$FastClassByCGLIB\$\$</exclude>
<exclude>\$\$EnhancerBySpringCGLIB\$\$</exclude>
<exclude>^sun\.reflect\..*\.invoke</exclude>
<!-- JDK internals -->
<exclude>^com\.sun\.</exclude>
<exclude>^sun\.net\.</exclude>
<!-- dynamic invocation -->
<exclude>^net\.sf\.cglib\.proxy\.MethodProxy\.invoke</exclude>
<exclude>^org\.springframework\.cglib\.</exclude>
<exclude>^org\.springframework\.transaction\.</exclude>
<exclude>^org\.springframework\.validation\.</exclude>
<exclude>^org\.springframework\.app\.</exclude>
<exclude>^org\.springframework\.aop\.</exclude>
<exclude>^java\.lang\.reflect\.Method\.invoke</exclude>
<!-- Spring plumbing -->
<exclude>^org\.springframework\.ws\..*\.invoke</exclude>
<exclude>^org\.springframework\.ws\.transport\.</exclude>
<exclude>^org\.springframework\.ws\.soap\.saaj\.SaajSoapMessage\.</exclude>
<exclude>^org\.springframework\.ws\.client\.core\.WebServiceTemplate\.</exclude>
<exclude>^org\.springframework\.web\.filter\.</exclude>
<!-- Tomcat internals -->
<exclude>^org\.apache\.tomcat\.</exclude>
<exclude>^org\.apache\.catalina\.</exclude>
<exclude>^org\.apache\.coyote\.</exclude>
<exclude>^java\.util\.concurrent\.ThreadPoolExecutor\.runWorker</exclude>
<exclude>^java\.lang\.Thread\.run$</exclude>
<!--<evaluator class="myorg.MyCustomEvaluator"/>-->
<rootCauseFirst>true</rootCauseFirst>
<inlineHash>true</inlineHash>
</throwableConverter>
</encoder>
</appender>
</springProfile>
<!--输出到控制台-->
<!--<appender name="console" class="ch.qos.logback.core.ConsoleAppender">-->
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>-->
<!--<encoder>-->
<!--<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>-->
<!--</encoder>-->
<!--</appender>-->
<root level="info">
<!--只有生产环境才使用stash配置-->
<springProfile name="prod,test">
<appender-ref ref="stash"/>
</springProfile>
<!--<appender-ref ref="console"/>-->
<!--<appender-ref ref="file"/>-->
</root>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment