LoggerOne - 高效、簡約、強擴展性PHP日誌類庫

LoggerOne

One Efficient & Light & Flexible high performance PHP log implemention of PSR-3
一個高效、簡約、靈活高性能的遵循PSR-3的PHP日誌類庫實現。php

特性/Features

Inherently cached message record using PHP object attribute.
自然的緩存特性(Logger實例屬性),延遲批量寫入。git

安裝&使用/Istall&Usage

Install

$ composer require logger-one/logger-one

Invoke by default mode - 默認的調用方式

Will using FileHandler create a log file named for %Y%m%d and appendix .log at LoggerOne root folder, ervery message as line with the original - 在默認狀況下,Logger會使用FileHandlerCommonFormatter 輸出一個保存在Logger同目錄的命名格式爲 %Y%m%d的日誌文件。

$logger = LoggerOne\Logger::getInstance();  // Strongly recommend

$message = 'test log message {placeholder1} {placeholder2}';

$replacements = ['placeholder1' => 'Foo', 'placeholder2' => 'bar'];

$logger->info($message, $replacements);

Change FileHandler default path - 改變默認日誌文件保存地址

$logFile = './tmp/loggerOne.log'; // Your path where you want

$handler = new LoggerOne\Handler\FileHandler($logFile);

$logger->setHandler($handler);

$logger->info($message);

Specific Handler & Formatter - 指定使用特定的Handler/Formatter

$handler = new LoggerOne\Handler\FooHandler();

$handler = new LoggerOne\Formatter\BarFormatter();

$logger->setHandler($handler)->setFormatter($formatter);

$logger->info('some test log message');

Flush log message immediately - 當即輸出到日誌文件的調用方法

Once calling flush method will write all previous message by handler - 這樣將不緩存日誌內容,而把調用前全部的message當即寫入到標的處。

...
$logger->info('some test log message')->flush();
...

定製&擴展/Customization&Extending

Handler Extension Simple - Handler的擴展方式

Put your own MySQLHandler.php in LoggerOne\Handler folder - 將你的擴展文件放置到LoggerOne/Handler目錄

<?php
namespace LoggerOne\Handler;

class MySQLHandler implements Handler
{
    protected $db;
    
    protected $table_name;
    
    
    public function __construct(string $table_name, object $dbHandle = null)
    {
        $this->table_name = $table_name;
        $this->db = $dbHandle;
    }
    
    public function write($messages)
    {
        $batchData = [];
        foreach ($messages as $level => $message) {
            $batchData[] = ['level' => $level, 'message' => $message, 'created' => time()];
        }
        $this->db->insertBatch($this->table_name, $batchData);
    }
}

Use MySQLHandler.php - 使用擴展

$logger = new LoggerOne\Logger();

$handler = new LoggerOne\Handler\MysqlHandler($yourLogTableName, $yourDbHandle);

$logger->setHandler($handler);

$logger->info('some test log message');

Formatter Extension Simple - Formatter的擴展方式

The way just like Handler extending - 聰明的你,你能夠按你想發靈活的編寫你但願的Formatter處理類,過程和Handler擴展流程同理,放到Formatter目錄就能夠開始使用了。

注意/Notice

  • If you have not using composer, should remember load all dependency class in your way. - 若是你不使用composer, 請記得用你的方式加載全部必須的依賴類庫。github

  • Try to keep your handler write in batches for performace - 若是你作了Handler擴展,爲了性能請儘可能確保它是批量完成寫操做的。sql

Github地址: https://github.com/lijiebin/L...shell

相關文章
相關標籤/搜索