Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezlog.php
Go to the documentation of this file.
1 <?php
19 class eZLog
20 {
21 const MAX_LOGROTATE_FILES = 3;
22 const MAX_LOGFILE_SIZE = 204800; // 200*1024
23
29 static function write( $message, $logName = 'common.log', $dir = 'var/log' )
30 {
31 $fileName = $dir . '/' . $logName;
32 $oldumask = @umask( 0 );
33
34 $fileExisted = file_exists( $fileName );
35 if ( $fileExisted and
36 filesize( $fileName ) > eZLog::maxLogSize() )
37 {
38 if ( eZLog::rotateLog( $fileName ) )
39 $fileExisted = false;
40 }
41 else if ( !$fileExisted and !file_exists( $dir ) )
42 {
43 eZDir::mkdir( $dir, false, true );
44 }
45
46 $logFile = @fopen( $fileName, "a" );
47 if ( $logFile )
48 {
49 $ini = eZINI::instance();
50 $time = date( "M d Y H:i:s", strtotime( "now" ) );
51
52 $logMessage = "[ " . $time . " ]";
53 $logMessage .= "[ {$GLOBALS['eZCurrentAccess']['name']} ]";
54 if (php_sapi_name() === 'cli') {
55 $pid = getmypid();
56 $command = exec("ps -p $pid -o args=");
57 $logMessage .= '[ ' . $command . ' ]';
58 } else {
59 $scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
60 $host = $_SERVER['HTTP_HOST'];
61 $uri = $_SERVER['REQUEST_URI'];
62 $url = "$scheme://$host$uri";
63 $logMessage .= '[ ' . $url . ' ]';
64 }
65 $logMessage .= " $message\n";
66 @fwrite( $logFile, $logMessage );
67 @fclose( $logFile );
68 if ( !$fileExisted )
69 {
70 $permissions = octdec( $ini->variable( 'FileSettings', 'LogFilePermissions' ) );
71 @chmod( $fileName, $permissions );
72 }
73 @umask( $oldumask );
74 }
75 else
76 {
77 eZDebug::writeError( 'Couldn\'t create the log file "' . $fileName . '"', __METHOD__ );
78 }
79 }
80
85 static function writeStorageLog( $name, $dir = false )
86 {
87 $ini = eZINI::instance();
88 $varDir = $ini->variable( 'FileSettings', 'VarDir' );
89 $logDir = $ini->variable( 'FileSettings', 'LogDir' );
90 $logName = 'storage.log';
91 $fileName = $varDir . '/' . $logDir . '/' . $logName;
92 $oldumask = @umask( 0 );
93
94 clearstatcache( true, $fileName );
95 $fileExisted = file_exists( $fileName );
96 if ( $fileExisted and
97 filesize( $fileName ) > eZLog::maxLogSize() )
98 {
99 if ( eZLog::rotateLog( $fileName ) )
100 $fileExisted = false;
101 }
102 else if ( !$fileExisted and !file_exists( $varDir . '/' . $logDir ) )
103 {
104 eZDir::mkdir( $varDir . '/' . $logDir, false, true );
105 }
106
107 if ( $dir !== false )
108 {
109 $dir = preg_replace( "#/$#", "", $dir );
110 $dir .= "/";
111 }
112 else
113 {
114 $dir = "";
115 }
116
117 $logFile = @fopen( $fileName, "a" );
118 if ( $logFile )
119 {
120 $time = date( "M d Y H:i: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 . '"', __METHOD__ );
134 }
135 }
136
141 static function maxLogSize()
142 {
143 $maxLogSize =& $GLOBALS['eZMaxLogSize'];
144 if ( isset( $maxLogSize ) )
145 {
146 return $maxLogSize;
147 }
148 else if ( defined( 'CUSTOM_LOG_MAX_FILE_SIZE' ) )
149 {
150 self::setMaxLogSize( (int)CUSTOM_LOG_MAX_FILE_SIZE );
151 return (int)CUSTOM_LOG_MAX_FILE_SIZE;
152 }
153 return self::MAX_LOGFILE_SIZE;
154 }
155
160 static function setMaxLogSize( $size )
161 {
162 $GLOBALS['eZMaxLogSize'] = $size;
163 }
164
169 static function maxLogrotateFiles()
170 {
171 $maxLogrotateFiles =& $GLOBALS['eZMaxLogrotateFiles'];
172 if ( isset( $maxLogrotateFiles ) )
173 {
174 return $maxLogrotateFiles;
175 }
176 else if ( defined( 'CUSTOM_LOG_ROTATE_FILES' ) )
177 {
178 self::setLogrotateFiles( (int)CUSTOM_LOG_ROTATE_FILES );
179 return (int)CUSTOM_LOG_ROTATE_FILES;
180 }
181 return self::MAX_LOGROTATE_FILES;
182 }
183
191 static function rotateLog( $fileName )
192 {
193 $maxLogrotateFiles = eZLog::maxLogrotateFiles();
194 if ( $maxLogrotateFiles == 0 )
195 {
196 return;
197 }
198 for ( $i = $maxLogrotateFiles; $i > 0; --$i )
199 {
200 $logRotateName = $fileName . '.' . $i;
201 if ( file_exists( $logRotateName ) )
202 {
203 if ( $i == $maxLogrotateFiles )
204 {
205 @unlink( $logRotateName );
206 }
207 else
208 {
209 $newLogRotateName = $fileName . '.' . ($i + 1);
210 eZFile::rename( $logRotateName, $newLogRotateName );
211 }
212 }
213 }
214 if ( file_exists( $fileName ) )
215 {
216 $newLogRotateName = $fileName . '.' . 1;
217 eZFile::rename( $fileName, $newLogRotateName );
218 return true;
219 }
220 return false;
221 }
222
227 static function setLogrotateFiles( $files )
228 {
229 $GLOBALS['eZMaxLogrotateFiles'] = $files;
230 }
231
232 }
233
234 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:73
eZFile\rename
static rename( $srcFile, $destFile, $mkdir=false, $flags=0)
Definition: ezfile.php:152
eZLog
Definition: ezlog.php:20
eZLog\MAX_LOGROTATE_FILES
const MAX_LOGROTATE_FILES
Definition: ezlog.php:21
eZLog\write
static write( $message, $logName='common.log', $dir='var/log')
Definition: ezlog.php:29
eZLog\setMaxLogSize
static setMaxLogSize( $size)
Definition: ezlog.php:160
eZLog\MAX_LOGFILE_SIZE
const MAX_LOGFILE_SIZE
Definition: ezlog.php:22
eZLog\writeStorageLog
static writeStorageLog( $name, $dir=false)
Definition: ezlog.php:85
eZLog\setLogrotateFiles
static setLogrotateFiles( $files)
Definition: ezlog.php:227
eZLog\rotateLog
static rotateLog( $fileName)
Definition: ezlog.php:191
eZLog\maxLogrotateFiles
static maxLogrotateFiles()
Definition: ezlog.php:169
eZLog\maxLogSize
static maxLogSize()
Definition: ezlog.php:141
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.