Action_Logger.php
2 weeks ago
Admin.php
2 weeks ago
Canonical_Formatter.php
2 weeks ago
File_Logger.php
2 weeks ago
Logger.php
2 weeks ago
Monolog_Logger.php
2 weeks ago
Null_Logger.php
2 weeks ago
Service_Provider.php
2 weeks ago
Service_Provider.php
177 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ${CARET} |
| 4 | * |
| 5 | * @since 4.9.16 |
| 6 | * |
| 7 | * @package Tribe\Log |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | namespace Tribe\Log; |
| 12 | |
| 13 | |
| 14 | use Monolog\Handler\ErrorLogHandler; |
| 15 | use Monolog\Logger; |
| 16 | |
| 17 | class Service_Provider extends \tad_DI52_ServiceProvider { |
| 18 | |
| 19 | /** |
| 20 | * Binds and sets up implementations. |
| 21 | * |
| 22 | * @since 4.9.16 |
| 23 | */ |
| 24 | public function register() { |
| 25 | $this->container->singleton( 'log', $this ); |
| 26 | $this->container->singleton( static::class, $this ); |
| 27 | $this->container->singleton( Logger::class, [ $this, 'build_logger' ] ); |
| 28 | $this->container->singleton( 'monolog', |
| 29 | function () { |
| 30 | return $this->container->make( Logger::class ); |
| 31 | } |
| 32 | ); |
| 33 | |
| 34 | add_action( 'tribe_log', [ $this, 'dispatch_log' ], 10, 3 ); |
| 35 | |
| 36 | /** |
| 37 | * Filters whether to make the Action Logger available as logger or not. |
| 38 | * |
| 39 | * @since 4.9.16 |
| 40 | * |
| 41 | * @param bool $use_action_logger Whether to allow logging messages from the \Tribe\Log\Logger class using the |
| 42 | * `tribe_log` action or not. |
| 43 | */ |
| 44 | $use_action_logger = apply_filters( 'tribe_log_use_action_logger', false ); |
| 45 | |
| 46 | if ( $use_action_logger ) { |
| 47 | add_filter( 'tribe_common_logging_engines', [ $this, 'add_logging_engine' ] ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Builds and returns the Monolog Logger instance that will listen to the `tribe_log` action. |
| 53 | * |
| 54 | * To avoid the over-head introduced by filtering the filters are applied here, only once, when the instance is |
| 55 | * first built. Any later call will use the singleton instance stored in the container. |
| 56 | * |
| 57 | * @since 4.9.16 |
| 58 | * |
| 59 | * @return Logger |
| 60 | */ |
| 61 | public function build_logger() { |
| 62 | /** |
| 63 | * Filters the level of the messages that will be logged. |
| 64 | * |
| 65 | * The threshold is inclusive of the level; it default to log any warning and above. |
| 66 | * |
| 67 | * @since 4.9.16 |
| 68 | * |
| 69 | * @param int The threshold level; if the level of a message is this level or above, then it will be logged. |
| 70 | * |
| 71 | * @see \Monolog\Logger for possible levels. |
| 72 | */ |
| 73 | $level_threshold = apply_filters( 'tribe_log_level', Logger::WARNING ); |
| 74 | |
| 75 | $error_log_handler = new ErrorLogHandler( ErrorLogHandler::OPERATING_SYSTEM, $level_threshold ); |
| 76 | |
| 77 | /** |
| 78 | * Filters whether to use canonical format for the logs or not. |
| 79 | * |
| 80 | * @since 4.9.16 |
| 81 | * |
| 82 | * @param bool $use_canonical_format Whether to use canonical format for the logs or not; defaults to `true`. |
| 83 | */ |
| 84 | $use_canonical_format = apply_filters( 'tribe_log_canonical', true ); |
| 85 | |
| 86 | if ( $use_canonical_format ) { |
| 87 | $error_log_handler->setFormatter( new Canonical_Formatter() ); |
| 88 | } |
| 89 | |
| 90 | $handlers = [ |
| 91 | 'default' => $error_log_handler |
| 92 | ]; |
| 93 | |
| 94 | /** |
| 95 | * Filters the list of handlers that will handle dispatched log messages. |
| 96 | * |
| 97 | * All handlers should implement the `\Monolog\Handler\HandlerInterface`. |
| 98 | * |
| 99 | * @since 4.9.16 |
| 100 | * |
| 101 | * @param array $handlers An array of default log handlers. |
| 102 | */ |
| 103 | $handlers = apply_filters( 'tribe_log_handlers', $handlers ); |
| 104 | |
| 105 | // Monolog will log to stderr when no handlers are set. |
| 106 | $logger = new Monolog_Logger( Monolog_Logger::DEFAULT_CHANNEL ); |
| 107 | |
| 108 | $logger->setHandlers( $handlers ); |
| 109 | |
| 110 | return $logger; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Dispatch a message of a specific level. |
| 115 | * |
| 116 | * Available levels are: `debug`, `info`, `notice`, `warning`, `error`, `critical`, `alert`, `emergency`. |
| 117 | * |
| 118 | * @since 4.9.16 |
| 119 | * |
| 120 | * @param string|int $level Either the log level or the log pretty name, see long description. |
| 121 | * @param string $message The message to log. |
| 122 | * @param array $context An array of values to define the context. |
| 123 | * |
| 124 | * @see \Monolog\Logger for the log level constants and names. |
| 125 | */ |
| 126 | public function dispatch_log( $level = 'debug', $message = '', array $context = [] ) { |
| 127 | // Goes from something like `debug` to `100`. |
| 128 | $level = is_numeric( $level ) ? $level : Logger::toMonologLevel( $level ); |
| 129 | |
| 130 | /** @var Logger $logger */ |
| 131 | $logger = $this->container->make( Logger::class ); |
| 132 | |
| 133 | $logger->log( $level, $message, $context ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Makes the action-based logging engine available in the backend. |
| 138 | * |
| 139 | * @since 4.9.16 |
| 140 | * |
| 141 | * @param array $logging_engines An array of available logging engines. |
| 142 | * |
| 143 | * @return array The updated array of logging engines. |
| 144 | */ |
| 145 | public function add_logging_engine( array $logging_engines = [] ) { |
| 146 | $logging_engines[ Action_Logger::class ] = new Action_Logger(); |
| 147 | |
| 148 | return $logging_engines; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Enables logging in the service provider, if not already enabled. |
| 153 | * |
| 154 | * @since 4.12.15 |
| 155 | */ |
| 156 | public function enable() { |
| 157 | if ( has_action( 'tribe_log', [ $this, 'dispatch_log' ] ) ) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | add_action( 'tribe_log', [ $this, 'dispatch_log' ] ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Disables the logging functions. |
| 166 | * |
| 167 | * @since 4.12.15 |
| 168 | */ |
| 169 | public function disable() { |
| 170 | if ( ! has_action( 'tribe_log', [ $this, 'dispatch_log' ] ) ) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | remove_action( 'tribe_log', [ $this, 'dispatch_log' ] ); |
| 175 | } |
| 176 | } |
| 177 |