Now I'm developing a project on yii2 and the task has arisen to add, in addition to the levels available in the logger, add more fatal and critical.
Actually, I redefined logger.php itself and began to redefine Yii.php, but it pulled a lot of dependencies inside vendor and errors began to appear like:
Fatal error: Uncaught Error: Class 'Yii' not found in /app/vendor/yiisoft/yii2/base/Module.php:183 Stack trace: #0 /app/components/Application.php(14): yii\base\Module::setInstance(Object(app\components\Application)) #1 /app/web/index.php(16): app\components\Application->__construct(Array) #2 {main} thrown in /app/vendor/yiisoft/yii2/base/Module.php on line 183
logger
class Logger extends \yii\log\Logger
{
/**
* Critical message level. An tracing message is one that reveals the code execution flow.
*/
const LEVEL_CRITICAL = 0x12;
/**
* Fatal message level. An tracing message is one that reveals the code execution flow.
*/
const LEVEL_FATAL = 0x16;
/**
* Returns the text display of the specified level.
* @param int $level the message level, e.g. [[LEVEL_ERROR]], [[LEVEL_WARNING]].
* @return string the text display of the level
*/
public static function getLevelName($level)
{
static $levels = [
self::LEVEL_ERROR => 'error',
self::LEVEL_WARNING => 'warning',
self::LEVEL_INFO => 'info',
self::LEVEL_TRACE => 'trace',
self::LEVEL_CRITICAL => 'critical',
self::LEVEL_FATAL => 'fatal',
self::LEVEL_PROFILE_BEGIN => 'profile begin',
self::LEVEL_PROFILE_END => 'profile end',
self::LEVEL_PROFILE => 'profile',
];
return isset($levels[$level]) ? $levels[$level] : 'unknown';
}
}
yii.php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/components/Yii.php';
$config = require __DIR__ . '/config/console.php';
$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);
Yii.php
<?php
namespace app\components;
use app\components\Logger;
use yii\di\Container;
class Yii extends \yii\BaseYii
{
private static $_logger;
/**
* @return Logger message logger
*/
public static function getLogger()
{
if (self::$_logger !== null) {
return self::$_logger;
}
return self::$_logger = static::createObject('app\components\Logger');
}
/**
* Sets the logger object.
* @param Logger $logger the logger object.
*/
public static function setLogger($logger)
{
self::$_logger = $logger;
}
public static function critical($message, $category = 'application')
{
static::getLogger()->log($message, Logger::LEVEL_CRITICAL, $category);
}
public static function fatal($message, $category = 'application')
{
static::getLogger()->log($message, Logger::LEVEL_FATAL, $category);
}
}
spl_autoload_register(['app\components\Yii', 'autoload'], true, true);
Yii::$classMap = require __DIR__ . '/../vendor/yiisoft/yii2/classes.php';
Yii::$container = new Container();
Is it possible to make it somehow simpler so as not to redefine the path for each component using it?