|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZLog class 00004 // 00005 // Created on: <17-Mar-2003 11:00:54 wy> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! \defgroup eZUtils Utility classes */ 00032 00033 /*! 00034 \class eZLog ezlog.php 00035 \ingroup eZUtils 00036 */ 00037 00038 00039 //include_once( "lib/ezutils/classes/ezsys.php" ); 00040 00041 class eZLog 00042 { 00043 const MAX_LOGROTATE_FILES = 3; 00044 const MAX_LOGFILE_SIZE = 204800; // 200*1024 00045 00046 /*! 00047 Creates a new log object. 00048 */ 00049 function eZLog( ) 00050 { 00051 } 00052 00053 /*! 00054 \static 00055 \public 00056 Writes a message $message to a given file name $name and directory $dir for logging 00057 */ 00058 static function write( $message, $logName = 'common.log', $dir = 'var/log' ) 00059 { 00060 $ini = eZINI::instance(); 00061 $fileName = $dir . '/' . $logName; 00062 if ( !file_exists( $dir ) ) 00063 { 00064 //include_once( 'lib/ezfile/classes/ezdir.php' ); 00065 eZDir::mkdir( $dir, false, true ); 00066 } 00067 $oldumask = @umask( 0 ); 00068 00069 $fileExisted = @file_exists( $fileName ); 00070 if ( $fileExisted and 00071 filesize( $fileName ) > eZLog::maxLogSize() ) 00072 { 00073 if ( eZLog::rotateLog( $fileName ) ) 00074 $fileExisted = false; 00075 } 00076 00077 $logFile = @fopen( $fileName, "a" ); 00078 if ( $logFile ) 00079 { 00080 $time = strftime( "%b %d %Y %H:%M:%S", strtotime( "now" ) ); 00081 $logMessage = "[ " . $time . " ] $message\n"; 00082 @fwrite( $logFile, $logMessage ); 00083 @fclose( $logFile ); 00084 if ( !$fileExisted ) 00085 { 00086 $ini = eZINI::instance(); 00087 $permissions = octdec( $ini->variable( 'FileSettings', 'LogFilePermissions' ) ); 00088 @chmod( $fileName, $permissions ); 00089 } 00090 @umask( $oldumask ); 00091 } 00092 } 00093 00094 /*! 00095 \private 00096 Writes file name $name and storage directory $dir to storage log 00097 */ 00098 static function writeStorageLog( $name, $dir = false ) 00099 { 00100 $ini = eZINI::instance(); 00101 $varDir = $ini->variable( 'FileSettings', 'VarDir' ); 00102 $logDir = $ini->variable( 'FileSettings', 'LogDir' ); 00103 $logName = 'storage.log'; 00104 $fileName = $varDir . '/' . $logDir . '/' . $logName; 00105 if ( !file_exists( $varDir . '/' . $logDir ) ) 00106 { 00107 //include_once( 'lib/ezfile/classes/ezdir.php' ); 00108 eZDir::mkdir( $varDir . '/' . $logDir, false, true ); 00109 } 00110 00111 $fileExisted = @file_exists( $fileName ); 00112 if ( $fileExisted and 00113 filesize( $fileName ) > eZLog::maxLogSize() ) 00114 { 00115 if ( eZLog::rotateLog( $fileName ) ) 00116 $fileExisted = false; 00117 } 00118 00119 if ( $dir !== false ) 00120 { 00121 $dir = preg_replace( "#/$#", "", $dir ); 00122 $dir .= "/"; 00123 } 00124 else 00125 { 00126 $dir = ""; 00127 } 00128 00129 $logFile = @fopen( $fileName, "a" ); 00130 if ( $logFile ) 00131 { 00132 $time = strftime( "%b %d %Y %H:%M:%S", strtotime( "now" ) ); 00133 $logMessage = "[ " . $time . " ] [" . $dir . $name . "]\n"; 00134 @fwrite( $logFile, $logMessage ); 00135 @fclose( $logFile ); 00136 if ( !$fileExisted ) 00137 { 00138 $ini = eZINI::instance(); 00139 $permissions = octdec( $ini->variable( 'FileSettings', 'LogFilePermissions' ) ); 00140 @chmod( $fileName, $permissions ); 00141 } 00142 @umask( $oldumask ); 00143 } 00144 } 00145 00146 /*! 00147 \static 00148 \return the maxium size for a log file in bytes. 00149 */ 00150 static function maxLogSize() 00151 { 00152 $maxLogSize =& $GLOBALS['eZMaxLogSize']; 00153 if ( isset( $maxLogSize ) ) 00154 return $maxLogSize; 00155 return self::MAX_LOGFILE_SIZE; 00156 } 00157 00158 /*! 00159 \static 00160 Sets the maxium size for a log file to \a $size. 00161 */ 00162 static function setMaxLogSize( $size ) 00163 { 00164 $GLOBALS['eZMaxLogSize'] = $size; 00165 } 00166 00167 /*! 00168 \static 00169 \return the maxium number of logrotate files to keep. 00170 */ 00171 static function maxLogrotateFiles() 00172 { 00173 $maxLogrotateFiles =& $GLOBALS['eZMaxLogrotateFiles']; 00174 if ( isset( $maxLogrotateFiles ) ) 00175 return $maxLogrotateFiles; 00176 return self::MAX_LOGROTATE_FILES; 00177 } 00178 00179 /*! 00180 \static 00181 Rotates logfiles so the current logfile is backed up, 00182 old rotate logfiles are rotated once more and those that 00183 exceed maxLogrotateFiles() will be removed. 00184 Rotated files will get the extension .1, .2 etc. 00185 */ 00186 static function rotateLog( $fileName ) 00187 { 00188 //include_once( 'lib/ezfile/classes/ezfile.php' ); 00189 $maxLogrotateFiles = eZLog::maxLogrotateFiles(); 00190 for ( $i = $maxLogrotateFiles; $i > 0; --$i ) 00191 { 00192 $logRotateName = $fileName . '.' . $i; 00193 if ( @file_exists( $logRotateName ) ) 00194 { 00195 if ( $i == $maxLogrotateFiles ) 00196 { 00197 @unlink( $logRotateName ); 00198 } 00199 else 00200 { 00201 $newLogRotateName = $fileName . '.' . ($i + 1); 00202 eZFile::rename( $logRotateName, $newLogRotateName ); 00203 } 00204 } 00205 } 00206 if ( @file_exists( $fileName ) ) 00207 { 00208 $newLogRotateName = $fileName . '.' . 1; 00209 eZFile::rename( $fileName, $newLogRotateName ); 00210 return true; 00211 } 00212 return false; 00213 } 00214 00215 /*! 00216 \static 00217 Sets the maxium number of logrotate files to keep to \a $files. 00218 */ 00219 static function setLogrotateFiles( $files ) 00220 { 00221 $GLOBALS['eZMaxLogrotateFiles'] = $files; 00222 } 00223 00224 } 00225 00226 ?>