知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
https://github.com/kmtong/logback-redis-appender
例 3.1. spring boot logback
root@netkiller /etc/logstash/conf.d % cat spring-boot-redis.conf input { redis { codec => json host => "localhost" port => 6379 key => "logstash:redis" data_type => "list" } } output { elasticsearch { hosts => ["127.0.0.1:9200"] index => "logstash-api" } }
src/main/resources/logback.xml
neo@MacBook-Pro ~/deployment % cat api.netkiller.cn/src/main/resources/logback.xml <?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> <property name="type.name" value="test" /> <appender name="LOGSTASH" class="com.cwbase.logback.RedisAppender"> <source>mySource</source> <sourcePath>mySourcePath</sourcePath> <type>myApplication</type> <tags>production</tags> <host>localhost</host> <port>6379</port> <database>0</database> <key>logstash:api</key> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} : %msg %n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> <appender-ref ref="LOGSTASH" /> </root> </configuration>
例 3.2. Elasticsearch 索引切割示例
root@netkiller /opt/api.netkiller.cn % cat /etc/logstash/conf.d/spring-boot-redis.conf input { redis { codec => json host => "localhost" port => 6379 db => 10 key => "logstash:redis" data_type => "list" } } output { stdout { codec => rubydebug } elasticsearch { hosts => ["127.0.0.1:9200"] index => "logstash-%{type}-%{+YYYY.MM.dd}" } }
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> <property name="logstash.type" value="api" /> <property name="logstash.tags" value="springboot" /> <appender name="LOGSTASH" class="com.cwbase.logback.RedisAppender"> <source>application.properties</source> <type>${logstash.type}</type> <tags>${logstash.tags}</tags> <host>localhost</host> <database>10</database> <key>logstash:redis</key> <mdc>true</mdc> <location>true</location> <callerStackIndex>0</callerStackIndex> </appender> <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="LOGSTASH" /> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} : %msg %n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> <appender-ref ref="LOGSTASH" /> </root> </configuration>