3 namespace Drupal\Core\Logger;
5 use Drupal\Core\Session\AccountInterface;
6 use Psr\Log\LoggerInterface;
7 use Psr\Log\LoggerTrait;
9 use Symfony\Component\HttpFoundation\RequestStack;
12 * Defines a logger channel that most implementations will use.
14 class LoggerChannel implements LoggerChannelInterface {
18 * Maximum call depth to self::log() for a single log message.
20 * It's very easy for logging channel code to call out to other library code
21 * that will create log messages. In that case, we will recurse back in to
22 * LoggerChannel::log() multiple times while processing a single originating
23 * message. To prevent infinite recursion, we track the call depth and bail
24 * out at LoggerChannel::MAX_CALL_DEPTH iterations.
28 const MAX_CALL_DEPTH = 5;
31 * Number of times LoggerChannel::log() has been called for a single message.
35 protected $callDepth = 0;
38 * The name of the channel of this logger instance.
45 * Map of PSR3 log constants to RFC 5424 log constants.
49 protected $levelTranslation = [
50 LogLevel::EMERGENCY => RfcLogLevel::EMERGENCY,
51 LogLevel::ALERT => RfcLogLevel::ALERT,
52 LogLevel::CRITICAL => RfcLogLevel::CRITICAL,
53 LogLevel::ERROR => RfcLogLevel::ERROR,
54 LogLevel::WARNING => RfcLogLevel::WARNING,
55 LogLevel::NOTICE => RfcLogLevel::NOTICE,
56 LogLevel::INFO => RfcLogLevel::INFO,
57 LogLevel::DEBUG => RfcLogLevel::DEBUG,
61 * An array of arrays of \Psr\Log\LoggerInterface keyed by priority.
65 protected $loggers = [];
68 * The request stack object.
70 * @var \Symfony\Component\HttpFoundation\RequestStack
72 protected $requestStack;
75 * The current user object.
77 * @var \Drupal\Core\Session\AccountInterface
79 protected $currentUser;
82 * Constructs a LoggerChannel object
84 * @param string $channel
85 * The channel name for this instance.
87 public function __construct($channel) {
88 $this->channel = $channel;
94 public function log($level, $message, array $context = []) {
95 if ($this->callDepth == self::MAX_CALL_DEPTH) {
100 // Merge in defaults.
102 'channel' => $this->channel,
109 'timestamp' => time(),
111 // Some context values are only available when in a request context.
112 if ($this->requestStack && $request = $this->requestStack->getCurrentRequest()) {
113 $context['request_uri'] = $request->getUri();
114 $context['referer'] = $request->headers->get('Referer', '');
115 $context['ip'] = $request->getClientIP();
117 if ($this->currentUser) {
118 $context['user'] = $this->currentUser;
119 $context['uid'] = $this->currentUser->id();
122 catch (\Exception $e) {
123 // An exception might be thrown if the database connection is not
124 // available or due to another unexpected reason. It is more important
125 // to log the error that we already have so any additional exceptions
130 if (is_string($level)) {
131 // Convert to integer equivalent for consistency with RFC 5424.
132 $level = $this->levelTranslation[$level];
134 // Call all available loggers.
135 foreach ($this->sortLoggers() as $logger) {
136 $logger->log($level, $message, $context);
145 public function setRequestStack(RequestStack $requestStack = NULL) {
146 $this->requestStack = $requestStack;
152 public function setCurrentUser(AccountInterface $current_user = NULL) {
153 $this->currentUser = $current_user;
159 public function setLoggers(array $loggers) {
160 $this->loggers = $loggers;
166 public function addLogger(LoggerInterface $logger, $priority = 0) {
167 $this->loggers[$priority][] = $logger;
171 * Sorts loggers according to priority.
174 * An array of sorted loggers by priority.
176 protected function sortLoggers() {
178 krsort($this->loggers);
180 foreach ($this->loggers as $loggers) {
181 $sorted = array_merge($sorted, $loggers);