eZ Publish  [trunk]
ezlog.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZLog class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*! \defgroup eZUtils Utility classes */
00012 
00013 /*!
00014   \class eZLog ezlog.php
00015   \ingroup eZUtils
00016 */
00017 
00018 
00019 class eZLog
00020 {
00021     const MAX_LOGROTATE_FILES = 3;
00022     const MAX_LOGFILE_SIZE = 204800; // 200*1024
00023 
00024     /*!
00025       Creates a new log object.
00026     */
00027     function eZLog( )
00028     {
00029     }
00030 
00031     /*!
00032      \static
00033      \public
00034      Writes a message $message to a given file name $name and directory $dir for logging
00035     */
00036     static function write( $message, $logName = 'common.log', $dir = 'var/log' )
00037     {
00038         $fileName = $dir . '/' . $logName;
00039         $oldumask = @umask( 0 );
00040 
00041         $fileExisted = file_exists( $fileName );
00042         if ( $fileExisted and
00043              filesize( $fileName ) > eZLog::maxLogSize() )
00044         {
00045             if ( eZLog::rotateLog( $fileName ) )
00046                 $fileExisted = false;
00047         }
00048         else if ( !$fileExisted and !file_exists( $dir ) )
00049         {
00050             eZDir::mkdir( $dir, false, true );
00051         }
00052 
00053         $logFile = @fopen( $fileName, "a" );
00054         if ( $logFile )
00055         {
00056             $time = strftime( "%b %d %Y %H:%M:%S", strtotime( "now" ) );
00057             $logMessage = "[ " . $time . " ] $message\n";
00058             @fwrite( $logFile, $logMessage );
00059             @fclose( $logFile );
00060             if ( !$fileExisted )
00061             {
00062                 $ini = eZINI::instance();
00063                 $permissions = octdec( $ini->variable( 'FileSettings', 'LogFilePermissions' ) );
00064                 @chmod( $fileName, $permissions );
00065             }
00066             @umask( $oldumask );
00067         }
00068         else
00069         {
00070             eZDebug::writeError( 'Couldn\'t create the log file "' . $fileName . '"', __METHOD__ );
00071         }
00072     }
00073 
00074     /*!
00075      \private
00076      Writes file name $name and storage directory $dir to storage log
00077     */
00078     static function writeStorageLog( $name, $dir = false )
00079     {
00080         $ini = eZINI::instance();
00081         $varDir = $ini->variable( 'FileSettings', 'VarDir' );
00082         $logDir = $ini->variable( 'FileSettings', 'LogDir' );
00083         $logName = 'storage.log';
00084         $fileName = $varDir . '/' . $logDir . '/' . $logName;
00085         $oldumask = @umask( 0 );
00086 
00087         $fileExisted = file_exists( $fileName );
00088         if ( $fileExisted and
00089              filesize( $fileName ) > eZLog::maxLogSize() )
00090         {
00091             if ( eZLog::rotateLog( $fileName ) )
00092                 $fileExisted = false;
00093         }
00094         else if ( !$fileExisted and !file_exists( $varDir . '/' . $logDir ) )
00095         {
00096             eZDir::mkdir( $varDir . '/' . $logDir, false, true );
00097         }
00098 
00099         if ( $dir !== false )
00100         {
00101             $dir = preg_replace( "#/$#", "", $dir );
00102             $dir .= "/";
00103         }
00104         else
00105         {
00106             $dir = "";
00107         }
00108 
00109         $logFile = @fopen( $fileName, "a" );
00110         if ( $logFile )
00111         {
00112             $time = strftime( "%b %d %Y %H:%M:%S", strtotime( "now" ) );
00113             $logMessage = "[ " . $time . " ] [" . $dir . $name . "]\n";
00114             @fwrite( $logFile, $logMessage );
00115             @fclose( $logFile );
00116             if ( !$fileExisted )
00117             {
00118                 $permissions = octdec( $ini->variable( 'FileSettings', 'LogFilePermissions' ) );
00119                 @chmod( $fileName, $permissions );
00120             }
00121             @umask( $oldumask );
00122         }
00123         else
00124         {
00125             eZDebug::writeError( 'Couldn\'t create the log file "' . $fileName . '"', __METHOD__ );
00126         }
00127     }
00128 
00129     /*!
00130      \static
00131      \return the maximum size for a log file in bytes.
00132     */
00133     static function maxLogSize()
00134     {
00135         $maxLogSize =& $GLOBALS['eZMaxLogSize'];
00136         if ( isset( $maxLogSize ) )
00137             return $maxLogSize;
00138         return self::MAX_LOGFILE_SIZE;
00139     }
00140 
00141     /*!
00142      \static
00143      Sets the maximum size for a log file to \a $size.
00144     */
00145     static function setMaxLogSize( $size )
00146     {
00147         $GLOBALS['eZMaxLogSize'] = $size;
00148     }
00149 
00150     /*!
00151      \static
00152      \return the maximum number of logrotate files to keep.
00153     */
00154     static function maxLogrotateFiles()
00155     {
00156         $maxLogrotateFiles =& $GLOBALS['eZMaxLogrotateFiles'];
00157         if ( isset( $maxLogrotateFiles ) )
00158             return $maxLogrotateFiles;
00159         return self::MAX_LOGROTATE_FILES;
00160     }
00161 
00162     /*!
00163      \static
00164      Rotates logfiles so the current logfile is backed up,
00165      old rotate logfiles are rotated once more and those that
00166      exceed maxLogrotateFiles() will be removed.
00167      Rotated files will get the extension .1, .2 etc.
00168     */
00169     static function rotateLog( $fileName )
00170     {
00171         $maxLogrotateFiles = eZLog::maxLogrotateFiles();
00172         for ( $i = $maxLogrotateFiles; $i > 0; --$i )
00173         {
00174             $logRotateName = $fileName . '.' . $i;
00175             if ( file_exists( $logRotateName ) )
00176             {
00177                 if ( $i == $maxLogrotateFiles )
00178                 {
00179                     @unlink( $logRotateName );
00180                 }
00181                 else
00182                 {
00183                     $newLogRotateName = $fileName . '.' . ($i + 1);
00184                     eZFile::rename( $logRotateName, $newLogRotateName );
00185                 }
00186             }
00187         }
00188         if ( file_exists( $fileName ) )
00189         {
00190             $newLogRotateName = $fileName . '.' . 1;
00191             eZFile::rename( $fileName, $newLogRotateName );
00192             return true;
00193         }
00194         return false;
00195     }
00196 
00197     /*!
00198      \static
00199      Sets the maximum number of logrotate files to keep to \a $files.
00200     */
00201     static function setLogrotateFiles( $files )
00202     {
00203         $GLOBALS['eZMaxLogrotateFiles'] = $files;
00204     }
00205 
00206 }
00207 
00208 ?>