Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: bejoy balan
Added: Sep 24, 2018 7:28 AM
Views: 4
Tags: log m2 sql
vendor/magento/framework/DB/Logger/LoggerProxy.php
  1. <?php
  2. /**
  3.  * Copyright © Magento, Inc. All rights reserved.
  4.  * See COPYING.txt for license details.
  5.  */
  6. namespace Magento\Framework\DB\Logger;
  7.  
  8. use Magento\Framework\DB\LoggerInterface;
  9.  
  10. class LoggerProxy implements LoggerInterface
  11. {
  12.     /**
  13.      * Configuration group name
  14.      */
  15.     const CONF_GROUP_NAME = 'db_logger';
  16.  
  17.     /**
  18.      * Logger alias param name
  19.      */
  20.     const PARAM_ALIAS = 'output';
  21.  
  22.     /**
  23.      * Logger log all param name
  24.      */
  25.     const PARAM_LOG_ALL = 'log_everything';
  26.  
  27.     /**
  28.      * Logger query time param name
  29.      */
  30.     const PARAM_QUERY_TIME = 'query_time_threshold';
  31.  
  32.     /**
  33.      * Logger call stack param name
  34.      */
  35.     const PARAM_CALL_STACK = 'include_stacktrace';
  36.  
  37.     /**
  38.      * File logger alias
  39.      */
  40.     const LOGGER_ALIAS_FILE = 'file';
  41.  
  42.     /**
  43.      * Quiet logger alias
  44.      */
  45.     const LOGGER_ALIAS_DISABLED = 'disabled';
  46.  
  47.     /**
  48.      * @var LoggerInterface
  49.      */
  50.     private $logger;
  51.  
  52.     /**
  53.      * @var FileFactory
  54.      */
  55.     private $fileFactory;
  56.  
  57.     /**
  58.      * @var QuietFactory
  59.      */
  60.     private $quietFactory;
  61.  
  62.     /**
  63.      * @var bool
  64.      */
  65.     private $loggerAlias;
  66.  
  67.     /**
  68.      * @var bool
  69.      */
  70.     private $logAllQueries;
  71.  
  72.     /**
  73.      * @var float
  74.      */
  75.     private $logQueryTime;
  76.  
  77.     /**
  78.      * @var bool
  79.      */
  80.     private $logCallStack;
  81.  
  82.     /**
  83.      * LoggerProxy constructor.
  84.      * @param FileFactory $fileFactory
  85.      * @param QuietFactory $quietFactory
  86.      * @param bool $loggerAlias
  87.      * @param bool $logAllQueries
  88.      * @param float $logQueryTime
  89.      * @param bool $logCallStack
  90.      */
  91.     public function __construct(
  92.         FileFactory $fileFactory,
  93.         QuietFactory $quietFactory,
  94.         $loggerAlias,
  95.         $logAllQueries = true,
  96.         $logQueryTime = 0.001,
  97.         $logCallStack = true
  98.     ) {
  99.         $this->fileFactory = $fileFactory;
  100.         $this->quietFactory = $quietFactory;
  101.         $this->loggerAlias = 'file';
  102.         $this->logAllQueries = $logAllQueries;
  103.         $this->logQueryTime = $logQueryTime;
  104.         $this->logCallStack = $logCallStack;
  105.     }
  106.  
  107.     /**
  108.      * Get logger object. Initialize if needed.
  109.      * @return LoggerInterface
  110.      */
  111.     private function getLogger()
  112.     {
  113.         if ($this->logger === null) {
  114.             switch ($this->loggerAlias) {
  115.                 case self::LOGGER_ALIAS_FILE:
  116.                     $this->logger = $this->fileFactory->create(
  117.                         [
  118.                             'logAllQueries' => $this->logAllQueries,
  119.                             'logQueryTime' => $this->logQueryTime,
  120.                             'logCallStack' => $this->logCallStack,
  121.                         ]
  122.                     );
  123.                     break;
  124.                 default:
  125.                     $this->logger = $this->quietFactory->create();
  126.                     break;
  127.             }
  128.         }
  129.         return $this->logger;
  130.     }
  131.  
  132.     /**
  133.      * Adds log record
  134.      *
  135.      * @param string $str
  136.      * @return void
  137.      */
  138.     public function log($str)
  139.     {
  140.         $this->getLogger()->log($str);
  141.     }
  142.  
  143.     /**
  144.      * @param string $type
  145.      * @param string $sql
  146.      * @param array $bind
  147.      * @param \Zend_Db_Statement_Pdo|null $result
  148.      * @return void
  149.      */
  150.     public function logStats($type, $sql, $bind = [], $result = null)
  151.     {
  152.         $this->getLogger()->logStats($type, $sql, $bind, $result);
  153.     }
  154.  
  155.     /**
  156.      * @param \Exception $exception
  157.      * @return void
  158.      */
  159.     public function critical(\Exception $exception)
  160.     {
  161.         $this->getLogger()->critical($exception);
  162.     }
  163.  
  164.     /**
  165.      * @return void
  166.      */
  167.     public function startTimer()
  168.     {
  169.         $this->getLogger()->startTimer();
  170.     }
  171. }
  172.