As a PHP development transition to Java development, I will make a record of the problems ENCOUNTERED in my work, and share my experience of stepping on the pit with everyone

Hit the pit reason

When the company operation and maintenance unified request through more system environment, consul read different environment configuration Ali Cloud log service configuration, and happened, spring Boot project startup logback to Ali Cloud failed to push logs.

Found the problem

Mainly due to reading yamL configuration, not read

Why is it read?

Mainly due to:

  • logback.xml
  • The logback-spring. XML file is the default configuration file name for Spring’s logback, which will precede the loading of the application.ymal file

My project uses logback-spring as the file name

The solution

1, custom file name: logback-xxx. XML 2, import configuration file:

logging:
    config: classpath:logback-xxx.xml
Copy the code

Specific configuration

Yaml file configuration (ali Cloud as an example, other sources can be modified)
logging:
    config: classpath:logback-xxx.xml
    
sls:
  endpoint: endpoint
  accessKeyId: accessKeyId
  accessKeySecret: accessKeySecret
  project: project
  logStore: logStore
  canalLogStore: canalLogStore
Copy the code
Logback file
<? xml version="1.0" encoding="UTF-8"? > <Configuration debug="false"> <! -- Get the application name in YAML --> <springProperty scope="context" name="application.name" source="spring.application.name"/>
  <springProperty scope="context" name="sls.endpoint" source="sls.endpoint"/>
  <springProperty scope="context" name="sls.accessKeyId" source="sls.accessKeyId"/>
  <springProperty scope="context" name="sls.accessKeySecret" source="sls.accessKeySecret"/>
  <springProperty scope="context" name="sls.project" source="sls.project"/>
  <springProperty scope="context" name="sls.logStore" source="sls.logStore"/>
  <appender name="aliyunLoghubAppender" class="com.aliyun.openservices.log.logback.LoghubAppender"> <! -- Required --> <! -- Account and network configuration --> <endpoint>${sls.endpoint}</endpoint>
    <accessKeyId>${sls.accessKeyId}</accessKeyId>
    <accessKeySecret>${sls.accessKeySecret}</accessKeySecret>
    <project>${sls.project}</project>
    <logStore>${sls.logStore}</logStore>
    <ioThreadCount>8</ioThreadCount>
    <retries>3</retries>
    <topic>${application.name}_${SYSTEM_ENV}</topic>
    <source>${HOSTNAME}</source>
    <timeFormat>yyyy-MM-dd'T'HH:mm:ssZ</timeFormat> <timeZone>Asia/Shanghai</timeZone> <batchSizeThresholdInBytes>3145728</batchSizeThresholdInBytes>  <batchCountThreshold>4096</batchCountThreshold> <encoder> <pattern>[TrackId:%X{track-id}] %logger{0}</pattern> </encoder> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <! -- encoders are assigned thetypech.qos.logback.classic.encoder.PatternLayoutEncoder by default --> <encoder> <pattern>[TrackId:%X{track-id}] %d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: - %msg %X{THREAD_ID} %n </pattern> </encoder> </appender> <! -- to get the status in StatusManager --> <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/ > <! -- Environment Settings --> <springProfile name="dev">
    <root level="INFO">
      <appender-ref ref="STDOUT"/>
      <appender-ref ref="aliyunLoghubAppender"/>
    </root>
  </springProfile>
  <springProfile name="test,prepare,production">
    <root level="WARN">
      <appender-ref ref="aliyunLoghubAppender"/>
    </root>
  </springProfile>
</Configuration>
Copy the code