tars-log

tars-log

tars-logphptars 远程日志模块

安装

使用composer进行安装 composer install phptars/tars-log

使用

配置

实例化CommunicatorConfig,可以逐个参数进行配置,也可以通过平台下发的配置文件统一配置

  • 单独配置某个参数
  1. $config = new \Tars\client\CommunicatorConfig();
  2. $config->setLocator("tars.tarsregistry.QueryObj@tcp -h 172.16.0.161 -p 17890");
  3. $config->setModuleName("tedtest");
  4. $config->setCharsetName("UTF-8");
  5. $config->setLogLevel("INFO"); //日志级别:`INFO`、`DEBUG`、`WARN`、`ERROR` 默认INFO
  6. $config->setSocketMode(2); //远程日志连接方式:1:socket,2:swoole tcp client 3: swoole coroutine tcp client
  • 配置文件初始化参数
  1. $config = new \Tars\client\CommunicatorConfig();
  2. $sFilePath = '项目地址/src/conf'; //配置文件下发路径
  3. $config->init($sFilePath);

输出日志

输出日志提供两种方式,一种直接调用LogServantlogger方式输出远程日志,另一种结合monolog输出远程日志(推荐)

  • 调用LogServantlogger方式
  1. $logServant = new \Tars\log\LogServant($config);
  2. $appName = "App"; //应用名称
  3. $serverName = "server"; //服务名称
  4. $file = "test.log"; //文件名称
  5. $format = "%Y%m%d"; //日志时间格式
  6. $buffer = ["hahahahaha"]; //日志内容,数组,每个元素为一条日志
  7. $result = $logServant->logger($appName,$serverName,$file,$format,$buffer);
  • 结合monolog方式(推荐)
  1. $logger = new \Monolog\Logger("tars_logger");
  2. //remote log
  3. $tarsHandler = new \Tars\log\handler\TarsHandler($config);
  4. //local log 这里可以根据业务需要添加其他handler,比如StreamHandler、ElasticSearchHandler 等
  5. $streamHandler = new \Monolog\Handler\StreamHandler(ENVConf::$logPath . "/" . __CLASS__ . ".log");
  6. $logger->pushHandler($tarsHandler);
  7. $logger->pushHandler($streamHandler);
  8. $array = [
  9. "key1" => "value1",
  10. "key2" => "value2",
  11. "key3" => "value3"
  12. ];
  13. $logger->debug("add a debug message", $array);
  14. $logger->info("add a info message", $array);
  15. $logger->notice("add a notice message", $array);
  16. $logger->warning("add a warning message", $array);
  17. $logger->error("add a error message", $array);
  18. $logger->critical("add a critical message", $array);
  19. $logger->emergency("add a emergency message", $array);