diff --git a/Engine/ArrayLog.php b/Engine/ArrayLog.php index 2c14fd72d..7c812afcf 100644 --- a/Engine/ArrayLog.php +++ b/Engine/ArrayLog.php @@ -33,7 +33,7 @@ class ArrayLog extends BaseLog * * @var array */ - protected array $_defaultConfig = [ + protected array $defaultConfig = [ 'levels' => [], 'scopes' => [], 'formatter' => [ @@ -56,7 +56,7 @@ class ArrayLog extends BaseLog * @param \Stringable|string $message The message you want to log. * @param array $context Additional information about the logged message * @return void - * @see \Cake\Log\Log::$_levels + * @see \Cake\Log\Log::$levels * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint */ public function log($level, Stringable|string $message, array $context = []): void diff --git a/Engine/BaseLog.php b/Engine/BaseLog.php index 19594c88f..04d7f1ad4 100644 --- a/Engine/BaseLog.php +++ b/Engine/BaseLog.php @@ -24,7 +24,6 @@ use Psr\Log\AbstractLogger; use Serializable; use Stringable; -use function Cake\Core\deprecationWarning; /** * Base log engine class. @@ -38,7 +37,7 @@ abstract class BaseLog extends AbstractLogger * * @var array */ - protected array $_defaultConfig = [ + protected array $defaultConfig = [ 'levels' => [], 'scopes' => [], 'formatter' => DefaultFormatter::class, @@ -58,23 +57,18 @@ public function __construct(array $config = []) { $this->setConfig($config); - // Backwards compatibility shim as we can't deprecate using false because of how 4.x merges configuration. - if ($this->_config['scopes'] === false) { - deprecationWarning('5.0.0', 'Using `false` to disable logging scopes is deprecated. Use `null` instead.'); - $this->_config['scopes'] = null; - } - if ($this->_config['scopes'] !== null) { - $this->_config['scopes'] = (array)$this->_config['scopes']; + if ($this->config['scopes'] !== null) { + $this->config['scopes'] = (array)$this->config['scopes']; } - $this->_config['levels'] = (array)$this->_config['levels']; + $this->config['levels'] = (array)$this->config['levels']; - if (!empty($this->_config['types']) && empty($this->_config['levels'])) { - $this->_config['levels'] = (array)$this->_config['types']; + if (!empty($this->config['types']) && empty($this->config['levels'])) { + $this->config['levels'] = (array)$this->config['types']; } /** @var \Cake\Log\Formatter\AbstractFormatter|array|class-string<\Cake\Log\Formatter\AbstractFormatter> $formatter */ - $formatter = $this->_config['formatter'] ?? DefaultFormatter::class; + $formatter = $this->config['formatter'] ?? DefaultFormatter::class; if (!is_object($formatter)) { if (is_array($formatter)) { /** @var class-string<\Cake\Log\Formatter\AbstractFormatter> $class */ @@ -97,7 +91,7 @@ public function __construct(array $config = []) */ public function levels(): array { - return $this->_config['levels']; + return $this->config['levels']; } /** @@ -107,7 +101,7 @@ public function levels(): array */ public function scopes(): ?array { - return $this->_config['scopes']; + return $this->config['scopes']; } /** diff --git a/Engine/ConsoleLog.php b/Engine/ConsoleLog.php index 49dbc7fcd..bd73ecac7 100644 --- a/Engine/ConsoleLog.php +++ b/Engine/ConsoleLog.php @@ -31,7 +31,7 @@ class ConsoleLog extends BaseLog * * @var array */ - protected array $_defaultConfig = [ + protected array $defaultConfig = [ 'stream' => 'php://stderr', 'levels' => null, 'scopes' => [], @@ -47,7 +47,7 @@ class ConsoleLog extends BaseLog * * @var \Cake\Console\ConsoleOutput */ - protected ConsoleOutput $_output; + protected ConsoleOutput $output; /** * Constructs a new Console Logger. @@ -67,17 +67,17 @@ public function __construct(array $config = []) { parent::__construct($config); - $config = $this->_config; + $config = $this->config; if ($config['stream'] instanceof ConsoleOutput) { - $this->_output = $config['stream']; + $this->output = $config['stream']; } elseif (is_string($config['stream'])) { - $this->_output = new ConsoleOutput($config['stream']); + $this->output = new ConsoleOutput($config['stream']); } else { throw new InvalidArgumentException('`stream` not a ConsoleOutput nor string'); } if (isset($config['outputAs'])) { - $this->_output->setOutputAs($config['outputAs']); + $this->output->setOutputAs($config['outputAs']); } } @@ -88,12 +88,12 @@ public function __construct(array $config = []) * @param \Stringable|string $message The message you want to log. * @param array $context Additional information about the logged message * @return void - * @see \Cake\Log\Log::$_levels + * @see \Cake\Log\Log::$levels * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint */ public function log($level, Stringable|string $message, array $context = []): void { $message = $this->interpolate($message, $context); - $this->_output->write($this->formatter->format($level, $message, $context)); + $this->output->write($this->formatter->format($level, $message, $context)); } } diff --git a/Engine/FileLog.php b/Engine/FileLog.php index 5538d1e54..d68017e39 100644 --- a/Engine/FileLog.php +++ b/Engine/FileLog.php @@ -19,6 +19,7 @@ use Cake\Log\Formatter\DefaultFormatter; use Cake\Utility\Text; use Stringable; +use function Cake\Core\triggerWarning; /** * File Storage stream for Logging. Writes logs to different files @@ -45,7 +46,7 @@ class FileLog extends BaseLog * * @var array */ - protected array $_defaultConfig = [ + protected array $defaultConfig = [ 'path' => null, 'file' => null, 'types' => null, @@ -65,21 +66,21 @@ class FileLog extends BaseLog * * @var string */ - protected string $_path; + protected string $path; /** * The name of the file to save logs into. * * @var string|null */ - protected ?string $_file = null; + protected ?string $file = null; /** * Max file size, used for log file rotation. * * @var int|null */ - protected ?int $_size = null; + protected ?int $size = null; /** * Sets protected properties based on config provided @@ -90,23 +91,23 @@ public function __construct(array $config = []) { parent::__construct($config); - $this->_path = $this->getConfig('path', sys_get_temp_dir() . DIRECTORY_SEPARATOR); - if (!is_dir($this->_path)) { - mkdir($this->_path, $this->_config['dirMask'] ^ umask(), true); + $this->path = $this->getConfig('path', sys_get_temp_dir() . DIRECTORY_SEPARATOR); + if (!is_dir($this->path)) { + mkdir($this->path, $this->config['dirMask'] ^ umask(), true); } - if (!empty($this->_config['file'])) { - $this->_file = $this->_config['file']; - if (!str_ends_with($this->_file, '.log')) { - $this->_file .= '.log'; + if (!empty($this->config['file'])) { + $this->file = $this->config['file']; + if (!str_ends_with($this->file, '.log')) { + $this->file .= '.log'; } } - if (!empty($this->_config['size'])) { - if (is_numeric($this->_config['size'])) { - $this->_size = (int)$this->_config['size']; + if (!empty($this->config['size'])) { + if (is_numeric($this->config['size'])) { + $this->size = (int)$this->config['size']; } else { - $this->_size = Text::parseFileSize($this->_config['size']); + $this->size = Text::parseFileSize($this->config['size']); } } } @@ -118,7 +119,7 @@ public function __construct(array $config = []) * @param \Stringable|string $message The message you want to log. * @param array $context Additional information about the logged message * @return void - * @see \Cake\Log\Log::$_levels + * @see \Cake\Log\Log::$levels * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint */ public function log($level, Stringable|string $message, array $context = []): void @@ -126,13 +127,13 @@ public function log($level, Stringable|string $message, array $context = []): vo $message = $this->interpolate($message, $context); $message = $this->formatter->format($level, $message, $context); - $filename = $this->_getFilename($level); - if ($this->_size) { - $this->_rotateFile($filename); + $filename = $this->getFilename($level); + if ($this->size) { + $this->rotateFile($filename); } - $pathname = $this->_path . $filename; - $mask = $this->_config['mask']; + $pathname = $this->path . $filename; + $mask = $this->config['mask']; if (!$mask) { file_put_contents($pathname, $message . "\n", FILE_APPEND); @@ -145,10 +146,10 @@ public function log($level, Stringable|string $message, array $context = []): vo if (!$selfError && !$exists && !chmod($pathname, (int)$mask)) { $selfError = true; - trigger_error(vsprintf( + triggerWarning(vsprintf( 'Could not apply permission mask `%s` on log file `%s`', [$mask, $pathname], - ), E_USER_WARNING); + )); $selfError = false; } } @@ -159,12 +160,12 @@ public function log($level, Stringable|string $message, array $context = []): vo * @param string $level The level of log. * @return string File name */ - protected function _getFilename(string $level): string + protected function getFilename(string $level): string { $debugTypes = ['notice', 'info', 'debug']; - if ($this->_file) { - $filename = $this->_file; + if ($this->file) { + $filename = $this->file; } elseif ($level === 'error' || $level === 'warning') { $filename = 'error.log'; } elseif (in_array($level, $debugTypes, true)) { @@ -184,19 +185,19 @@ protected function _getFilename(string $level): string * @return bool|null True if rotated successfully or false in case of error. * Null if file doesn't need to be rotated. */ - protected function _rotateFile(string $filename): ?bool + protected function rotateFile(string $filename): ?bool { - $filePath = $this->_path . $filename; + $filePath = $this->path . $filename; clearstatcache(true, $filePath); if ( !is_file($filePath) || - filesize($filePath) < $this->_size + filesize($filePath) < $this->size ) { return null; } - $rotate = $this->_config['rotate']; + $rotate = $this->config['rotate']; if ($rotate === 0) { $result = unlink($filePath); } else { diff --git a/Engine/SyslogLog.php b/Engine/SyslogLog.php index 543a2d51a..4aafa58fc 100644 --- a/Engine/SyslogLog.php +++ b/Engine/SyslogLog.php @@ -51,7 +51,7 @@ class SyslogLog extends BaseLog * * @var array */ - protected array $_defaultConfig = [ + protected array $defaultConfig = [ 'levels' => [], 'scopes' => [], 'flag' => LOG_ODELAY, @@ -68,7 +68,7 @@ class SyslogLog extends BaseLog * * @var array */ - protected array $_levelMap = [ + protected array $levelMap = [ 'emergency' => LOG_EMERG, 'alert' => LOG_ALERT, 'critical' => LOG_CRIT, @@ -84,7 +84,7 @@ class SyslogLog extends BaseLog * * @var bool */ - protected bool $_open = false; + protected bool $open = false; /** * Writes a message to syslog @@ -96,25 +96,25 @@ class SyslogLog extends BaseLog * @param \Stringable|string $message The message you want to log. * @param array $context Additional information about the logged message * @return void - * @see \Cake\Log\Log::$_levels + * @see \Cake\Log\Log::$levels * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint */ public function log($level, Stringable|string $message, array $context = []): void { - if (!$this->_open) { - $config = $this->_config; - $this->_open($config['prefix'], $config['flag'], $config['facility']); - $this->_open = true; + if (!$this->open) { + $config = $this->config; + $this->open($config['prefix'], $config['flag'], $config['facility']); + $this->open = true; } $priority = LOG_DEBUG; - if (isset($this->_levelMap[$level])) { - $priority = $this->_levelMap[$level]; + if (isset($this->levelMap[$level])) { + $priority = $this->levelMap[$level]; } $lines = explode("\n", $this->interpolate($message, $context)); foreach ($lines as $line) { - $this->_write($priority, $this->formatter->format($level, $line, $context)); + $this->write($priority, $this->formatter->format($level, $line, $context)); } } @@ -127,7 +127,7 @@ public function log($level, Stringable|string $message, array $context = []): vo * @param int $facility the stream or facility to log to * @return void */ - protected function _open(string $ident, int $options, int $facility): void + protected function open(string $ident, int $options, int $facility): void { openlog($ident, $options, $facility); } @@ -140,7 +140,7 @@ protected function _open(string $ident, int $options, int $facility): void * @param string $message Message to log. * @return bool */ - protected function _write(int $priority, string $message): bool + protected function write(int $priority, string $message): bool { return syslog($priority, $message); } diff --git a/Formatter/AbstractFormatter.php b/Formatter/AbstractFormatter.php index 5629d7800..ff8049764 100644 --- a/Formatter/AbstractFormatter.php +++ b/Formatter/AbstractFormatter.php @@ -27,7 +27,7 @@ abstract class AbstractFormatter * * @var array */ - protected array $_defaultConfig = [ + protected array $defaultConfig = [ ]; /** diff --git a/Formatter/DefaultFormatter.php b/Formatter/DefaultFormatter.php index 75bc044f1..56553a027 100644 --- a/Formatter/DefaultFormatter.php +++ b/Formatter/DefaultFormatter.php @@ -25,7 +25,7 @@ class DefaultFormatter extends AbstractFormatter * * @var array */ - protected array $_defaultConfig = [ + protected array $defaultConfig = [ 'dateFormat' => 'Y-m-d H:i:s', 'includeTags' => false, 'includeDate' => true, @@ -36,12 +36,12 @@ class DefaultFormatter extends AbstractFormatter */ public function format($level, string $message, array $context = []): string { - if ($this->_config['includeDate']) { - $message = sprintf('%s %s: %s', (new DateTime())->format($this->_config['dateFormat']), $level, $message); + if ($this->config['includeDate']) { + $message = sprintf('%s %s: %s', new DateTime()->format($this->config['dateFormat']), $level, $message); } else { $message = sprintf('%s: %s', $level, $message); } - if ($this->_config['includeTags']) { + if ($this->config['includeTags']) { return sprintf('<%s>%s', $level, $message, $level); } diff --git a/Formatter/JsonFormatter.php b/Formatter/JsonFormatter.php index 55e322ec7..24ca85d33 100644 --- a/Formatter/JsonFormatter.php +++ b/Formatter/JsonFormatter.php @@ -23,7 +23,7 @@ class JsonFormatter extends AbstractFormatter * * @var array */ - protected array $_defaultConfig = [ + protected array $defaultConfig = [ 'dateFormat' => DATE_ATOM, 'flags' => JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, 'appendNewline' => true, @@ -34,9 +34,9 @@ class JsonFormatter extends AbstractFormatter */ public function format($level, string $message, array $context = []): string { - $log = ['date' => date($this->_config['dateFormat']), 'level' => (string)$level, 'message' => $message]; - $json = json_encode($log, JSON_THROW_ON_ERROR | $this->_config['flags']); + $log = ['date' => date($this->config['dateFormat']), 'level' => (string)$level, 'message' => $message]; + $json = json_encode($log, JSON_THROW_ON_ERROR | $this->config['flags']); - return $this->_config['appendNewline'] ? $json . "\n" : $json; + return $this->config['appendNewline'] ? $json . "\n" : $json; } } diff --git a/Log.php b/Log.php index ffecaf2b5..97c765c7a 100644 --- a/Log.php +++ b/Log.php @@ -17,6 +17,9 @@ use Cake\Core\StaticConfigTrait; use Cake\Log\Engine\BaseLog; +use Cake\Log\Engine\ConsoleLog; +use Cake\Log\Engine\FileLog; +use Cake\Log\Engine\SyslogLog; use Closure; use InvalidArgumentException; use Psr\Log\LoggerInterface; @@ -112,37 +115,26 @@ class Log setConfig as protected _setConfig; } - /** - * An array mapping url schemes to fully qualified Log engine class names - * - * @var array - */ - protected static array $_dsnClassMap = [ - 'console' => Engine\ConsoleLog::class, - 'file' => Engine\FileLog::class, - 'syslog' => Engine\SyslogLog::class, - ]; - /** * Internal flag for tracking whether configuration has been changed. * * @var bool */ - protected static bool $_dirtyConfig = false; + protected static bool $dirtyConfig = false; /** * LogEngineRegistry class * * @var \Cake\Log\LogEngineRegistry */ - protected static LogEngineRegistry $_registry; + protected static LogEngineRegistry $registry; /** * Handled log levels * * @var array */ - protected static array $_levels = [ + protected static array $levels = [ 'emergency', 'alert', 'critical', @@ -159,7 +151,7 @@ class Log * * @var array */ - protected static array $_levelMap = [ + protected static array $levelMap = [ 'emergency' => LOG_EMERG, 'alert' => LOG_ALERT, 'critical' => LOG_CRIT, @@ -170,6 +162,20 @@ class Log 'debug' => LOG_DEBUG, ]; + /** + * Returns the default DSN class map. + * + * @return array + */ + protected static function initDsnClassMap(): array + { + return [ + 'console' => ConsoleLog::class, + 'file' => FileLog::class, + 'syslog' => SyslogLog::class, + ]; + } + /** * Creates registry if doesn't exist and creates all defined logging * adapters if config isn't loaded. @@ -178,21 +184,21 @@ class Log */ protected static function getRegistry(): LogEngineRegistry { - static::$_registry ??= new LogEngineRegistry(); + static::$registry ??= new LogEngineRegistry(); - if (static::$_dirtyConfig) { - foreach (static::$_config as $name => $properties) { + if (static::$dirtyConfig) { + foreach (static::$config as $name => $properties) { if (isset($properties['engine'])) { $properties['className'] = $properties['engine']; } - if (!static::$_registry->has((string)$name)) { - static::$_registry->load((string)$name, $properties); + if (!static::$registry->has((string)$name)) { + static::$registry->load((string)$name, $properties); } } } - static::$_dirtyConfig = false; + static::$dirtyConfig = false; - return static::$_registry; + return static::$registry; } /** @@ -207,11 +213,11 @@ protected static function getRegistry(): LogEngineRegistry */ public static function reset(): void { - if (isset(static::$_registry)) { - static::$_registry->reset(); + if (isset(static::$registry)) { + static::$registry->reset(); } - static::$_config = []; - static::$_dirtyConfig = true; + static::$config = []; + static::$dirtyConfig = true; } /** @@ -224,7 +230,7 @@ public static function reset(): void */ public static function levels(): array { - return static::$_levels; + return static::$levels; } /** @@ -270,7 +276,7 @@ public static function levels(): array public static function setConfig(array|string $key, LoggerInterface|Closure|array|null $config = null): void { static::_setConfig($key, $config); - static::$_dirtyConfig = true; + static::$dirtyConfig = true; } /** @@ -345,11 +351,11 @@ public static function engine(string $name): ?LoggerInterface */ public static function write(string|int $level, Stringable|string $message, array|string $context = []): bool { - if (is_int($level) && in_array($level, static::$_levelMap, true)) { - $level = array_search($level, static::$_levelMap, true); + if (is_int($level) && in_array($level, static::$levelMap, true)) { + $level = array_search($level, static::$levelMap, true); } - if (!in_array($level, static::$_levels, true)) { + if (!in_array($level, static::$levels, true)) { throw new InvalidArgumentException(sprintf('Invalid log level `%s`', $level)); } diff --git a/LogEngineRegistry.php b/LogEngineRegistry.php index 570badf42..3c25fb82d 100644 --- a/LogEngineRegistry.php +++ b/LogEngineRegistry.php @@ -36,7 +36,7 @@ class LogEngineRegistry extends ObjectRegistry * @param string $class Partial classname to resolve. * @return class-string<\Psr\Log\LoggerInterface>|null Either the correct class name or null. */ - protected function _resolveClassName(string $class): ?string + protected function resolveClassName(string $class): ?string { /** @var class-string<\Psr\Log\LoggerInterface>|null */ return App::className($class, 'Log/Engine', 'Log'); @@ -52,7 +52,7 @@ protected function _resolveClassName(string $class): ?string * @return void * @throws \Cake\Core\Exception\CakeException */ - protected function _throwMissingClassError(string $class, ?string $plugin): void + protected function throwMissingClassError(string $class, ?string $plugin): void { throw new CakeException(sprintf('Could not load class `%s`.', $class)); } @@ -67,7 +67,7 @@ protected function _throwMissingClassError(string $class, ?string $plugin): void * @param array $config An array of settings to use for the logger. * @return \Psr\Log\LoggerInterface The constructed logger class. */ - protected function _create(callable|object|string $class, string $alias, array $config): LoggerInterface + protected function create(callable|object|string $class, string $alias, array $config): LoggerInterface { if (is_string($class)) { /** @var class-string<\Psr\Log\LoggerInterface> $class */ @@ -87,9 +87,9 @@ protected function _create(callable|object|string $class, string $alias, array $ * @param string $name The logger name. * @return $this */ - public function unload(string $name) + public function unload(string $name): static { - unset($this->_loaded[$name]); + unset($this->loaded[$name]); return $this; } diff --git a/composer.json b/composer.json index 9fb156838..cf769242f 100644 --- a/composer.json +++ b/composer.json @@ -23,8 +23,8 @@ "source": "https://github.com/cakephp/log" }, "require": { - "php": ">=8.2", - "cakephp/core": "^5.4.0", + "php": ">=8.4", + "cakephp/core": "6.0.*@dev", "psr/log": "^3.0" }, "autoload": { @@ -39,7 +39,7 @@ "prefer-stable": true, "extra": { "branch-alias": { - "dev-5.next": "5.5.x-dev" + "6.x-dev": "6.0.x-dev" } } }