-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from sj-i/logger_wrapper
Logger wrapper
- Loading branch information
Showing
13 changed files
with
530 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the sj-i/php-profiler package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpProfiler\Lib\Log; | ||
|
||
use PhpProfiler\Lib\Log\StateCollector\NullStateCollector; | ||
use PhpProfiler\Lib\Log\StateCollector\StateCollector; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
use Psr\Log\NullLogger; | ||
|
||
final class Log | ||
{ | ||
public const LOG_LEVELS = [ | ||
LogLevel::EMERGENCY, | ||
LogLevel::ALERT, | ||
LogLevel::CRITICAL, | ||
LogLevel::ERROR, | ||
LogLevel::WARNING, | ||
LogLevel::NOTICE, | ||
LogLevel::INFO, | ||
LogLevel::DEBUG, | ||
]; | ||
|
||
private static ?LoggerInterface $logger = null; | ||
private static ?StateCollector $state_collector = null; | ||
|
||
public static function initializeLogger( | ||
LoggerInterface $logger, | ||
StateCollector $state_collector, | ||
): void { | ||
self::$logger = $logger; | ||
self::$state_collector = $state_collector; | ||
} | ||
|
||
public static function getLogger(): LoggerInterface | ||
{ | ||
if (!isset(self::$logger)) { | ||
self::$logger = new NullLogger(); | ||
} | ||
return self::$logger; | ||
} | ||
|
||
public static function getCollector(): StateCollector | ||
{ | ||
if (!isset(self::$state_collector)) { | ||
self::$state_collector = new NullStateCollector(); | ||
} | ||
return self::$state_collector; | ||
} | ||
|
||
/** | ||
* @param value-of<self::LOG_LEVELS> $level | ||
*/ | ||
public static function log(string $level, string $message, array $context = []): void | ||
{ | ||
self::getLogger()->log($level, $message, $context + self::getCollector()->collect()); | ||
} | ||
|
||
public static function emergency(string $message, array $context = []): void | ||
{ | ||
self::log(LogLevel::EMERGENCY, $message, $context); | ||
} | ||
|
||
public static function alert(string $message, array $context = []): void | ||
{ | ||
self::log(LogLevel::ALERT, $message, $context); | ||
} | ||
|
||
public static function critical(string $message, array $context = []): void | ||
{ | ||
self::log(LogLevel::CRITICAL, $message, $context); | ||
} | ||
|
||
public static function error(string $message, array $context = []): void | ||
{ | ||
self::log(LogLevel::ERROR, $message, $context); | ||
} | ||
|
||
public static function warning(string $message, array $context = []): void | ||
{ | ||
self::log(LogLevel::WARNING, $message, $context); | ||
} | ||
|
||
public static function notice(string $message, array $context = []): void | ||
{ | ||
self::log(LogLevel::NOTICE, $message, $context); | ||
} | ||
|
||
public static function info(string $message, array $context = []): void | ||
{ | ||
self::log(LogLevel::INFO, $message, $context); | ||
} | ||
|
||
public static function debug(string $message, array $context = []): void | ||
{ | ||
self::log(LogLevel::DEBUG, $message, $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the sj-i/php-profiler package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpProfiler\Lib\Log\StateCollector; | ||
|
||
use PhpProfiler\Lib\Log\Log; | ||
|
||
use function debug_backtrace; | ||
|
||
final class CallerStateCollector implements StateCollector | ||
{ | ||
public function collect(): array | ||
{ | ||
$backtrace = debug_backtrace(limit: 5); | ||
|
||
$last_logger_frame = []; | ||
$caller_frame = []; | ||
|
||
foreach ($backtrace as $frame) { | ||
if ($this->isLoggerFrame($frame)) { | ||
$last_logger_frame = $frame; | ||
} elseif ($last_logger_frame) { | ||
$caller_frame = $frame; | ||
break; | ||
} | ||
} | ||
|
||
$result = []; | ||
if ($last_logger_frame) { | ||
$result['context'] = [ | ||
'file' => $last_logger_frame['file'], | ||
'line' => $last_logger_frame['line'], | ||
'class' => $caller_frame['class'] ?? '', | ||
'function' => $caller_frame['function'] ?? '', | ||
'args' => $caller_frame['args'] ?? '', | ||
]; | ||
} | ||
return $result; | ||
} | ||
|
||
private function isLoggerFrame(array $frame): bool | ||
{ | ||
if ($frame['class'] === Log::class) { | ||
if (in_array($frame['function'], ['log', ...Log::LOG_LEVELS], true)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the sj-i/php-profiler package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpProfiler\Lib\Log\StateCollector; | ||
|
||
final class GroupedStateCollector implements StateCollector | ||
{ | ||
/** @var StateCollector[] */ | ||
private array $collectors; | ||
|
||
/** | ||
* @param StateCollector ...$collectors | ||
*/ | ||
public function __construct(StateCollector ...$collectors) | ||
{ | ||
$this->collectors = $collectors; | ||
} | ||
|
||
public function collect(): array | ||
{ | ||
$result = []; | ||
foreach ($this->collectors as $collector) { | ||
$result += $collector->collect(); | ||
} | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the sj-i/php-profiler package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpProfiler\Lib\Log\StateCollector; | ||
|
||
final class NullStateCollector implements StateCollector | ||
{ | ||
public function collect(): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the sj-i/php-profiler package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpProfiler\Lib\Log\StateCollector; | ||
|
||
use function getmypid; | ||
use function memory_get_peak_usage; | ||
use function memory_get_usage; | ||
|
||
final class ProcessStateCollector implements StateCollector | ||
{ | ||
public function collect(): array | ||
{ | ||
return [ | ||
'pid' => getmypid(), | ||
'memory_usage' => memory_get_usage(), | ||
'peak_memory_usage' => memory_get_peak_usage(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the sj-i/php-profiler package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpProfiler\Lib\Log\StateCollector; | ||
|
||
interface StateCollector | ||
{ | ||
public function collect(): array; | ||
} |
Oops, something went wrong.