sebk/small-logger

此包已被废弃且不再维护。未建议替代包。

小型日志记录器

1.1.1 2022-11-10 09:31 UTC

This package is auto-updated.

Last update: 2023-02-26 18:11:50 UTC


README

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

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

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

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

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

迁移

此库已迁移到 framagit 项目。

新的 composer 包可在 https://packagist.org.cn/packages/small/small-logger 获取

未来的提交将在 framagit 上完成。

安装

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

...
    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
...