0, 'alert' => 1, 'critical' => 2, 'error' => 3, 'warning' => 4, 'notice' => 5, 'info' => 6, 'debug' => 7, ]; /** * Minimum log level to capture. * * @var int */ private $min_priority; /** * Constructor. * * @param string $min_level Minimum level to log. */ public function __construct( string $min_level = 'info' ) { // Default to debug if WP_DEBUG is enabled if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { $min_level = 'debug'; } $this->min_priority = self::LEVELS[ strtolower( $min_level ) ] ?? 6; } /** * Logs with an arbitrary level. * * @param mixed $level * @param string|\Stringable $message * @param array $context * @return void */ public function log( $level, $message, array $context = [] ): void { $level_str = is_scalar( $level ) ? (string) $level : 'info'; $priority = self::LEVELS[ strtolower( $level_str ) ] ?? 6; if ( $priority > $this->min_priority ) { return; } $msg_str = is_scalar( $message ) ? (string) $message : ''; $output = sprintf( '[WebCalendar %s] %s', strtoupper( $level_str ), $msg_str ); if ( ! empty( $context ) ) { $output .= ' ' . (string) wp_json_encode( $context ); } $this->write( $output ); } /** * Write to the error log. * * Extracted to a method for easier unit testing. * * @param string $message Message to log. */ protected function write( string $message ): void { error_log( $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- this IS the logger } }