phptars / tars-log
此包最新版本(0.1.6)没有可用的许可证信息。
tars的远程日志
0.1.6
2021-03-17 13:31 UTC
Requires
- php: >=5.5
- phptars/tars-client: ~0.1
This package is auto-updated.
Last update: 2024-09-17 21:28:52 UTC
README
Tarlog
是一个 phptars
远程日志模块
安装
使用 composer
安装
composer install phptars/tars-log
使用方法
配置
实例化通信者配置,可以通过逐个配置或通过平台分发的配置文件进行配置
-分别配置参数
$config = new \Tars\client\CommunicatorConfig(); $config->setLocator("tars.tarsregistry.QueryObj@tcp -h 172.16.0.161 -p 17890"); $config->setModuleName("tedtest"); $config->setCharsetName("UTF-8"); $config->setLogLevel("INFO"); //log level:`INFO`、`DEBUG`、`WARN`、`ERROR` default INFO $config->setSocketMode(2); //Remote log connection mode:1:socket,2:swoole tcp client 3: swoole coroutine tcp client
- 配置初始化参数
$config = new \Tars\client\CommunicatorConfig(); $sFilePath = 'project address/src/conf'; //Profile distribution path $config->init($sFilePath);
输出日志
输出日志有两种方式:一种是通过直接调用 'logservice' 的 'logger' 模式输出远程日志,另一种是将远程日志与 'monitoring' 结合输出(推荐)
-调用 'logservant' 的 'logger' 方法
$logServant = new \Tars\log\LogServant($config); $appName = "App"; //app name $serverName = "server"; //service name $file = "test.log"; //file name $format = "%Y%m%d"; //log time $buffer = ["hahahahaha"]; //Log content, array, each element is a log $result = $logServant->logger($appName,$serverName,$file,$format,$buffer);
- 与 'monolog' 方法结合(推荐)
use Monolog\Handler\AbstractProcessingHandler; use Monolog\Logger; use Tars\client\CommunicatorConfig; use Tars\log\LogServant; class TarsHandler extends AbstractProcessingHandler { protected $app = 'Undefined'; protected $server = 'Undefined'; protected $dateFormat = '%Y%m%d'; private $logServant; private $logConf; public function __construct(CommunicatorConfig $config, $servantName = "tars.tarslog.LogObj", $level = Logger::WARNING, $bubble = true) { parent::__construct($level, $bubble); $this->logConf = $config; $this->logServant = new LogServant($config, $servantName); $moduleName = $this->logConf->getModuleName(); $moduleData = explode('.', $moduleName); $this->app = $moduleData ? $moduleData[0] : $this->app; $this->server = isset($moduleData[1]) ? $moduleData[1] : $this->server; } /** * @return string */ public function getApp() { return $this->app; } /** * @param string $app */ public function setApp($app) { $this->app = $app; } /** * @return string */ public function getServer() { return $this->server; } /** * @param string $server */ public function setServer($server) { $this->server = $server; } /** * @return string */ public function getDateFormat() { return $this->dateFormat; } /** * @param string $dateFormat */ public function setDateFormat($dateFormat) { $this->dateFormat = $dateFormat; } /** * Writes the record down to the log of the implementing handler * * @param array $record * @return void * @throws \Exception */ protected function write(array $record) { $this->logServant->logger($this->app, $this->server, $record['channel'], $this->dateFormat, [$record['formatted']]); } }
$logger = new \Monolog\Logger("tars_logger"); //remote log $tarsHandler = new TarsHandler($config); //local log Here you can add other handlers according to business needs,such as StreamHandler、ElasticSearchHandler $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);