参考:
简单记录一下实现过程:
build.gradle:
// 增加 logback支持,以便于发送log到ES
implementation 'org.slf4j:slf4j-api:1.7.25'
implementation 'com.github.tony19:logback-android:2.0.0'
implementation 'com.internetitem:logback-elasticsearch-appender:1.6'
添加logback.xml 配置文件,到app/src/main/assets下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="false">
<appender name="ELASTIC" class="com.internetitem.logback.elasticsearch.ElasticsearchAppender">
<!-- 下面是我本地的ES server,替换成自己的。如果有用户名密码,用这个形式: http://username:password@yourserver/_bulk -->
<url>http://192.168.8.167:9000/_bulk</url>
<!-- ES 日志名,起个有特点的名字便于在Kibana中过滤 -->
<index>pay-%date{yyyy-MM-dd}</index>
<!-- type对应ES中的 _type, 可选,最好有 -->
<type>android</type>
<!-- 下面的配置基本不用动 -->
<loggerName>es-logger</loggerName> <!-- optional -->
<errorLoggerName>es-error-logger</errorLoggerName> <!-- optional -->
<connectTimeout>30000</connectTimeout> <!-- optional (in ms, default 30000) -->
<errorsToStderr>false</errorsToStderr> <!-- optional (default false) -->
<includeCallerData>false</includeCallerData> <!-- optional (default false) -->
<logsToStderr>false</logsToStderr> <!-- optional (default false) -->
<maxQueueSize>104857600</maxQueueSize> <!-- optional (default 104857600) -->
<maxRetries>3</maxRetries> <!-- optional (default 3) -->
<readTimeout>30000</readTimeout> <!-- optional (in ms, default 30000) -->
<sleepTime>250</sleepTime> <!-- optional (in ms, default 250) -->
<rawJsonMessage>false</rawJsonMessage> <!-- optional (default false) -->
<includeMdc>false</includeMdc> <!-- optional (default false) -->
<maxMessageSize>100</maxMessageSize> <!-- optional (default -1 -->
<authentication class="com.internetitem.logback.elasticsearch.config.BasicAuthentication" /> <!-- optional -->
<properties>
<property>
<name>host</name>
<value>${HOSTNAME}</value>
<allowEmpty>false</allowEmpty>
</property>
<property>
<name>severity</name>
<value>%level</value>
</property>
<property>
<name>thread</name>
<value>%thread</value>
</property>
<property>
<name>stacktrace</name>
<value>%ex</value>
</property>
<property>
<name>logger</name>
<value>%logger</value>
</property>
</properties>
<headers>
<header>
<name>Content-Type</name>
<value>application/json</value>
</header>
</headers>
</appender>
<root level="info">
<!-- <appender-ref ref="FILELOGGER" />-->
<appender-ref ref="ELASTIC" />
</root>
<logger name="es-error-logger" level="ERROR" additivity="false">
<!-- <appender-ref ref="FILELOGGER" />-->
<appender-ref ref="ELASTIC" />
</logger>
<logger name="es-logger" level="INFO" additivity="false">
<appender name="ES_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- ... -->
<encoder>
<pattern>%msg</pattern>
<!-- This pattern is important, otherwise it won't be the raw Elasticsearch format anyomre -->
</encoder>
</appender>
</logger>
</configuration>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/// ..
private static String TAG = "MyTAG";
// ...
Logger LOG = LoggerFactory.getLogger(TAG );
LOG.info("Some message");