myoperator/centrallog

MyOperator 详细日志包

v1.0 2019-06-25 18:46 UTC

This package is not auto-updated.

Last update: 2024-09-30 18:09:06 UTC


README

此库旨在作为基本 log4php 包装器,以我们希望的日志模式进行日志记录。

功能

  • 命名空间和定义
  • PSR-4 自动加载兼容结构
  • 默认详细日志兼容配置器
  • 一个集中日志位置

安装

您可以通过在您的 composer.json 中添加以下部分轻松安装此包:

"repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/myoperator/centrallog.git"
        }
    ]

然后执行: composer require myoperator/centrallog:dev-master

或者在您的 composer.json 中添加以下内容:

  "require": {
        "myoperator/centrallog": "dev-master"
  }

您的 composer.json 将看起来像这样:

{
    "require": {
        "myoperator/centrallog": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/myoperator/centrallog.git"
        }
    ]
}

用法

  1. vendor/autoload 包含在您的项目中
   include_once 'vendor/autoload.php';
  1. 配置记录器
  use \MyOperator\CentralLog;

  CentralLog::configure('mylog.log'); //logs.log refers to log output file
  1. 获取记录器并记录任何内容
  $log = CentralLog::getLogger('myLogger');
  $log->log("Something");

总的来说,这可以总结为

include_once 'vendor/autoload.php';
use \MyOperator\CentralLog;

CentralLog::configure("mylog.log");

$log = CentralLog::getLogger('myLogger');
$log->log("Something");

//Or you can also provide the title
$log->withTitle("Making curl request")->log("curl response");

配置

记录器调整为根据 myoperator 特定日志进行配置。因此,以下参数可以传递给 configure 方法。

  CentralLog::configure(string  $outputpath = null, string  $server = null, string|\MyOperator\class  $class = null, string  $pattern = null, string  $maxsize = null)

参数

string	$outputpath	Output path of the log file

string	$server	Server on which the application is running. Ex- S6, API01

string	$class	Class name under which the logger is being used

string	$pattern	logger pattern as described in https://logging.apache.ac.cn/log4php/docs/layouts/pattern.html

string	$maxsize	Maximum size per log

可用方法

记录通用日志

任何日志都可以使用以下方法签名进行记录

   CentralLog::log(mixed  $message, string  $uid = null,  integer  $acl = null, string $loglevel="info")

参数

mixed	$message	Item to be logged

string	$uid	The unique id of item. In case of sync script, this can be engine uid. (optional)

integer	$acl	The ACL to be used to log the item. (optional). Can be one of [1,2,4]

string	$loglevel The loglevel to use for the log. defaults to `info`. (optional)

注意,支持/开发者/客户端日志方法不需要 $acl 参数,因为它很明显将使用哪个 $acl

带有标题的记录

标题有助于上下文化日志域。它提供了一种在日志中识别活动的方法。为了使日志更易于阅读,您可以使用 ->withTitle($title)->title($title) 提供日志标题。例如 -

  $logger->withTitle($title)->log($message, $uid, $acl);
  //This is same as above
  $logger->title($title)->log($message, $uid, $acl);

记录支持日志

   $logger->slog(mixed  $message, string  $uid = null, string  $level = null)

记录客户端日志

   $logger->clog(mixed  $message, string  $uid = null, string  $level = null)

记录开发者日志

   $logger->dlog(mixed  $message, string  $uid = null, string  $level = null);

记录组合日志

有时,您可能希望为同一事件记录不同类型的响应。您可以通过设置以下消息键中的不同消息轻松完成此操作

   $message = array(
                  'dmsg' => 'Your developer log here',
                  'smsg' => 'Your support log here',
                  'cmsg' => 'Your customer log here'
              );

例如,在异常的情况下,您可能希望将堆栈跟踪发送给开发者,异常消息发送给支持,并将字符串 “发生错误” 发送给最终客户。这可以通过将 log 实现如下轻松完成

    try{
      throw new \Exception("Application error....", 400);
    } catch (\Exception $e) {
      $log->log(['dmsg' => $e, 'cmsg' => 'Some error occured', 'smsg' => $e->getMessage()]);
    }

查看文档

此包使用 phpdoc 生成文档。您可以通过克隆存储库并安装开发依赖项来生成包文档

    composer update --dev

然后使用 phpdoc 通过

   phpdoc -d src/

待办事项

  • 添加 phpunit 测试用例