laravel接合monolog实现日志记录到Elasticsearch实践
生活随笔
收集整理的這篇文章主要介紹了
laravel接合monolog实现日志记录到Elasticsearch实践
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
需求
記錄所有前臺(tái)用戶(hù)請(qǐng)求返回?cái)?shù)據(jù)到ES
實(shí)踐
引用拓展包
$ composer require ruflin/elastica: ^5.0ruflin/elastica 拓展包說(shuō)明
| 5.x | 5.x | ^5.0 | >=5.6 |
| 3.2.3 (unmaintained) | 2.4.0 | no | >=5.4 |
| 2.x (unmaintained) | 1.7.2 | no | >=5.3.3 |
由于項(xiàng)目中已經(jīng)繼承了BaseController ,其中有所有接口通過(guò)showReturnCodeAndSaveLog返回(也可以通過(guò)中間件的方式去實(shí)現(xiàn))
/*** 返回函數(shù)* * @param $code* @param string $msg* @param array $data** @return array*/protected function showReturnCodeAndSaveLog($code, $msg = '', $data = []){//添加日志內(nèi)容$this->addLog($msg, $data);//返回信息return self::showReturnCode($code, $msg, $data);}添加析構(gòu)函數(shù)
/*** 保存日志動(dòng)作*/protected function saveLogAction(){if (!empty($this->log)) {//獲取文件前綴&&es的type$file_title = $this->getLogTitle();//添加es日志記錄隊(duì)列$job = (new JobEsMonolog($this->request->method(), $this->log, $file_title))->onQueue(config('queue_list.lcsc'));dispatch($job);//添加文本日志if ($file_title) {$log = newLogRecord($file_title . '-' . date('Ymd'), env('APP_NAME'));foreach ($this->log as $info) {$log->info($this->request->method(), $info);}} else {info($this->request->method(), $this->log);}}}public function __destruct(){if (!empty($this->log) && $this->saveLog == true) {//保存日志$this->saveLogAction();}}添加job
$ php artisan make:job JobEsMonologJobEsMonolog.php
<?phpnamespace App\Jobs;use Elastica\Client; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Monolog\Formatter\ElasticaFormatter; use Monolog\Handler\ElasticSearchHandler; use Monolog\Logger;class JobEsMonolog implements ShouldQueue {use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;//日志messageprivate $msg;//日志內(nèi)容private $data;//es typeprivate $type;//錯(cuò)誤級(jí)別private $level;/*** Create a new job instance.** @return void*/public function __construct($msg,$data,$type = 'orders',$level = Logger::INFO){$this->msg = $msg;$this->data = $data;$this->type = $type;$this->level = $level;}/*** Execute the job.** @return void*/public function handle(){//es host配置$es_host = env('ES_HOSTS','192.168.12.35:9202');$es_host_array = explode(':',$es_host);$config = ['host'=>$es_host_array[0],'port'=>$es_host_array[1],];$client = new Client($config);$options = ['index' => 'monolog_' . date('Y_m_d'),'type' => $this->type,];$handler = new ElasticSearchHandler($client, $options);//es formatter$formatter = new ElasticaFormatter($options['index'],$options['type']);$handler->setFormatter($formatter);$log = new Logger('monolog');$log->pushHandler($handler);//目前暫時(shí)引用兩種switch ($this->level) {case Logger::INFO :$log->info($this->msg,$this->data);break;case Logger::DEBUG :$log->debug($this->msg,$this->data);break;default :$log->warning('未添加錯(cuò)誤類(lèi)型',$this->data);break;}} }kibana 中顯示
其實(shí)對(duì)于大家來(lái)說(shuō)就只有JobEsMonolog代碼比較有參考價(jià)值;
總結(jié)
以上是生活随笔為你收集整理的laravel接合monolog实现日志记录到Elasticsearch实践的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PIC汇编指令
- 下一篇: springboot毕业设计 基于spr