經過maven建立一個基於spring mvc的web工程,啓動以後成功顯示hello world(過程略)。nginx
<!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!--Spring Web + Spring MVC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency>
本第二天志組件使用的是logback;git
<!--logback--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.12</version> </dependency> <!--logstash begin --> <dependency> <groupId>com.cwbase</groupId> <artifactId>logback-redis-appender</artifactId> <version>1.1.5</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="LOGSTASH" class="com.cwbase.logback.RedisAppender"> <source>SpringELK</source> <type>dev-type</type> <tags>dev</tags> <host>127.0.0.1</host> <port>6379</port> <key>logstash</key> </appender> <root level="DEBUG"> <appender-ref ref="LOGSTASH" /> </root> </configuration>
因爲項目比較小,採用了日誌寫入redis,大型項目可參考將redis替換爲kafka。 商業項目可用logstash監聽nginx access log,定時將日誌上傳kafka集羣。github
#輸出日誌到logstash log4j.appender.logstash=org.apache.log4j.net.SocketAppender log4j.appender.logstash.RemoteHost=127.0.0.1 log4j.appender.logstash.port=4560 log4j.appender.logstash.ReconnectionDelay=60000 log4j.appender.logstash.LocationInfo=true
input { log4j { host => "127.0.0.1" port => 4560 } }
private Logger log = LoggerFactory.getLogger(HomeController.class); @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { log.error("Main錯誤"); return "/home/hello"; }
(過程略)web
因爲新版本的elasticsearch支持是jdk 1.8,個人jdk是1.7因此要謹慎選擇elk版本。redis
logstash會經過redis讀取日誌,導入es中進行存儲和索引操做。spring
能夠完整的看到spring mvc對應一個http請求的生命週期;apache
至此一個簡單的基於spring+spring mvc+redis+elk的日誌系統搭建成功了!windows
code 完整地址:https://github.com/zhangcj/springELK後端