LoggerOne
One Efficient & Light & Flexible high performance PHP log implemention of PSR-3
一個高效、簡約、靈活高效能的遵循PSR-3的PHP日誌類庫實現。
特性/Features
Inherently cached message record using PHP object attribute.
天然的快取特性(Logger例項屬性),延遲批量寫入。
安裝&使用/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
會使用FileHandler
和 CommonFormatter
輸出一個儲存在Logger
同目錄的命名格式為 %Y%m%d
的日誌檔案。
$logger = LoggerOneLogger::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 LoggerOneHandlerFileHandler($logFile);
$logger->setHandler($handler);
$logger->info($message);
Specific Handler & Formatter – 指定使用特定的Handler
/Formatter
$handler = new LoggerOneHandlerFooHandler();
$handler = new LoggerOneFormatterBarFormatter();
$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 LoggerOneHandler
folder – 將你的擴充套件檔案放置到LoggerOne/Handler
目錄
<?php
namespace LoggerOneHandler;
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 LoggerOneLogger();
$handler = new LoggerOneHandlerMysqlHandler($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, 請記得用你的方式載入所有必須的依賴類庫。
-
Try to keep your handler write in batches for performace – 如果你做了Handler擴充套件,為了效能請儘量確保它是批量完成寫操作的。
Github地址: https://github.com/lijiebin/L…