Skip to content

Use NullLogger when available #14682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions src/Symfony/Bridge/Propel1/Logger/PropelLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Propel1\Logger;

use Psr\Log\NullLogger;
use Symfony\Component\Stopwatch\Stopwatch;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -47,7 +48,7 @@ class PropelLogger
*/
public function __construct(LoggerInterface $logger = null, Stopwatch $stopwatch = null)
{
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
$this->queries = array();
$this->stopwatch = $stopwatch;
$this->isPrepared = false;
Expand All @@ -60,9 +61,7 @@ public function __construct(LoggerInterface $logger = null, Stopwatch $stopwatch
*/
public function alert($message)
{
if (null !== $this->logger) {
$this->logger->alert($message);
}
$this->logger->alert($message);
}

/**
Expand All @@ -72,9 +71,7 @@ public function alert($message)
*/
public function crit($message)
{
if (null !== $this->logger) {
$this->logger->critical($message);
}
$this->logger->critical($message);
}

/**
Expand All @@ -84,9 +81,7 @@ public function crit($message)
*/
public function err($message)
{
if (null !== $this->logger) {
$this->logger->error($message);
}
$this->logger->error($message);
}

/**
Expand All @@ -96,9 +91,7 @@ public function err($message)
*/
public function warning($message)
{
if (null !== $this->logger) {
$this->logger->warning($message);
}
$this->logger->warning($message);
}

/**
Expand All @@ -108,9 +101,7 @@ public function warning($message)
*/
public function notice($message)
{
if (null !== $this->logger) {
$this->logger->notice($message);
}
$this->logger->notice($message);
}

/**
Expand All @@ -120,9 +111,7 @@ public function notice($message)
*/
public function info($message)
{
if (null !== $this->logger) {
$this->logger->info($message);
}
$this->logger->info($message);
}

/**
Expand Down Expand Up @@ -152,9 +141,7 @@ public function debug($message)

if ($add) {
$this->queries[] = $message;
if (null !== $this->logger) {
$this->logger->debug($message);
}
$this->logger->debug($message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Routing;

use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Component\Config\Exception\FileLoaderLoadException;
use Symfony\Component\Config\Loader\DelegatingLoader as BaseDelegatingLoader;
Expand Down Expand Up @@ -41,7 +42,7 @@ class DelegatingLoader extends BaseDelegatingLoader
public function __construct(ControllerNameParser $parser, LoggerInterface $logger = null, LoaderResolverInterface $resolver)
{
$this->parser = $parser;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();

parent::__construct($resolver);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Symfony/Bundle/FrameworkBundle/Templating/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Templating;

use Psr\Log\NullLogger;
use Symfony\Component\Templating\DebuggerInterface;
use Psr\Log\LoggerInterface;

Expand All @@ -30,7 +31,7 @@ class Debugger implements DebuggerInterface
*/
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand All @@ -40,8 +41,6 @@ public function __construct(LoggerInterface $logger = null)
*/
public function log($message)
{
if (null !== $this->logger) {
$this->logger->debug($message);
}
$this->logger->debug($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Controller;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\Request;

/**
Expand All @@ -36,7 +37,7 @@ class ControllerResolver implements ControllerResolverInterface
*/
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand All @@ -57,9 +58,7 @@ public function __construct(LoggerInterface $logger = null)
public function getController(Request $request)
{
if (!$controller = $request->attributes->get('_controller')) {
if (null !== $this->logger) {
$this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing');
}
$this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing');

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpKernel\Debug;

use Psr\Log\NullLogger;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\HttpKernel\KernelEvents;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function __construct(EventDispatcherInterface $dispatcher, Stopwatch $sto
{
$this->dispatcher = $dispatcher;
$this->stopwatch = $stopwatch;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
$this->called = array();
$this->wrappedListeners = array();
$this->firstCalledEvent = array();
Expand Down Expand Up @@ -175,9 +176,7 @@ public function getNotCalledListeners()
try {
$allListeners = $this->getListeners();
} catch (\Exception $e) {
if (null !== $this->logger) {
$this->logger->info(sprintf('An exception was thrown while getting the uncalled listeners (%s)', $e->getMessage()), array('exception' => $e));
}
$this->logger->info(sprintf('An exception was thrown while getting the uncalled listeners (%s)', $e->getMessage()), array('exception' => $e));

// unable to retrieve the uncalled listeners
return array();
Expand Down Expand Up @@ -218,7 +217,7 @@ public function __call($method, $arguments)
*/
public function logSkippedListeners($eventName, $eventId, Event $event, $listener)
{
if (null === $this->logger) {
if ($this->logger instanceof NullLogger) {
return;
}

Expand Down Expand Up @@ -261,9 +260,7 @@ public function preListenerCall($eventName, $eventId, $listener)

$info = $this->getListenerInfo($listener, $eventId, $eventName);

if (null !== $this->logger) {
$this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
}
$this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));

$this->called[$eventName.'.'.$info['pretty']] = $info;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\EventListener;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
Expand All @@ -31,14 +32,12 @@ class ErrorsLoggerListener implements EventSubscriberInterface
public function __construct($channel, LoggerInterface $logger = null)
{
$this->channel = $channel;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

public function injectLogger()
{
if (null !== $this->logger) {
ErrorHandler::setLogger($this->logger, $this->channel);
}
ErrorHandler::setLogger($this->logger, $this->channel);
}

public static function getSubscribedEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\EventListener;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Expand All @@ -33,7 +34,7 @@ class ExceptionListener implements EventSubscriberInterface
public function __construct($controller, LoggerInterface $logger = null)
{
$this->controller = $controller;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

public function onKernelException(GetResponseForExceptionEvent $event)
Expand Down Expand Up @@ -97,14 +98,17 @@ protected function logException(\Exception $exception, $message, $original = tru
{
$isCritical = !$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500;
$context = array('exception' => $exception);
if (null !== $this->logger) {
if ($isCritical) {
$this->logger->critical($message, $context);
} else {
$this->logger->error($message, $context);
}
} elseif (!$original || $isCritical) {

if ($this->logger instanceof NullLogger && (!$original || $isCritical)) {
error_log($message);

return;
}

if ($isCritical) {
$this->logger->critical($message, $context);
} else {
$this->logger->error($message, $context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\EventListener;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function __construct($matcher, RequestContext $context = null, LoggerInte

$this->matcher = $matcher;
$this->context = $context ?: $matcher->getContext();
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand Down Expand Up @@ -102,9 +103,7 @@ public function onKernelRequest(GetResponseEvent $event)
$parameters = $this->matcher->match($request->getPathInfo());
}

if (null !== $this->logger) {
$this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], $this->parametersToString($parameters)));
}
$this->logger->info(sprintf('Matched route "%s" (parameters: %s)', $parameters['_route'], $this->parametersToString($parameters)));

$request->attributes->add($parameters);
unset($parameters['_route'], $parameters['_controller']);
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/HttpKernel/Profiler/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpKernel\Profiler;

use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
Expand Down Expand Up @@ -52,7 +53,7 @@ class Profiler
public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
{
$this->storage = $storage;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand Down Expand Up @@ -108,7 +109,7 @@ public function loadProfile($token)
*/
public function saveProfile(Profile $profile)
{
if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
if (!($ret = $this->storage->write($profile))) {
$this->logger->warning('Unable to store the profiler information.');
}

Expand Down
Loading