Abstract.php 9.91 KB
Newer Older
Phạm Văn Đoan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
<?php
/*
 * Copyright 2014 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

if (!class_exists('Google_Client')) {
  require_once dirname(__FILE__) . '/../autoload.php';
}

/**
 * Abstract logging class based on the PSR-3 standard.
 *
 * NOTE: We don't implement `Psr\Log\LoggerInterface` because we need to
 * maintain PHP 5.2 support.
 *
 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
 */
abstract class Google_Logger_Abstract
{
  /**
   * Default log format
   */
  const DEFAULT_LOG_FORMAT = "[%datetime%] %level%: %message% %context%\n";
  /**
   * Default date format
   *
   * Example: 16/Nov/2014:03:26:16 -0500
   */
  const DEFAULT_DATE_FORMAT = 'd/M/Y:H:i:s O';

  /**
   * System is unusable
   */
  const EMERGENCY = 'emergency';
  /**
   * Action must be taken immediately
   *
   * Example: Entire website down, database unavailable, etc. This should
   * trigger the SMS alerts and wake you up.
   */
  const ALERT = 'alert';
  /**
   * Critical conditions
   *
   * Example: Application component unavailable, unexpected exception.
   */
  const CRITICAL = 'critical';
  /**
   * Runtime errors that do not require immediate action but should typically
   * be logged and monitored.
   */
  const ERROR = 'error';
  /**
   * Exceptional occurrences that are not errors.
   *
   * Example: Use of deprecated APIs, poor use of an API, undesirable things
   * that are not necessarily wrong.
   */
  const WARNING = 'warning';
  /**
   * Normal but significant events.
   */
  const NOTICE = 'notice';
  /**
   * Interesting events.
   *
   * Example: User logs in, SQL logs.
   */
  const INFO = 'info';
  /**
   * Detailed debug information.
   */
  const DEBUG = 'debug';

  /**
   * @var array $levels Logging levels
   */
  protected static $levels = array(
      self::EMERGENCY  => 600,
      self::ALERT => 550,
      self::CRITICAL => 500,
      self::ERROR => 400,
      self::WARNING => 300,
      self::NOTICE => 250,
      self::INFO => 200,
      self::DEBUG => 100,
  );

  /**
   * @var integer $level The minimum logging level
   */
  protected $level = self::DEBUG;

  /**
   * @var string $logFormat The current log format
   */
  protected $logFormat = self::DEFAULT_LOG_FORMAT;
  /**
   * @var string $dateFormat The current date format
   */
  protected $dateFormat = self::DEFAULT_DATE_FORMAT;

  /**
   * @var boolean $allowNewLines If newlines are allowed
   */
  protected $allowNewLines = false;

  /**
   * @param Google_Client $client  The current Google client
   */
  public function __construct(Google_Client $client)
  {
    $this->setLevel(
        $client->getClassConfig('Google_Logger_Abstract', 'level')
    );

    $format = $client->getClassConfig('Google_Logger_Abstract', 'log_format');
    $this->logFormat = $format ? $format : self::DEFAULT_LOG_FORMAT;

    $format = $client->getClassConfig('Google_Logger_Abstract', 'date_format');
    $this->dateFormat = $format ? $format : self::DEFAULT_DATE_FORMAT;

    $this->allowNewLines = (bool) $client->getClassConfig(
        'Google_Logger_Abstract',
        'allow_newlines'
    );
  }

  /**
   * Sets the minimum logging level that this logger handles.
   *
   * @param integer $level
   */
  public function setLevel($level)
  {
    $this->level = $this->normalizeLevel($level);
  }

  /**
   * Checks if the logger should handle messages at the provided level.
   *
   * @param  integer $level
   * @return boolean
   */
  public function shouldHandle($level)
  {
    return $this->normalizeLevel($level) >= $this->level;
  }

  /**
   * System is unusable.
   *
   * @param string $message The log message
   * @param array $context  The log context
   */
  public function emergency($message, array $context = array())
  {
    $this->log(self::EMERGENCY, $message, $context);
  }

  /**
   * Action must be taken immediately.
   *
   * Example: Entire website down, database unavailable, etc. This should
   * trigger the SMS alerts and wake you up.
   *
   * @param string $message The log message
   * @param array $context  The log context
   */
  public function alert($message, array $context = array())
  {
    $this->log(self::ALERT, $message, $context);
  }

  /**
   * Critical conditions.
   *
   * Example: Application component unavailable, unexpected exception.
   *
   * @param string $message The log message
   * @param array $context  The log context
   */
  public function critical($message, array $context = array())
  {
    $this->log(self::CRITICAL, $message, $context);
  }

