FastD 最佳實踐五: 構建ELK日誌分析

過去我們開發中,對日誌這個環節其實並不過重視,直到有一天,應用出現異常,這個時候纔想起來「日誌」,但很惋惜,爲時已晚。php

我們作運維和開發,除了救火,還須要防火,所以一些防範的意識也是很是重要的。java

效果圖

圖片描述

安裝ELK

安裝 ELK 是相對簡單的,可是後期也須要對其進行優化,適當考慮運維人力,若是以爲我的能夠折騰的不放能夠嘗試。web

若是已經存在 ELK 環境,能夠直接跳過,進行框架日誌配置環節。json

點擊前往: 中文地址ruby

先決條件: Java 8
sudo add-apt-repository -y ppa:webupd8team/java
sudo apt-get update
sudo apt-get -y install oracle-java8-installer
簡單安裝 elasticsearch

下載地址: elasticsearchoracle

下載 zip 或者其餘均可以。app

解壓壓縮包。執行:框架

運行前請配置好系統參數。有必定要求,若是啓動失敗,仔細留意錯誤信息,對應調整便可。運維

bin/elasticsearch

驗證:curl

curl http://localhost:9200/
安裝 kibana

下載地址: kibana

設置 elasticsearch url 地址,也就是剛纔的 localhost:9200 (默認)

運行: ./bin/kibana

打開: http://localhost:5601 訪問面板

安裝 logstash

下載地址: logstash

配置文件: config/logstash.conf

可參照:

input {
    file {
        type => "fastd"
        path => ["log file path"]
    }
}

filter {
    json {
        source => "message"
    }
}

output {
    # stdout { codec => rubydebug }
    elasticsearch {
        action => "index"
        hosts => "127.0.0.1:9200"
        index => "fastd"
    }
}

運行: bin/logstash -f config/logstash.conf

stdout 做爲標準輸出,能夠經過 stdout { codec => rubydebug } 對採集數據進行調試。

處理 php 應用日誌 (FastD 3.2新特性)

慶幸咱們有這麼一個需求,也能將工做經驗總結並造成最終的解決方案分享給你們。

!!3.1 版本處理方案

新建格式日誌文件。而後往下按照步驟進行處理便可(命名空間自定義,添加到日誌配置中的第四個參數中)。

<?php
namespace Logger\Formatter;

use Monolog\Formatter\LogstashFormatter;

/**
 * Class StashFormatter.
 */
class StashFormatter extends LogstashFormatter
{
    public function __construct()
    {
        parent::__construct(app()->getName(), get_local_ip(), null, null, self::V1);
    }
}

配置 app.php

<?php

return [
    // code ...
    'log' => [
        [
            \Monolog\Handler\StreamHandler::class,
            'info.log',
            \FastD\Logger\Logger::INFO,
            \FastD\Logger\Formatter\StashFormatter::class,
        ],
    ],
    // code ...
];

日誌會隨着配置進行生成,結果以下:

{"@timestamp":"2017-09-12T15:47:37.189080+08:00","@version":1,"host":"10.1.81.60","message":"sprintf(): Too few arguments","type":"fast-d","channel":"fast-d","level":"ERROR","msg":"sprintf(): Too few arguments","code":0,"file":"/Users/janhuang/Documents/htdocs/me/fastd/fastd/vendor/fastd/routing/src/RouteDispatcher.php","line":78,"trace":["#0 [internal function]: FastD\\Application->FastD\\{closure}(2, 'sprintf(): Too ...', '/Users/janhuang...', 78, Array)","#1 /Users/janhuang/Documents/htdocs/me/fastd/fastd/vendor/fastd/routing/src/RouteDispatcher.php(78): sprintf('Middleware %s i...')","#2 /Users/janhuang/Documents/htdocs/me/fastd/fastd/vendor/fastd/routing/src/RouteDispatcher.php(60): FastD\\Routing\\RouteDispatcher->callMiddleware(Object(FastD\\Routing\\Route), Object(FastD\\Http\\ServerRequest))","#3 /Users/janhuang/Documents/htdocs/me/fastd/fastd/src/Application.php(142): FastD\\Routing\\RouteDispatcher->dispatch(Object(FastD\\Http\\ServerRequest))","#4 /Users/janhuang/Documents/htdocs/me/fastd/fastd/src/Application.php(205): FastD\\Application->handleRequest(Object(FastD\\Http\\ServerRequest))","#5 /Users/janhuang/Documents/htdocs/me/fastd/fastd/tests/app/web/index.php(15): FastD\\Application->run()","#6 {main}"]}

忽略上述日誌內容,程序看得懂便可

配置 logstash 推送到 elasticsearch.

config/logstash.conf, 須要根據業務場景進行配置,如今顯示最簡單的配置。

input {
    file {
        type => "fastd"
        path => ["log file path"]
    }
}

filter {
    json {
        source => "message"
    }
}

output {
    elasticsearch {
        action => "index"
        hosts => "127.0.0.1:9200"
        index => "fastd"
    }
}

配置無誤後,開啓日誌採集

./bin/logstash -f path/to/logstash.conf

每當日誌刷新新增的時候,agent 就會將日誌採集推送到 elasticsearch. 若是以爲 ELK 會有卡頓或者性能問題,可能嘗試結合 kafka 進行優化。

打開: http://127.0.0.1:5601/,便可看到剛纔 php 應用產生的日誌。

總結

若是 php 應用發生問題,異常而又沒法第一時間發現的時候,日誌多是一個很好的切入點,當機器愈來愈多,應用愈來愈普遍的時候,日誌可能會大量消耗人力,這個時候咱們不得不去想辦法,一方面解決人力問題,一方面須要提升應用質量。

相關文章
相關標籤/搜索