我正在使用Monolog创建我的应用程序的日志记录系统。在核心应用程序文件中,创建新的 Monolog 对象后,我需要在日志文件中选择要打印的日志级别。我想使用一个全局常量LOG_LEVEL
,它可以是“DEBUG”、“INFO”等。我需要 Monolog 类将其值视为类常量。
// content of config.php
// Here I declare the constants in a separate file called 'config.php'
define("LOG_FILE", "patch/to/my/log.log");
define("LOG_LEVEL", "ERROR");
// content of app.php
require 'config.php';
require 'vendor/autoload.php';
$container['logger'] = function($c) {
$logger = new \Monolog\Logger('logger');
error_log('log level ' . LOG_LEVEL); // prints 'log level ERROR'
$fileHandler = new \Monolog\Handler\StreamHandler(LOG_FILE, $logger::LOG_LEVEL); // here I get the error 'Undefined class constant LOG_LEVEL'
//the normal syntax would be '$logger::ERROR' in this case and that works fine
$logger->pushHandler($fileHandler);
return $logger;
};
我需要'LOG_LEVEL'常量被独白类用作'ERROR',而不是'LOG_LEVEL'。我在这里做错了什么,现在已经搜索了几个小时没有任何运气的答案。