One Efficient & Light & Flexible high performance PHP log implemention of PSR-3
一個高效、簡約、靈活高性能的遵循PSR-3的PHP日誌類庫實現。php
Inherently cached message record using PHP object attribute.
自然的緩存特性(Logger實例屬性),延遲批量寫入。git
$ composer require logger-one/logger-one
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 = LoggerOne\Logger::getInstance(); // Strongly recommend $message = 'test log message {placeholder1} {placeholder2}'; $replacements = ['placeholder1' => 'Foo', 'placeholder2' => 'bar']; $logger->info($message, $replacements);
FileHandler
default path - 改變默認日誌文件保存地址$logFile = './tmp/loggerOne.log'; // Your path where you want $handler = new LoggerOne\Handler\FileHandler($logFile); $logger->setHandler($handler); $logger->info($message);
Handler
/Formatter
$handler = new LoggerOne\Handler\FooHandler(); $handler = new LoggerOne\Formatter\BarFormatter(); $logger->setHandler($handler)->setFormatter($formatter); $logger->info('some test log message');
flush
method will write all previous message by handler - 這樣將不緩存日誌內容,而把調用前全部的message當即寫入到標的處。... $logger->info('some test log message')->flush(); ...
Handler
的擴展方式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); } }
MySQLHandler.php
- 使用擴展$logger = new LoggerOne\Logger(); $handler = new LoggerOne\Handler\MysqlHandler($yourLogTableName, $yourDbHandle); $logger->setHandler($handler); $logger->info('some test log message');
Formatter
的擴展方式Formatter
處理類,過程和Handler
擴展流程同理,放到Formatter
目錄就能夠開始使用了。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