Load configuration options from a file or a string
public static fileConfig ( string $resource ) | ||
$resource | string | Path to config file or string or array |
/** * Configure Cascaded Monolog logger and use it. * * @param $configFile * @param string $loggerName */ private function _initLoggerCascade($configFile, $loggerName) { $err = ''; try { $fs = $this->_obm->get(Filesystem::class); if ($fs->isAbsolutePath($configFile)) { $fileName = $configFile; } else { $fileName = BP . '/' . $configFile; } $realPath = realpath($fileName); if ($realPath) { Cascade::fileConfig($realPath); $this->_logger = Cascade::getLogger($loggerName); } else { $err = "Cannot open logging configuration file '{$fileName}'. Default Magento logger is used."; } } catch (\Exception $e) { $err = $e->getMessage(); } finally { if (is_null($this->_logger)) { $this->_logger = $this->_obm->get(\Magento\Framework\Logger\Monolog::class); $this->warning($err); } } }
/** * Create an object * * @param ContainerInterface $container * @param string $requestedName * @param null|array $options * @return object * @throws ServiceNotFoundException if unable to resolve the service. * @throws ServiceNotCreatedException if an exception is raised when * creating a service. * @throws ContainerException if any other error occurs */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $config = $container->has('config') ? $container->get('config') : []; Cascade::fileConfig(isset($config['logger']) ? $config['logger'] : ['loggers' => []]); // Compose requested logger name $loggerName = isset($options) & isset($options['logger_name']) ? $options['logger_name'] : 'Logger'; $nameParts = explode('_', $requestedName); if (count($nameParts) > 1) { $loggerName = $nameParts[1]; } return Cascade::getLogger($loggerName); }
/** * Load a configuration for the loggers from `.settings.php` or `.settings_extra.php`. * * @param bool $force Load even if the configuration has already been loaded. * * @return bool */ public static function loadConfiguration($force = false) { if ($force === false && static::$isConfigurationLoaded === true) { return true; } if (class_exists('\\Bitrix\\Main\\Config\\Configuration')) { $config = Configuration::getInstance()->get('monolog'); if (is_array($config) && !empty($config)) { Cascade::fileConfig($config); return true; } } return false; }
public function setLogFromFile($file) { Cascade::fileConfig($file); }
<?php /** * @link https://github.com/bitrix-expert/monolog-adapter * @copyright Copyright © 2015 Nik Samokhvalov * @license MIT */ use Bitrix\Main\Config\Configuration; use Cascade\Cascade; if (class_exists('\\Bitrix\\Main\\Config\\Configuration')) { $config = Configuration::getInstance()->get('monolog'); if (is_array($config) && !empty($config)) { Cascade::fileConfig($config); } }
<?php require_once realpath(__DIR__ . '/../vendor/autoload.php'); use Cascade\Cascade; $loggerConfigFile = realpath(__DIR__ . '/logger_config.yml'); // you can use json too! // $loggerConfigFile = realpath(__DIR__.'/logger_config.json'); Cascade::fileConfig($loggerConfigFile); Cascade::getLogger('loggerA')->info('Well, that works!'); Cascade::getLogger('loggerB')->error('Maybe not...'); // This should log into 2 different log files depending on the level: 'example_info.log' and 'example_error.log'
/** * Configure Cascaded Monolog logger and use it. * * @param $configFile */ private function _initLoggerCascade($configFile) { $err = ''; try { $fs = $this->getFilesystem(); if ($fs->isAbsolutePath($configFile)) { $fileName = $configFile; } else { $fileName = BP . '/' . $configFile; } $realPath = realpath($fileName); if ($realPath) { /* check configs registry and load config if not loaded before */ if (!isset(self::$_cascadeConfigs[$realPath])) { Cascade::fileConfig($realPath); self::$_cascadeConfigs[$realPath] = true; } $this->_isMonologUsed = true; } else { $err = "Cannot open logging configuration file '{$fileName}'. Default Magento logger is used."; } } catch (\Exception $e) { $err = $e->getMessage(); $err .= " Default Magento logger is used."; } finally { if (!$this->_isMonologUsed) { $this->_initLoggerMagento(); $this->warning($err); } } }
public function testFileConfig() { $options = Fixtures::getPhpArrayConfig(); Cascade::fileConfig($options); $this->assertInstanceOf('Cascade\\Config', Cascade::getConfig()); }