  /**
   * Runtime errors that do not require immediate action but should typically
   * be logged and monitored.
   *
   * @param string $message The log message
   * @param array $context  The log context
   */
  public function error($message, array $context = array())
  {
    $this->log(self::ERROR, $message, $context);
  }

  /**
   * Exceptional occurrences that are not errors.
   *
   * Example: Use of deprecated APIs, poor use of an API, undesirable things
   * that are not necessarily wrong.
   *
   * @param string $message The log message
   * @param array $context  The log context
   */
  public function warning($message, array $context = array())
  {
    $this->log(self::WARNING, $message, $context);
  }

  /**
   * Normal but significant events.
   *
   * @param string $message The log message
   * @param array $context  The log context
   */
  public function notice($message, array $context = array())
  {
    $this->log(self::NOTICE, $message, $context);
  }

  /**
   * Interesting events.
   *
   * Example: User logs in, SQL logs.
   *
   * @param string $message The log message
   * @param array $context  The log context
   */
  public function info($message, array $context = array())
  {
    $this->log(self::INFO, $message, $context);
  }

  /**
   * Detailed debug information.
   *
   * @param string $message The log message
   * @param array $context  The log context
   */
  public function debug($message, array $context = array())
  {
    $this->log(self::DEBUG, $message, $context);
  }

  /**
   * Logs with an arbitrary level.
   *
   * @param mixed $level    The log level
   * @param string $message The log message
   * @param array $context  The log context
   */
  public function log($level, $message, array $context = array())
  {
    if (!$this->shouldHandle($level)) {
      return false;
    }

    $levelName = is_int($level) ? array_search($level, self::$levels) : $level;
    $message = $this->interpolate(
        array(
            'message' => $message,
            'context' => $context,
            'level' => strtoupper($levelName),
            'datetime' => new DateTime(),
        )
    );

    $this->write($message);
  }

  /**
   * Interpolates log variables into the defined log format.
   *
   * @param  array $variables The log variables.
   * @return string
   */
  protected function interpolate(array $variables = array())
  {
    $template = $this->logFormat;

    if (!$variables['context']) {
      $template = str_replace('%context%', '', $template);
      unset($variables['context']);
    } else {
      $this->reverseJsonInContext($variables['context']);
    }

    foreach ($variables as $key => $value) {
      if (strpos($template, '%'. $key .'%') !== false) {
        $template = str_replace(
            '%' . $key . '%',
            $this->export($value),
            $template
        );
      }
    }

    return $template;
  }

  /**
   * Reverses JSON encoded PHP arrays and objects so that they log better.
   *
   * @param array $context The log context
   */
  protected function reverseJsonInContext(array &$context)
  {
    if (!$context) {
      return;
    }

    foreach ($context as $key => $val) {
      if (!$val || !is_string($val) || !($val[0] == '{' || $val[0] == '[')) {
        continue;
      }

      $json = @json_decode($val);
      if (is_object($json) || is_array($json)) {
        $context[$key] = $json;
      }
    }
  }

  /**
   * Exports a PHP value for logging to a string.
   *
   * @param mixed $value The value to
   */
  protected function export($value)
  {
    if (is_string($value)) {
      if ($this->allowNewLines) {
        return $value;
      }

      return preg_replace('/[\r\n]+/', ' ', $value);
    }

    if (is_resource($value)) {
      return sprintf(
          'resource(%d) of type (%s)',
          $value,
          get_resource_type($value)
      );
    }

    if ($value instanceof DateTime) {
      return $value->format($this->dateFormat);
    }

    if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
      $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

      if ($this->allowNewLines) {
        $options |= JSON_PRETTY_PRINT;
      }

      return @json_encode($value, $options);
    }

    return str_replace('\\/', '/', @json_encode($value));
  }

  /**
   * Converts a given log level to the integer form.
   *
   * @param  mixed $level   The logging level
   * @return integer $level The normalized level
   * @throws Google_Logger_Exception If $level is invalid
   */
  protected function normalizeLevel($level)
  {
    if (is_int($level) && array_search($level, self::$levels) !== false) {
      return $level;
    }

    if (is_string($level) && isset(self::$levels[$level])) {
      return self::$levels[$level];
    }

    throw new Google_Logger_Exception(
        sprintf("Unknown LogLevel: '%s'", $level)
    );
  }

  /**
   * Writes a message to the current log implementation.
   *
   * @param string $message The message
   */
  abstract protected function write($message);
}