/** * {@inheritDoc} */ public function register(Container $container) { // Append custom settings with missing params from default settings $container['settings']['logger'] = self::mergeWithDefaultSettings($container['settings']['logger']); /** * Add dependency (DI). * * @param Container $c * * @return Logger */ $container['logger'] = function (Container $c) { $settings = $c['settings']['logger']; $loggerFormat = "[%datetime%] %level_name% %message% %context% %extra%\n"; $loggerTimeFormat = "Y-m-d H:i:s"; $loggerTimeZone = new DateTimeZone('Europe/Berlin'); $logger = new Logger($settings['name']); if ($settings['color']) { $logger->pushProcessor(new ConsoleColorProcessor()); } $logger->pushProcessor(new CleanupProcessor($settings['trimPaths'])); $logger->pushProcessor(new IntrospectionProcessor(Logger::WARNING)); $logger->pushProcessor(new ProcessIdProcessor()); $logger->pushProcessor(new PsrLogMessageProcessor()); $logger->setTimezone($loggerTimeZone); $logger->useMicrosecondTimestamps(false); // Using microseconds is buggy (2016-08-04) $formatter = new LineFormatter($loggerFormat, $loggerTimeFormat); $formatter->ignoreEmptyContextAndExtra(true); $defaultHandler = new StreamHandler('php://stdout', $settings['level'], $bubble = false); $defaultHandler->setFormatter($formatter); $logger->pushHandler($defaultHandler); $errorHandler = new StreamHandler('php://stderr', Logger::ERROR, $bubble = false); $errorHandler->setFormatter($formatter); $logger->pushHandler($errorHandler); // Register logger as default PHP error, exception and shutdown handler // Note: Make sure only this handler handles errors (set $callPrevious to false) $errorHandler = ErrorHandler::register($logger, $errorLevelMap = false, $exceptionLevelMap = false); $errorHandler->registerErrorHandler($levelMap = [], $callPrevious = false); $errorHandler->registerExceptionHandler($levelMap = [], $callPrevious = false); return $logger; }; }
/** * Register this plates view provider with a Pimple container * * @param string $name * @param array $settings */ public function __construct($name = 'slim-app', $settings = []) { $this->name = $name; $this->monolog = new Logger($this->name); $this->settings = array_merge($this->settings, $settings); if (null !== $this->settings['timezone']) { if (is_string($this->settings['timezone'])) { $this->settings['timezone'] = new \DateTimeZone($this->settings['timezone']); } Logger::setTimezone($this->settings['timezone']); } $this->monolog->setHandlers($this->settings['handlers']); $levels = array_keys(Logger::getLevels()); if (!in_array(strtoupper($this->settings['level']), $levels)) { $this->settings['level'] = 'debug'; } if ($path = $this->settings['directory']) { if ($path === 'syslog') { $this->useSyslog($this->name, $this->settings['level']); } elseif (is_dir($path)) { $path .= '/' . $this->name; $this->useRotatingFiles($path, $this->settings['level']); } } }
/** @test */ public function testIntegration() { $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build(); try { $client->ping(); } catch (NoNodesAvailableException $e) { $this->markTestSkipped('Skipped due to a missing instance of Elasticsearch'); } try { $client->indices()->delete(['index' => ElasticsearchHandler::DEFAULT_INDEX_NAME]); } catch (Missing404Exception $e) { // Noop } $logger = new Logger('application', [new ElasticsearchHandler($client)]); $timezone = new \DateTimeZone('UTC'); $datetime = DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $timezone)->setTimezone($timezone)->format(DateTime::ISO8601); $logger->setTimezone($timezone); $logger->info('This is a test!'); sleep(1); $results = $client->search(['index' => ElasticsearchHandler::DEFAULT_INDEX_NAME, 'type' => ElasticsearchHandler::DEFAULT_TYPE_NAME, 'body' => ['query' => ['match_all' => new \stdClass()]]]); $this->assertGreaterThan(0, $results['hits']['total']); $expected = ['message' => 'This is a test!', 'context' => [], 'level' => 200, 'level_name' => 'INFO', 'channel' => 'application', 'datetime' => $datetime, 'extra' => []]; $this->assertEquals($expected, $results['hits']['hits'][0]['_source']); }
/** * @dataProvider setTimezoneProvider * @covers Monolog\Logger::setTimezone */ public function testSetTimezone($tz) { Logger::setTimezone($tz); $logger = new Logger('foo'); $handler = new TestHandler(); $logger->pushHandler($handler); $logger->info('test'); list($record) = $handler->getRecords(); $this->assertEquals($tz, $record['datetime']->getTimezone()); }
/** * Set the timezone to be used for the timestamp of log records. * * This is stored globally for all Logger instances * * @param \DateTimeZone $tz Timezone object * @static */ public static function setTimezone($tz) { return \Monolog\Logger::setTimezone($tz); }
// Register a handler that will buffer the event stream and // pass events to the nested handler after closing the request $wmgMonologConfig['handlers'][$bufferedHandler] = array('class' => '\\MediaWiki\\Logger\\Monolog\\BufferHandler', 'args' => array(function () use($handlerName) { return LoggerFactory::getProvider()->getHandler($handlerName); })); } $handlers[$idx] = $bufferedHandler; } } if ($handlers) { // T118057: wrap the collection of handlers in a WhatFailureGroupHandler // to swallow any exceptions that might leak out otherwise $failureGroupHandler = 'failuregroup|' . implode('|', $handlers); if (!isset($wmgMonologConfig['handlers'][$failureGroupHandler])) { $wmgMonologConfig['handlers'][$failureGroupHandler] = array('class' => '\\Monolog\\Handler\\WhatFailureGroupHandler', 'args' => array(function () use($handlers) { $provider = LoggerFactory::getProvider(); return array_map(array($provider, 'getHandler'), $handlers); })); } $wmgMonologConfig['loggers'][$channel] = array('handlers' => array($failureGroupHandler), 'processors' => array_keys($wmgMonologProcessors), 'calls' => $wmgMonologLoggerCalls); } else { // No handlers configured, so use the blackhole route $wmgMonologConfig['loggers'][$channel] = array('handlers' => array('blackhole'), 'calls' => $wmgMonologLoggerCalls); } } $wgMWLoggerDefaultSpi = array('class' => '\\MediaWiki\\Logger\\MonologSpi', 'args' => array($wmgMonologConfig)); // Bug: T99581 - force logger timezone to UTC // Guard condition needed for Jenkins; class from mediawiki/vendor if (method_exists('\\Monolog\\Logger', 'setTimezone')) { \Monolog\Logger::setTimezone(new DateTimeZone('UTC')); }