| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register logging definitions in the container. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Log; |
| 11 |
|
| 12 |
use SolidWP\Performance\Contracts\Service_Provider; |
| 13 |
use SolidWP\Performance\Log\Handlers\Null_Handler; |
| 14 |
use SolidWP\Performance\Monolog\Formatter\LineFormatter; |
| 15 |
use SolidWP\Performance\Monolog\Handler\ErrorLogHandler; |
| 16 |
use SolidWP\Performance\Monolog\Logger; |
| 17 |
use SolidWP\Performance\Monolog\Processor\PsrLogMessageProcessor; |
| 18 |
use SolidWP\Performance\Psr\Log\LoggerInterface; |
| 19 |
|
| 20 |
/** |
| 21 |
* Register logging definitions in the container. |
| 22 |
*/ |
| 23 |
final class Provider extends Service_Provider { |
| 24 |
|
| 25 |
/** |
| 26 |
* @inheritDoc |
| 27 |
*/ |
| 28 |
public function register(): void { |
| 29 |
$this->register_logging(); |
| 30 |
$this->register_log_actions(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register our logging system. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
private function register_logging(): void { |
| 39 |
// Enable logging to the error log if WP_DEBUG is enabled and error_log is not listed in the php.ini/fpm disable_functions directive. |
| 40 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && function_exists( 'error_log' ) ) { |
| 41 |
/** |
| 42 |
* Filter the log level to use when debugging. |
| 43 |
* |
| 44 |
* @param string $log_level One of: debug, info, notice, warning, error, critical, alert, emergency |
| 45 |
*/ |
| 46 |
$log_level = apply_filters( 'solidwp/performance/log/level', 'debug' ); |
| 47 |
|
| 48 |
$this->container->when( ErrorLogHandler::class ) |
| 49 |
->needs( '$level' ) |
| 50 |
->give( static fn(): int => Logger::toMonologLevel( $log_level ) ); |
| 51 |
|
| 52 |
$this->container->when( LineFormatter::class ) |
| 53 |
->needs( '$dateFormat' ) |
| 54 |
->give( static fn(): string => 'd/M/Y:H:i:s O' ); |
| 55 |
|
| 56 |
$this->container->bind( |
| 57 |
LoggerInterface::class, |
| 58 |
function () { |
| 59 |
$logger = new Logger( 'solid_performance' ); |
| 60 |
$handler = $this->container->get( ErrorLogHandler::class ); |
| 61 |
$handler->setFormatter( $this->container->get( LineFormatter::class ) ); |
| 62 |
|
| 63 |
$logger->pushHandler( $handler ); |
| 64 |
$logger->pushProcessor( new PsrLogMessageProcessor() ); |
| 65 |
|
| 66 |
return $logger; |
| 67 |
} |
| 68 |
); |
| 69 |
} else { |
| 70 |
// Disable logging. |
| 71 |
$this->container->bind( |
| 72 |
LoggerInterface::class, |
| 73 |
static function () { |
| 74 |
$logger = new Logger( 'null' ); |
| 75 |
$logger->pushHandler( new Null_Handler() ); |
| 76 |
|
| 77 |
return $logger; |
| 78 |
} |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Register actions so logging can be performed via do_action(). |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
private function register_log_actions(): void { |
| 89 |
add_action( Log::EMERGENCY, $this->container->callback( LoggerInterface::class, 'emergency' ), 10, 2 ); |
| 90 |
add_action( Log::ALERT, $this->container->callback( LoggerInterface::class, 'alert' ), 10, 2 ); |
| 91 |
add_action( Log::CRITICAL, $this->container->callback( LoggerInterface::class, 'critical' ), 10, 2 ); |
| 92 |
add_action( Log::ERROR, $this->container->callback( LoggerInterface::class, 'error' ), 10, 2 ); |
| 93 |
add_action( Log::WARNING, $this->container->callback( LoggerInterface::class, 'warning' ), 10, 2 ); |
| 94 |
add_action( Log::NOTICE, $this->container->callback( LoggerInterface::class, 'notice' ), 10, 2 ); |
| 95 |
add_action( Log::DEBUG, $this->container->callback( LoggerInterface::class, 'debug' ), 10, 2 ); |
| 96 |
} |
| 97 |
} |
| 98 |
|