Google_Logger_File

File logging class based on the PSR-3 standard.

Defined (1)

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

/includes/api-libs/Google/Logger/File.php  
  1. class Google_Logger_File extends Google_Logger_Abstract 
  2. /** 
  3. * @var string|resource $file Where logs are written 
  4. */ 
  5. private $file; 
  6. /** 
  7. * @var integer $mode The mode to use if the log file needs to be created 
  8. */ 
  9. private $mode = 0640; 
  10. /** 
  11. * @var boolean $lock If a lock should be attempted before writing to the log 
  12. */ 
  13. private $lock = false; 
  14.  
  15. /** 
  16. * @var integer $trappedErrorNumber Trapped error number 
  17. */ 
  18. private $trappedErrorNumber; 
  19. /** 
  20. * @var string $trappedErrorString Trapped error string 
  21. */ 
  22. private $trappedErrorString; 
  23.  
  24. /** 
  25. * {@inheritdoc} 
  26. */ 
  27. public function __construct(Google_Client $client) 
  28. parent::__construct($client); 
  29.  
  30. $file = $client->getClassConfig('Google_Logger_File', 'file'); 
  31. if (!is_string($file) && !is_resource($file)) { 
  32. throw new Google_Logger_Exception( 
  33. 'File logger requires a filename or a valid file pointer' 
  34. ); 
  35.  
  36. $mode = $client->getClassConfig('Google_Logger_File', 'mode'); 
  37. if (!$mode) { 
  38. $this->mode = $mode; 
  39.  
  40. $this->lock = (bool) $client->getClassConfig('Google_Logger_File', 'lock'); 
  41. $this->file = $file; 
  42.  
  43. /** 
  44. * {@inheritdoc} 
  45. */ 
  46. protected function write($message) 
  47. if (is_string($this->file)) { 
  48. $this->open(); 
  49. } elseif (!is_resource($this->file)) { 
  50. throw new Google_Logger_Exception('File pointer is no longer available'); 
  51.  
  52. if ($this->lock) { 
  53. flock($this->file, LOCK_EX); 
  54.  
  55. fwrite($this->file, (string) $message); 
  56.  
  57. if ($this->lock) { 
  58. flock($this->file, LOCK_UN); 
  59.  
  60. /** 
  61. * Opens the log for writing. 
  62. * @return resource 
  63. */ 
  64. private function open() 
  65. // Used for trapping `fopen()` errors. 
  66. $this->trappedErrorNumber = null; 
  67. $this->trappedErrorString = null; 
  68.  
  69. $old = set_error_handler(array($this, 'trapError')); 
  70.  
  71. $needsChmod = !file_exists($this->file); 
  72. $fh = fopen($this->file, 'a'); 
  73.  
  74. restore_error_handler(); 
  75.  
  76. // Handles trapped `fopen()` errors. 
  77. if ($this->trappedErrorNumber) { 
  78. throw new Google_Logger_Exception( 
  79. sprintf( 
  80. "Logger Error: '%s'",  
  81. $this->trappedErrorString 
  82. ),  
  83. $this->trappedErrorNumber 
  84. ); 
  85.  
  86. if ($needsChmod) { 
  87. @chmod($this->file, $this->mode & ~umask()); 
  88.  
  89. return $this->file = $fh; 
  90.  
  91. /** 
  92. * Closes the log stream resource. 
  93. */ 
  94. private function close() 
  95. if (is_resource($this->file)) { 
  96. fclose($this->file); 
  97.  
  98. /** 
  99. * Traps `fopen()` errors. 
  100. * @param integer $errno The error number 
  101. * @param string $errstr The error string 
  102. */ 
  103. private function trapError($errno, $errstr) 
  104. $this->trappedErrorNumber = $errno; 
  105. $this->trappedErrorString = $errstr; 
  106.  
  107. public function __destruct() 
  108. $this->close();