LoggerOne

LoggerOne

One Efficient & Light & Simple 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 = 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 = 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

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

定制&扩展/Customization&Extending

Handler Extension Simple

Put your own MySQLHandler.php in LoggerOne\Handler folder

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

The way just like Handler extending

注意/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扩展,为了性能请尽量确保它是批量完成写操作的
时间: 2024-11-05 01:13:55

LoggerOne的相关文章

Logback手记

1.什么是logback logback的产生是作为著名的日志框架log4j项目的一个继承者.它的设计者正是log4j的创造者Ceki Gülcü.logback在很大幅度上比现有的日志系统拥有更快的速度和更小的体积.logback也提供了独特和一些有用的特性,譬如标记.参数化的日志语句.有条件的堆栈跟踪和强大的事件过滤机制.这些只是运用logback提供的特性的一些例子.对于它自己的错误报表,logback依赖于状态对象,这样很大程度上促进了故障的排除.你可能更希望在日志之外依赖于状态对象.l