PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezfile/classes/ezlog.php

https://github.com/lserwatka/ezpublish
PHP | 228 lines | 141 code | 19 blank | 68 comment | 20 complexity | b9e8f3fc597996ac61a3c906556c5846 MD5 | raw file
  1. <?php
  2. //
  3. // Definition of eZLog class
  4. //
  5. // Created on: <17-Mar-2003 11:00:54 wy>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*! \defgroup eZUtils Utility classes */
  31. /*!
  32. \class eZLog ezlog.php
  33. \ingroup eZUtils
  34. */
  35. class eZLog
  36. {
  37. const MAX_LOGROTATE_FILES = 3;
  38. const MAX_LOGFILE_SIZE = 204800; // 200*1024
  39. /*!
  40. Creates a new log object.
  41. */
  42. function eZLog( )
  43. {
  44. }
  45. /*!
  46. \static
  47. \public
  48. Writes a message $message to a given file name $name and directory $dir for logging
  49. */
  50. static function write( $message, $logName = 'common.log', $dir = 'var/log' )
  51. {
  52. $fileName = $dir . '/' . $logName;
  53. $oldumask = @umask( 0 );
  54. $fileExisted = @file_exists( $fileName );
  55. if ( $fileExisted and
  56. filesize( $fileName ) > eZLog::maxLogSize() )
  57. {
  58. if ( eZLog::rotateLog( $fileName ) )
  59. $fileExisted = false;
  60. }
  61. else if ( !$fileExisted and !file_exists( $dir ) )
  62. {
  63. eZDir::mkdir( $dir, false, true );
  64. }
  65. $logFile = @fopen( $fileName, "a" );
  66. if ( $logFile )
  67. {
  68. $time = strftime( "%b %d %Y %H:%M:%S", strtotime( "now" ) );
  69. $logMessage = "[ " . $time . " ] $message\n";
  70. @fwrite( $logFile, $logMessage );
  71. @fclose( $logFile );
  72. if ( !$fileExisted )
  73. {
  74. $ini = eZINI::instance();
  75. $permissions = octdec( $ini->variable( 'FileSettings', 'LogFilePermissions' ) );
  76. @chmod( $fileName, $permissions );
  77. }
  78. @umask( $oldumask );
  79. }
  80. else
  81. {
  82. eZDebug::writeError( 'Couldn\'t create the log file "' . $fileName . '"', 'eZLog::write()' );
  83. }
  84. }
  85. /*!
  86. \private
  87. Writes file name $name and storage directory $dir to storage log
  88. */
  89. static function writeStorageLog( $name, $dir = false )
  90. {
  91. $ini = eZINI::instance();
  92. $varDir = $ini->variable( 'FileSettings', 'VarDir' );
  93. $logDir = $ini->variable( 'FileSettings', 'LogDir' );
  94. $logName = 'storage.log';
  95. $fileName = $varDir . '/' . $logDir . '/' . $logName;
  96. $oldumask = @umask( 0 );
  97. $fileExisted = @file_exists( $fileName );
  98. if ( $fileExisted and
  99. filesize( $fileName ) > eZLog::maxLogSize() )
  100. {
  101. if ( eZLog::rotateLog( $fileName ) )
  102. $fileExisted = false;
  103. }
  104. else if ( !$fileExisted and !file_exists( $varDir . '/' . $logDir ) )
  105. {
  106. eZDir::mkdir( $varDir . '/' . $logDir, false, true );
  107. }
  108. if ( $dir !== false )
  109. {
  110. $dir = preg_replace( "#/$#", "", $dir );
  111. $dir .= "/";
  112. }
  113. else
  114. {
  115. $dir = "";
  116. }
  117. $logFile = @fopen( $fileName, "a" );
  118. if ( $logFile )
  119. {
  120. $time = strftime( "%b %d %Y %H:%M:%S", strtotime( "now" ) );
  121. $logMessage = "[ " . $time . " ] [" . $dir . $name . "]\n";
  122. @fwrite( $logFile, $logMessage );
  123. @fclose( $logFile );
  124. if ( !$fileExisted )
  125. {
  126. $permissions = octdec( $ini->variable( 'FileSettings', 'LogFilePermissions' ) );
  127. @chmod( $fileName, $permissions );
  128. }
  129. @umask( $oldumask );
  130. }
  131. else
  132. {
  133. eZDebug::writeError( 'Couldn\'t create the log file "' . $fileName . '"', 'eZLog::writeStorageLog()' );
  134. }
  135. }
  136. /*!
  137. \static
  138. \return the maxium size for a log file in bytes.
  139. */
  140. static function maxLogSize()
  141. {
  142. $maxLogSize =& $GLOBALS['eZMaxLogSize'];
  143. if ( isset( $maxLogSize ) )
  144. return $maxLogSize;
  145. return self::MAX_LOGFILE_SIZE;
  146. }
  147. /*!
  148. \static
  149. Sets the maxium size for a log file to \a $size.
  150. */
  151. static function setMaxLogSize( $size )
  152. {
  153. $GLOBALS['eZMaxLogSize'] = $size;
  154. }
  155. /*!
  156. \static
  157. \return the maxium number of logrotate files to keep.
  158. */
  159. static function maxLogrotateFiles()
  160. {
  161. $maxLogrotateFiles =& $GLOBALS['eZMaxLogrotateFiles'];
  162. if ( isset( $maxLogrotateFiles ) )
  163. return $maxLogrotateFiles;
  164. return self::MAX_LOGROTATE_FILES;
  165. }
  166. /*!
  167. \static
  168. Rotates logfiles so the current logfile is backed up,
  169. old rotate logfiles are rotated once more and those that
  170. exceed maxLogrotateFiles() will be removed.
  171. Rotated files will get the extension .1, .2 etc.
  172. */
  173. static function rotateLog( $fileName )
  174. {
  175. $maxLogrotateFiles = eZLog::maxLogrotateFiles();
  176. for ( $i = $maxLogrotateFiles; $i > 0; --$i )
  177. {
  178. $logRotateName = $fileName . '.' . $i;
  179. if ( @file_exists( $logRotateName ) )
  180. {
  181. if ( $i == $maxLogrotateFiles )
  182. {
  183. @unlink( $logRotateName );
  184. }
  185. else
  186. {
  187. $newLogRotateName = $fileName . '.' . ($i + 1);
  188. eZFile::rename( $logRotateName, $newLogRotateName );
  189. }
  190. }
  191. }
  192. if ( @file_exists( $fileName ) )
  193. {
  194. $newLogRotateName = $fileName . '.' . 1;
  195. eZFile::rename( $fileName, $newLogRotateName );
  196. return true;
  197. }
  198. return false;
  199. }
  200. /*!
  201. \static
  202. Sets the maxium number of logrotate files to keep to \a $files.
  203. */
  204. static function setLogrotateFiles( $files )
  205. {
  206. $GLOBALS['eZMaxLogrotateFiles'] = $files;
  207. }
  208. }
  209. ?>