fliglio/logging

2.3.1 2020-04-29 14:10 UTC

This package is auto-updated.

Last update: 2024-09-06 02:32:47 UTC


README

Build Status Latest Stable Version

Fliglio.Logging

使用以下命令运行测试

$ make test

提供 \Fliglio\Logging\FLog trait 以添加兼容 psr 的 log() 方法。

使用 monolog 进行配置

$ composer require monolog/monolog

//index.php
$logger = new Logger("Demo");¬
$handler = new StreamHandler('php://stderr');¬
$logger->pushHandler($handler);¬

FLogRegistry::set(new FLogger($logger , new FLogContext()));

使用 KLogger 进行配置

$ composer require katzgrau/klogger

//index.php
$logger = new \Katzgrau\KLogger\Logger('php://stderr');

FLogRegistry::set(new FLogger($logger , new FLogContext()));

在类中使用(带有 trait)

class Demo {
	use FLog;

	public function __construct() {
		$this->log()->info("hello constructor!");
	}
	
	public function doATask($taskType) {
		$this->log()->context()->add("taskType", $taskType);
		$this->log()->info("Starting Task");
		foreach (["a", "b", "c", "d"] as $subTask) {
			$this->log()->context()->add("subTask", $subTask);
			$this->log()->info("Starting SubTask");

			try {
				doWork();
				$this->log()->info("SubTask Completed!");
			} catch (\Exception $e) {
				$this->log()->warning("SubtTask Problem!", ["e" => $e]);
			
			}
		}
		$this->log()->context()->remove("subTask");
		$this->log()->info("SubtTask Completed!");
		$this->log()->context()->remove("taskType");
	}
}


$logger = new \Katzgrau\KLogger\Logger('php://stderr');
$flogger = new FLogger($logger , new FLogContext()));
FLogRegistry::set($flogger);


$flogger->info("Starting...");

$demo = new Demo();
$demo->doATask("Foo");

$flogger->info("Ending...");

输出

[2019-10-04 20:25:28.001557] [info] Starting...
[2019-10-04 20:25:28.001606] [info] hello constructor!
[2019-10-04 20:25:28.001641] [info] Starting Task
    taskType: 'Foo'
[2019-10-04 20:25:28.001688] [info] Starting SubTask
    taskType: 'Foo'
    subTask: 'a'
[2019-10-04 20:25:28.001739] [info] SubTask Completed!
    taskType: 'Foo'
    subTask: 'a'
[2019-10-04 20:25:28.001780] [info] Starting SubTask
    taskType: 'Foo'
    subTask: 'b'
[2019-10-04 20:25:28.001821] [info] SubTask Completed!
    taskType: 'Foo'
    subTask: 'b'
[2019-10-04 20:25:28.001862] [info] Starting SubTask
    taskType: 'Foo'
    subTask: 'c'
[2019-10-04 20:25:28.001902] [info] SubTask Completed!
    taskType: 'Foo'
    subTask: 'c'
[2019-10-04 20:25:28.001945] [info] Starting SubTask
    taskType: 'Foo'
    subTask: 'd'
[2019-10-04 20:25:28.001985] [info] SubTask Completed!
    taskType: 'Foo'
    subTask: 'd'
[2019-10-04 20:25:28.002028] [info] SubtTask Completed!
    taskType: 'Foo'
[2019-10-04 20:25:28.002070] [info] Ending...