/** * Constructor - initializes this logger with a level and a * default EchoWriter writer (which means all logs are printed on screen) * @param Level $level [optional] the level in which to set the root logger. * defaulted to ALL. */ public function __construct(Level $level = null) { parent::__construct('root'); if ($level == null) { $level = Level::all(); } $this->setLevel($level); }
/** * Tests that we can write using the Console writer. This writer will be set so * that all output is passed to STDOUT so it can be captured using output buffering. */ public function testIfElseStatement() { //clear any previous configuration (as hierarchy is stored between calls) Logger::getHierarchy()->clear(); $logger = Logger::getLogger('test_logger', array('writers' => array('Console' => array('useMinErrorLevel' => false, 'target' => Console::OUTPUT, 'layout' => array('pattern' => '%if ifValue > 4% %trueValue %else% %elseValue %endif%'))), 'extras' => array('local' => array('ifValue' => 3, 'trueValue' => 'It was more than four', 'elseValue' => 'It was less than four')))); ob_start(); $logger->info('Tester Message'); $logs = ob_get_clean(); $this->assertNotEquals('', $logs); }
public function testFile() { //clear any previous configuration (as hierarchy is stored between calls) Logger::getHierarchy()->clear(); $logger = Logger::getLogger('test_logger', array('writers' => array('File' => array('file' => __DIR__ . '/testFile.csv', 'append' => false, 'locking' => true)))); $logger->info('A Tester Message'); $logger->close(); //close this logger instance. //open the csv and determine if there is one row in the CSV (where the log has been written.) if (($handle = fopen(__DIR__ . '/testFile.csv', 'r')) === false) { $this->fail('The CSV file could not be opened, which means it could not be generated'); } //close the handle. fclose($handle); $this->assertEquals(1, $this->getLines(__DIR__ . '/testFile.csv')); //remove the tester csv file. if (file_exists(__DIR__ . '/testFile.csv')) { unlink(__DIR__ . '/testFile.csv'); } }
/** * sets any initial configuration for a logger. * @param Logger $logger the logger to configure. * @param array $config the config to configure the logger with. * @return Logger the configured logger. */ private function setInitialConfiguration(&$logger, $config) { if (isset($config['level'])) { $level = $config['level']; if (!$level instanceof Level) { $level = Level::parseFromString($level); } if ($level instanceof Level) { $logger->setLevel($level); } } if (isset($config['enablePropogation']) && is_bool($config['enablePropogation'])) { $logger->setPropogation($config['enablePropogation']); } //see if any global extras or logger extras have been added to the config. if (isset($config['extras'])) { if (isset($config['extras']['global']) && is_array($config['extras']['global']) && count($config['extras']['global']) > 0) { foreach ($config['extras']['global'] as $key => $value) { Logger::addGlobalExtra($key, $value); } } if (isset($config['extras']['local']) && is_array($config['extras']['local']) && count($config['extras']['local']) > 0) { foreach ($config['extras']['local'] as $key => $value) { $logger->addExtra($key, $value); } } } if (isset($config['writers'])) { //set all of the valid writers. foreach ($config['writers'] as $name => $writerConf) { $className = '\\PHPLog\\Writer\\' . $name; if (class_exists($className)) { $writer = new $className($writerConf); $logger->addWriter($writer); } } } if (isset($config['filters'])) { //set all valid filters. foreach ($config['filters'] as $name => $filterConf) { $className = '\\PHPLog\\Filter\\' . $name; if (class_exists($className)) { $filter = new $className($writerConf); $logger->addFilter($filter); } } } if (isset($config['renderers'])) { foreach ($config['renderers'] as $class => $renderer) { if (!$renderer instanceof RendererInterface) { if (!is_string($renderer)) { return; } if (!class_exists($renderer)) { $renderer = '\\PHPLog\\Renderer\\' . $renderer; if (!class_exists($renderer)) { return; } } $renderer = new $renderer(); } $logger->addRenderer($class, $renderer); } } return $logger; }
/** * gets a system service from this instances logger. * @param string $serviceIndentifer the service required. * @return mixed the service requested. */ public final function getSystemService($serviceIdentifer) { return Logger::getSystemService($this->getLoggerName(), $serviceIdentifer); }
public function testWriteObject() { $object = new \PHPLog\Exception\CompilerException('A Complier Exception', ' %if tester < 3 Hello %else%', 10); //clear any previous configuration (as hierarchy is stored between calls) Logger::getHierarchy()->clear(); $logger = Logger::getLogger('test_logger', array('writers' => array('EchoWriter' => array()))); $logger->addRenderer($object, new CompilerRenderer(), true); ob_start(); $logger->info($object); $message = ob_get_clean(); $this->assertNotEquals('', $message); }