There are two ways to output logs. One is to directly call the logger of LogServant to output the remote log, and the other is to combine the monolog to output the remote log.
$logger =new\Monolog\Logger("tars_logger");//remote log$tarsHandler =new\Tars\log\handler\TarsHandler($config);//local log$streamHandler =new\Monolog\Handler\StreamHandler(ENVConf::$logPath."/".__CLASS__.".log");$logger->pushHandler($tarsHandler);$logger->pushHandler($streamHandler);$array = ["key1"=>"value1","key2"=>"value2","key3"=>"value3"];$logger->debug("add a debug message", $array);$logger->info("add a info message", $array);$logger->notice("add a notice message", $array);$logger->warning("add a warning message", $array);$logger->error("add a error message", $array);$logger->critical("add a critical message", $array);$logger->emergency("add a emergency message", $array);
With ELK
Remote logs are distinguished by application and service. They are located in /usr/local/app/tars/remote_app_log/{App}/{ServerName}. In actual business, you can synchronize remote logs to filebeat orlogstash. ElasticSearch`.
In addition, monolog itself provides ElasticSearchHandler, which can easily output logs directly to ElasticSearch.
The ruflin/elastica package needs to be introduced into composer before use.
Note: Until the update of this article, the release of ruflin/elastica only supports elasticsearch: 6. version. If you want to use elasticsearch: 7. , you can use the elasticsearch/elasticsearch package to encapsulate the handler yourself
The sample code:
$logger =new\Monolog\Logger("elk_logger");$client =new\Elastica\Client(['host'=>'127.0.0.1','port'=>9200]);$elkHanlder =newMonolog\Handler\ElasticSearchHandler($client, ['index'=>'monolog_index_test','type'=>'record']);$logger->pushHandler($elkHanlder);$logger->error("this is a test msg from monolog");