small/logger

一个小型日志记录器

22.0.2 2023-12-26 14:12 UTC

This package is auto-updated.

Last update: 2024-09-16 12:31:51 UTC


README

small-logger

Small logger 是一个简单的 PHP 日志记录器,您可以轻松地扩展以满足自己的需求。

基本上,它实现了将日志记录到:标准输出、文件(带有日志轮转)、Http 服务(如 logstash)的可能性。

实现自己的格式化器或输出写入器非常简单。

SwitchLogicInterface 允许您实现自己的逻辑来管理多个日志流。

还可以为日志添加快捷方式,以便最终开发者更容易使用。

安装

composer require sebk/small-logger

记录到标准输出

首先定义日志切换逻辑

\Sebk\SmallLogger\Logger::setSwitchLogic(new \Sebk\SmallLogger\SwitchLogic\DefaultSwitchLogic());

然后,您就可以记录日志了

\Sebk\SmallLogger\Logger::log(new \Sebk\SmallLogger\Log\BasicLog(
    new \DateTime(),
    \Sebk\SmallLogger\Contracts\LogInterface::ERR_LEVEL_INFO,
    'This is an info log'
));

记录到文件

您可以记录到单个文件

\Sebk\SmallLogger\Logger::setSwitchLogic(new \Sebk\SmallLogger\SwitchLogic\DefaultFileSwitchLogic('/var/log/my-log.log'));

或将错误记录到另一个文件

\Sebk\SmallLogger\Logger::setSwitchLogic(new \Sebk\SmallLogger\SwitchLogic\DefaultFileSwitchLogic('/var/log/my-log.log', '/var/log/my-error-log.log'));

在第二种情况下,错误将被重定向到第二个文件。

然后这个调用将写入 '/var/log/my-log.log'

\Sebk\SmallLogger\Logger::log(new \Sebk\SmallLogger\Log\BasicLog(
    new \DateTime(),
    \Sebk\SmallLogger\Contracts\LogInterface::ERR_LEVEL_INFO,
    'This is an info log'
));

而这个调用将写入 '/var/log/my-error-log.log'

\Sebk\SmallLogger\Logger::log(new \Sebk\SmallLogger\Log\BasicLog(
    new \DateTime(),
    \Sebk\SmallLogger\Contracts\LogInterface::ERR_LEVEL_ERROR,
    'This is an info log'
));

自定义日志

您可以通过编写自己的实现接口的类来轻松自定义日志的行为

  • switch:切换逻辑
  • formatter:日志类将日志内容转换为日志格式
  • output:输出写入器

例如,我们想写入 logstash。我们将使用带有输出工厂的 http 类型。为了获得最适合您项目的输出,请使用输出工厂。

$output = (new \Sebk\SmallLogger\Output\OutputFactory())->get('http', new \Sebk\SmallLogger\Output\Config\HttpConfig('localhost', 8080, false));

现在我们有了输出,我们可以创建自己的切换器


namespace App\Logs;

class HttpSwitcherLogic implements \Sebk\SmallLogger\Contracts\SwitchLogicInterface
{

    protected \Sebk\SmallLogger\Contracts\StreamInterface $stream;
    
    public function __construct(\Sebk\SmallLogger\Output\Config\HttpConfig $config)
    {
        $this->stream = new \Sebk\SmallLogger\Stream\Stream(
            new \Sebk\SmallLogger\Formatter\JsonFormatter(), 
            (new \Sebk\SmallLogger\Output\OutputFactory())->get('http', $config)
        );
    }
    
    public function getStream(\Sebk\SmallLogger\Contracts\LogInterface $log, array $data = []) : \Sebk\SmallLogger\Contracts\StreamInterface
    {
        $this->stream;
    }
    
}

在 getStream 方法中,您可以根据 $log 本身或附加的 $data 来管理多个流。

例如,您可以设置一个错误级别的流和一个关键级别的流。

如果您愿意,$data 数组可以注入信息以在复杂架构中进行切换。

现在在日志中定义您的切换并记录

\Sebk\SmallLogger\Logger::setSwitchLogic(new HttpSwitcher(
    new \Sebk\SmallLogger\Output\Config\HttpConfig(
        'logstash.my-domain.com',
        8080,
        true
    )
));
\Sebk\SmallLogger\Logger::log(new \Sebk\SmallLogger\Log\BasicLog(
    new \DateTime(),
    \Sebk\SmallLogger\Contracts\LogInterface::ERR_LEVEL_ERROR,
    'This an error'
));

单元测试

要运行单元测试,您需要构建单元测试容器

$ sudo apt-get update && apt-get install docker docker-compose
$ docker-compose up -d --build

然后容器将构建测试环境并启动测试。

如果您想开发和添加单元测试,请通过在 docker-compose.yml 中将 BUILD 环境变量设置为 0 来关闭 BUILD 环境。

...
    environment:
      - BUILD=1 # If set to 0, the unit test are not launched and container will sleep to let you run all tests commands you want when you develop tests
...