GoogleGAL_Logger_Abstract

Abstract logging class based on the PSR-3 standard.

Defined (1)

The class is defined in the following location(s).

/core/Google/Logger/Abstract.php  
  1. abstract class GoogleGAL_Logger_Abstract 
  2. /** 
  3. * Default log format 
  4. */ 
  5. const DEFAULT_LOG_FORMAT = "[%datetime%] %level%: %message% %context%\n"; 
  6. /** 
  7. * Default date format 
  8. * Example: 16/Nov/2014:03:26:16 -0500 
  9. */ 
  10. const DEFAULT_DATE_FORMAT = 'd/M/Y:H:i:s O'; 
  11.  
  12. /** 
  13. * System is unusable 
  14. */ 
  15. const EMERGENCY = 'emergency'; 
  16. /** 
  17. * Action must be taken immediately 
  18. * Example: Entire website down, database unavailable, etc. This should 
  19. * trigger the SMS alerts and wake you up. 
  20. */ 
  21. const ALERT = 'alert'; 
  22. /** 
  23. * Critical conditions 
  24. * Example: Application component unavailable, unexpected exception. 
  25. */ 
  26. const CRITICAL = 'critical'; 
  27. /** 
  28. * Runtime errors that do not require immediate action but should typically 
  29. * be logged and monitored. 
  30. */ 
  31. const ERROR = 'error'; 
  32. /** 
  33. * Exceptional occurrences that are not errors. 
  34. * Example: Use of deprecated APIs, poor use of an API, undesirable things 
  35. * that are not necessarily wrong. 
  36. */ 
  37. const WARNING = 'warning'; 
  38. /** 
  39. * Normal but significant events. 
  40. */ 
  41. const NOTICE = 'notice'; 
  42. /** 
  43. * Interesting events. 
  44. * Example: User logs in, SQL logs. 
  45. */ 
  46. const INFO = 'info'; 
  47. /** 
  48. * Detailed debug information. 
  49. */ 
  50. const DEBUG = 'debug'; 
  51.  
  52. /** 
  53. * @var array $levels Logging levels 
  54. */ 
  55. protected static $levels = array( 
  56. self::EMERGENCY => 600,  
  57. self::ALERT => 550,  
  58. self::CRITICAL => 500,  
  59. self::ERROR => 400,  
  60. self::WARNING => 300,  
  61. self::NOTICE => 250,  
  62. self::INFO => 200,  
  63. self::DEBUG => 100,  
  64. ); 
  65.  
  66. /** 
  67. * @var integer $level The minimum logging level 
  68. */ 
  69. protected $level = self::DEBUG; 
  70.  
  71. /** 
  72. * @var string $logFormat The current log format 
  73. */ 
  74. protected $logFormat = self::DEFAULT_LOG_FORMAT; 
  75. /** 
  76. * @var string $dateFormat The current date format 
  77. */ 
  78. protected $dateFormat = self::DEFAULT_DATE_FORMAT; 
  79.  
  80. /** 
  81. * @var boolean $allowNewLines If newlines are allowed 
  82. */ 
  83. protected $allowNewLines = false; 
  84.  
  85. /** 
  86. * @param GoogleGAL_Client $client The current Google client 
  87. */ 
  88. public function __construct(GoogleGAL_Client $client) 
  89. $this->setLevel( 
  90. $client->getClassConfig('GoogleGAL_Logger_Abstract', 'level') 
  91. ); 
  92.  
  93. $format = $client->getClassConfig('GoogleGAL_Logger_Abstract', 'log_format'); 
  94. $this->logFormat = $format ? $format : self::DEFAULT_LOG_FORMAT; 
  95.  
  96. $format = $client->getClassConfig('GoogleGAL_Logger_Abstract', 'date_format'); 
  97. $this->dateFormat = $format ? $format : self::DEFAULT_DATE_FORMAT; 
  98.  
  99. $this->allowNewLines = (bool) $client->getClassConfig( 
  100. 'GoogleGAL_Logger_Abstract',  
  101. 'allow_newlines' 
  102. ); 
  103.  
  104. /** 
  105. * Sets the minimum logging level that this logger handles. 
  106. * @param integer $level 
  107. */ 
  108. public function setLevel($level) 
  109. $this->level = $this->normalizeLevel($level); 
  110.  
  111. /** 
  112. * Checks if the logger should handle messages at the provided level. 
  113. * @param integer $level 
  114. * @return boolean 
  115. */ 
  116. public function shouldHandle($level) 
  117. return $this->normalizeLevel($level) >= $this->level; 
  118.  
  119. /** 
  120. * System is unusable. 
  121. * @param string $message The log message 
  122. * @param array $context The log context 
  123. */ 
  124. public function emergency($message, array $context = array()) 
  125. $this->log(self::EMERGENCY, $message, $context); 
  126.  
  127. /** 
  128. * Action must be taken immediately. 
  129. * Example: Entire website down, database unavailable, etc. This should 
  130. * trigger the SMS alerts and wake you up. 
  131. * @param string $message The log message 
  132. * @param array $context The log context 
  133. */ 
  134. public function alert($message, array $context = array()) 
  135. $this->log(self::ALERT, $message, $context); 
  136.  
  137. /** 
  138. * Critical conditions. 
  139. * Example: Application component unavailable, unexpected exception. 
  140. * @param string $message The log message 
  141. * @param array $context The log context 
  142. */ 
  143. public function critical($message, array $context = array()) 
  144. $this->log(self::CRITICAL, $message, $context); 
  145.  
  146. /** 
  147. * Runtime errors that do not require immediate action but should typically 
  148. * be logged and monitored. 
  149. * @param string $message The log message 
  150. * @param array $context The log context 
  151. */ 
  152. public function error($message, array $context = array()) 
  153. $this->log(self::ERROR, $message, $context); 
  154.  
  155. /** 
  156. * Exceptional occurrences that are not errors. 
  157. * Example: Use of deprecated APIs, poor use of an API, undesirable things 
  158. * that are not necessarily wrong. 
  159. * @param string $message The log message 
  160. * @param array $context The log context 
  161. */ 
  162. public function warning($message, array $context = array()) 
  163. $this->log(self::WARNING, $message, $context); 
  164.  
  165. /** 
  166. * Normal but significant events. 
  167. * @param string $message The log message 
  168. * @param array $context The log context 
  169. */ 
  170. public function notice($message, array $context = array()) 
  171. $this->log(self::NOTICE, $message, $context); 
  172.  
  173. /** 
  174. * Interesting events. 
  175. * Example: User logs in, SQL logs. 
  176. * @param string $message The log message 
  177. * @param array $context The log context 
  178. */ 
  179. public function info($message, array $context = array()) 
  180. $this->log(self::INFO, $message, $context); 
  181.  
  182. /** 
  183. * Detailed debug information. 
  184. * @param string $message The log message 
  185. * @param array $context The log context 
  186. */ 
  187. public function debug($message, array $context = array()) 
  188. $this->log(self::DEBUG, $message, $context); 
  189.  
  190. /** 
  191. * Logs with an arbitrary level. 
  192. * @param mixed $level The log level 
  193. * @param string $message The log message 
  194. * @param array $context The log context 
  195. */ 
  196. public function log($level, $message, array $context = array()) 
  197. if (!$this->shouldHandle($level)) { 
  198. return false; 
  199.  
  200. $levelName = is_int($level) ? array_search($level, self::$levels) : $level; 
  201. $message = $this->interpolate( 
  202. array( 
  203. 'message' => $message,  
  204. 'context' => $context,  
  205. 'level' => strtoupper($levelName),  
  206. 'datetime' => new DateTime(),  
  207. ); 
  208.  
  209. $this->write($message); 
  210.  
  211. /** 
  212. * Interpolates log variables into the defined log format. 
  213. * @param array $variables The log variables. 
  214. * @return string 
  215. */ 
  216. protected function interpolate(array $variables = array()) 
  217. $template = $this->logFormat; 
  218.  
  219. if (!$variables['context']) { 
  220. $template = str_replace('%context%', '', $template); 
  221. unset($variables['context']); 
  222. } else { 
  223. $this->reverseJsonInContext($variables['context']); 
  224.  
  225. foreach ($variables as $key => $value) { 
  226. if (strpos($template, '%'. $key .'%') !== false) { 
  227. $template = str_replace( 
  228. '%' . $key . '%',  
  229. $this->export($value),  
  230. $template 
  231. ); 
  232.  
  233. return $template; 
  234.  
  235. /** 
  236. * Reverses JSON encoded PHP arrays and objects so that they log better. 
  237. * @param array $context The log context 
  238. */ 
  239. protected function reverseJsonInContext(array &$context) 
  240. if (!$context) { 
  241. return; 
  242.  
  243. foreach ($context as $key => $val) { 
  244. if (!$val || !is_string($val) || !($val[0] == '{' || $val[0] == '[')) { 
  245. continue; 
  246.  
  247. $json = @json_decode($val); 
  248. if (is_object($json) || is_array($json)) { 
  249. $context[$key] = $json; 
  250.  
  251. /** 
  252. * Exports a PHP value for logging to a string. 
  253. * @param mixed $value The value to 
  254. */ 
  255. protected function export($value) 
  256. if (is_string($value)) { 
  257. if ($this->allowNewLines) { 
  258. return $value; 
  259.  
  260. return preg_replace('/[\r\n]+/', ' ', $value); 
  261.  
  262. if (is_resource($value)) { 
  263. return sprintf( 
  264. 'resource(%d) of type (%s)',  
  265. $value,  
  266. get_resource_type($value) 
  267. ); 
  268.  
  269. if ($value instanceof DateTime) { 
  270. return $value->format($this->dateFormat); 
  271.  
  272. if (version_compare(PHP_VERSION, '5.4.0', '>=')) { 
  273. $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; 
  274.  
  275. if ($this->allowNewLines) { 
  276. $options |= JSON_PRETTY_PRINT; 
  277.  
  278. return @json_encode($value, $options); 
  279.  
  280. return str_replace('\\/', '/', @json_encode($value)); 
  281.  
  282. /** 
  283. * Converts a given log level to the integer form. 
  284. * @param mixed $level The logging level 
  285. * @return integer $level The normalized level 
  286. * @throws GoogleGAL_Logger_Exception If $level is invalid 
  287. */ 
  288. protected function normalizeLevel($level) 
  289. if (is_int($level) && array_search($level, self::$levels) !== false) { 
  290. return $level; 
  291.  
  292. if (is_string($level) && isset(self::$levels[$level])) { 
  293. return self::$levels[$level]; 
  294.  
  295. throw new GoogleGAL_Logger_Exception( 
  296. sprintf("Unknown LogLevel: '%s'", $level) 
  297. ); 
  298.  
  299. /** 
  300. * Writes a message to the current log implementation. 
  301. * @param string $message The message 
  302. */ 
  303. abstract protected function write($message);