定制 sebk/small-logger 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sebk/small-logger

最新稳定版本:1.1.1

Composer 安装命令:

composer require sebk/small-logger

包简介

A small logger

README 文档

README

Small logger is a simple php logger you can easily extends for your own need.

Basically, it implements possibility to log to : Standard output File (With log rotates) Http service such as logstash

It it easy to implement your own Formatter or Output writer.

SwitchLogicInterface allow you to implements your own logic to manage multiple log streams.

It also possible to add shortcut to logger for easier use by final developer.

Migrated

This lib has been migrated to framagit project.

A new composer package is available at https://packagist.org/packages/small/small-logger

Future commits will be done on framagit.

Install

composer require sebk/small-logger

Log to standard output

First define the switch loggic :

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

Then, you are abble to log :

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

Log to file

You can log to a single file :

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

Or redirect errors to another file :

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

In the second case, errors are redirected to the second file.

Then this call will write in '/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'
));

And this call will write in '/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'
));

Customizing logs

You can easy customize the behaviour of logs by writing your own classes implementing interfaces :

  • switch : the switch logic
  • formatter : log class diggest to log format
  • output : the output writer

For example, we want to write to logstash. We will use the http type with the output factory. To get the best appropriate output for your project, use the output factory :

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

New we have the output, we can create our switcher :

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;
    }
    
}

In the getStream method, you can put your logic to manage more than one stream depends on $log itself or additional $data.

For example, you can set up a stream for error level and a stream for critical level.

If you want, the $data array may inject information to switch in complex architectures.

Now define your switch in logger and log :

\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'
));

Unit tests

To run unit tests, you are require to build unit-test container :

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

Then the container will build environement for testing and launch tests.

If you want to develop and add unit tests, turn off the BUILD environement var in docker-compose.yml by setting it to 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
...

统计信息

  • 总下载量: 1.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2022-10-16