public function testWritingAndFormatting() { $output = $this->getMock('Symfony\\Component\\Console\\Output\\OutputInterface'); $output->expects($this->any())->method('getVerbosity')->will($this->returnValue(OutputInterface::VERBOSITY_DEBUG)); $output->expects($this->once())->method('write')->with('<info>[2013-05-29 16:21:54] app.INFO:</info> My info message ' . "\n"); $handler = new ConsoleHandler(null, false); $handler->setOutput($output); $infoRecord = array('message' => 'My info message', 'context' => array(), 'level' => Logger::INFO, 'level_name' => Logger::getLevelName(Logger::INFO), 'channel' => 'app', 'datetime' => new \DateTime('2013-05-29 16:21:54'), 'extra' => array()); $this->assertTrue($handler->handle($infoRecord), 'The handler finished handling the log as bubble is false.'); }
public function register(Application $app) { $app['console.output'] = $app->share(function () { return new ConsoleOutput(); }); $app['console.input'] = $app->share(function () { return new ArgvInput(); }); $app['logger.console_format'] = "%start_tag%%level_name%:%end_tag% %message%\n"; $app['monolog.handler'] = function () use($app) { $logfile = $app->offsetExists('monolog.console_logfile') ? $app['monolog.console_logfile'] : $app['monolog.logfile']; return new StreamHandler($logfile, $app['monolog.level']); }; $app['logger'] = $app->share($app->extend('logger', function (Logger $logger, \Pimple $app) { $consoleHandler = new ConsoleHandler($app['console.output']); if (!class_exists('Symfony\\Bridge\\Monolog\\Handler\\ConsoleHandler')) { throw new \Exception('ConsoleLoggerServiceProvider requires symfony/monolog-bridge ~2.4.'); } $consoleHandler->setFormatter(new ConsoleFormatter($app['logger.console_format'])); $logger->pushHandler($consoleHandler); return $logger; })); }
/** * Configures and returns the logger instance * * @param OutputInterface $output * @param Configuration $configuration * * @return Logger */ private function configureLogger(OutputInterface $output, Configuration $configuration) { $consoleHandler = new ConsoleHandler($output); $consoleHandler->setFormatter(new ColoredLineFormatter(null, "[%datetime%] %level_name%: %message%\n")); $logger = new Logger(self::COMMAND_NAME); $logger->pushHandler($consoleHandler); $logger->pushProcessor(new PsrLogMessageProcessor()); if ($configuration->getLogPath() !== null) { $fileHandler = new StreamHandler($configuration->getLogPath()); $logger->pushHandler($fileHandler); } return $logger; }
<?php use Interop\Container\ContainerInterface; use Monolog\Logger; use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter; use Symfony\Bridge\Monolog\Handler\ConsoleHandler; use Symfony\Component\Console\Output\OutputInterface; return array('log.handlers' => array(DI\get('Symfony\\Bridge\\Monolog\\Handler\\ConsoleHandler')), 'Symfony\\Bridge\\Monolog\\Handler\\ConsoleHandler' => function (ContainerInterface $c) { // Override the default verbosity map to make it more verbose by default $verbosityMap = array(OutputInterface::VERBOSITY_NORMAL => Logger::INFO, OutputInterface::VERBOSITY_VERBOSE => Logger::DEBUG, OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::DEBUG, OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG); $handler = new ConsoleHandler(null, true, $verbosityMap); $handler->setFormatter(new ConsoleFormatter($c->get('log.console.format'), null, true, true)); return $handler; }, 'log.console.format' => '%start_tag%%level_name% [%datetime%]%end_tag% %message%' . PHP_EOL);