mvf-tech/system_logger

用于执行DataDog和系统日志的包

3.1.1 2022-05-27 11:51 UTC

This package is auto-updated.

Last update: 2024-09-27 17:04:01 UTC


README

用于执行DataDog和系统日志的包

发布版本

安装和配置

  1. 运行composer require mvf-tech/system_logger来安装此包
  2. 您还需要设置DATADOG_PROJECT_NAMEDATADOG_SERVICE_NAME环境变量

基本使用

use MVF\SystemLogger\Reporters\Remotes\DataDog\DataDog;
use MVF\SystemLogger\Reporters\Host\CommandLine;
use MVF\SystemLogger\SystemLogger;

$logger = new SystemLogger();
$logger->info(
    ["some_kind_of_tag", "key" => "another_tag"],
    "message",
    new CommandLine(),
    DataDog::histogram("metric_name_suffix", 2)
);

目前有两种类型的报告器可用,即上面的例子中的host报告器和remote报告器。例如,DataDog.histogram(...)host报告器用于记录消息,这些报告器不接收标签列表,因此无法对其执行任何操作。remote报告器用于创建更复杂的日志,它们只接收标签列表,消息作为"message" => "the message"附加到标签列表中。您可以通过实现HostLogInterfaceRemoteLogInterface来创建自己的报告器,关于这方面的更多信息稍后介绍。

SystemLogger

包含

  • use MVF\SystemLogger\SystemLogger;

方法

  • ->info(array $tags, string $message, (HostLogInterface|RemoteLogInterface) ...$loggers)

  • ->warning(array $tags, string $message, (HostLogInterface|RemoteLogInterface) ...$loggers)

  • ->error(array $tags, string $message, (HostLogInterface|RemoteLogInterface) ...$loggers)

系统日志用作中央存储单元,它本身不执行任何日志记录,而是负责消息处理和减少代码重复,因为相同的标签和消息传递给所有提供的报告器。

默认标签

以下是在提供的列表中附加的默认标签。

  • infowarningerror之一将根据调用的系统日志函数附加。

消息占位符

有两种类型的消息占位符:tagreporter

$logger->info(
    [
        "name" => "Bob"                     // tag key name
    ], 
    "There are :1 messages from :name",
    new CommandLine(),                      // reporter index 0
    DataDog::histogram("messages", 2),      // reporter index 1
    ...                                     // reporter index ...
);

tag占位符通过一个冒号(:)后跟您的一个标签的键来识别,在上面的例子中:name是一个有效的tag。例如,reporter占位符通过冒号(:)后跟您的一个报告器的索引来识别,在上面的例子中:1将被替换为DataDog.histogram("messages", 2)的值,在这种情况下将是2

#####注意

  • :0不会被替换,因为CommandLine实现了HostLogInterface,而主机报告器没有任何返回值。

默认报告器

这些负责实际的信息记录。目前有两个内置的默认报告器:CommandLineDataDog

CommandLine

这是一个基本的报告器,它将简单地将消息输出到标准输出。

DataDog

包含

  • use MVF\SystemLogger\Reporters\Remotes\DataDog\DataDog;

方法

  • ::gauge(string $suffix, int $value): Gauge
  • ::histogram(string $suffix, int $value, float $sample_rate = 1.0): Histogram
  • ::time(string $suffix, (float|callable) $time): Time
  • ::unique(string $suffix, int $value): Unique

suffix 是您 DataDog 指标名称的最后一部分。所有 DataDog 指标名称将组成 DATADOG_PROJECT_NAME.suffix,其中 DATADOG_PROJECT_NAME 从环境变量中加载,如果这些变量未设置,则您的指标名称的起始部分将默认为 notset.suffix。此外,所有 DataDog 日志都将附带额外的标签 DATADOG_SERVICE_NAME,该标签也从环境变量中加载,如果该变量未设置,则默认值为 notset

自定义报告器

您可以通过创建一个类并分别实现 HostLogInterfaceRemoteLogInterface 来创建自己的 hostremote 报告器。

HostLogInterface

包含

  • use MVF\SystemLogger\HostLogInterface;

方法

  • ->info(string $message): \Exception|null : 应使用信息严重性记录日志。

  • ->warning(string $message): \Exception|null : 应使用警告严重性记录日志。

  • ->error(string $message): \Exception|null : 应使用错误严重性记录日志。

每个方法接收带有替换 占位符message。如果在报告器中抛出异常,则应捕获并返回它,系统记录器将在执行所有其他日志后重新抛出它。Laravel 标准输出报告器的示例可以在 此处 找到。

RemoteLogInterface

包含

  • use MVF\SystemLogger\RemoteLogInterface;

方法

  • ->send(string[] $tags): \Exception|null : tags 数组始终至少包含以下值 [ "<info|warning|error>", "message" => "<message>", <DATADOG_SERVICE_NAME> ]。如果在报告器中抛出异常,则应捕获并返回它,系统记录器将在执行所有其他日志后重新抛出它。

  • ->getValue(): string|int : 如果您的远程报告器有一个可能返回的值,则此函数应返回它,否则返回字符串 NOT_IMPLEMENTED