Project

General

Profile

« Previous | Next » 

Revision 2133

Added by darkviper over 8 years ago

+ new error_handler added
+ var/log/error.log added
+ new class LogRotation added

View differences:

branches/2.8.x/CHANGELOG
10 10
# = Bugfix
11 11
! = Update/Change
12 12
===============================================================================
13
01 Sep -2015 Build 2133 Manuela v.d.Decken(DarkViper)
14
+ new error_handler added
15
+ var/log/error.log added
16
+ new class LogRotation added
13 17
01 Sep -2015 Build 2132 Manuela v.d.Decken(DarkViper)
14 18
! class WbDatabase: all deprecated methods now will trigger a E_USER_DEPRECATED error.
15 19
23 Jun -2015 Build 2131 Manuela v.d.Decken(DarkViper)
branches/2.8.x/wb/admin/interface/version.php
51 51

  
52 52
// check if defined to avoid errors during installation (redirect to admin panel fails if PHP error/warnings are enabled)
53 53
if(!defined('VERSION')) define('VERSION', '2.8.4');
54
if(!defined('REVISION')) define('REVISION', '2132');
54
if(!defined('REVISION')) define('REVISION', '2133');
55 55
if(!defined('SP')) define('SP', '');
branches/2.8.x/wb/framework/initialize.php
236 236
            $bRetval = true;
237 237
        }
238 238
        $aBt= debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
239
        $x = sizeof($aBt) -1;
240
        $x = $x < 2 ? $x : 2;
239 241
        $sEntry = date('c').' '.'['.$sErrorType.'] '.str_replace(dirname(__DIR__), '', $sErrorFile).':['.$iErrorLine.'] '
240
                . ' from '.str_replace(dirname(__DIR__), '', $aBt[2]['file']).':['.$aBt[2]['line'].'] '
241
                . (@$aBt[2]['class'] ? $aBt[2]['class'].$aBt[2]['type'] : '').$aBt[2]['function'].' '
242
                . ' from '.str_replace(dirname(__DIR__), '', $aBt[$x]['file']).':['.$aBt[$x]['line'].'] '
243
                . (@$aBt[$x]['class'] ? $aBt[$x]['class'].$aBt[$x]['type'] : '').$aBt[$x]['function'].' '
242 244
                . '"'.$sErrorText.'"'.PHP_EOL;
243
        file_put_contents(dirname(__DIR__).'/temp/ERROR.LOG', $sEntry, FILE_APPEND);
245
        file_put_contents(dirname(__DIR__).'/var/log/error.log', $sEntry, FILE_APPEND);
244 246
        return $bRetval;
245 247
    }
246 248
/* ***************************************************************************************
......
307 309
    if(!function_exists('globalExceptionHandler')) {
308 310
        include(__DIR__.'/globalExceptionHandler.php');
309 311
    }
312
// check logfiles and compress it if needed
313
    $oLogRotate = new LogRotation($oReg);
314
    $oLogRotate->execute();
315
    unset($oLogRotate);
310 316
// ---------------------------
311 317
// get Database connection data from configuration
312 318
    $aSqlData = initGetDbConnectData($aCfg, $sDbConnectType);
......
339 345
                    case 'STRING_FILE_MODE':
340 346
                        $iTmp = ((intval(octdec($aRecords[$i]['value'])) & ~0111)|0600);
341 347
                        if(!defined('OCTAL_FILE_MODE')) { define('OCTAL_FILE_MODE', $iTmp); } // deprecated
342
                        if(!defined('FILE_MODE_OCTAL')) { define('FILE_MODE_OCTAL', $iTmp); }
348
                        if(!defined('FILE_MODE_OCTAL')) { define('FILE_MODE_OCTAL', $iTmp); } // deprecated
343 349
                        if(!defined('STRING_FILE_MODE')) { define('STRING_FILE_MODE', sprintf('0%03o', $iTmp)); } // deprecated
350
                        if(!defined('FILE_MODE')) { define('FILE_MODE', $iTmp); }
344 351
                        if(!defined('FILE_MODE_STRING')) { define('FILE_MODE_STRING', sprintf('0%03o', $iTmp)); }
345 352
                        break;
346 353
                    case 'STRING_DIR_MODE':
347 354
                        $iTmp = (intval(octdec($aRecords[$i]['value'])) |0711);
348 355
                        if(!defined('OCTAL_DIR_MODE')) { define('OCTAL_DIR_MODE', $iTmp); } // deprecated
349
                        if(!defined('DIR_MODE_OCTAL')) { define('DIR_MODE_OCTAL', $iTmp); }
356
                        if(!defined('DIR_MODE_OCTAL')) { define('DIR_MODE_OCTAL', $iTmp); } // deprecated
350 357
                        if(!defined('STRING_DIR_MODE')) { define('STRING_DIR_MODE', sprintf('0%03o', $iTmp)); } // deprecated
358
                        if(!defined('DIR_MODE')) { define('DIR_MODE', $iTmp); }
351 359
                        if(!defined('DIR_MODE_STRING')) { define('DIR_MODE_STRING', sprintf('0%03o', $iTmp)); }
352 360
                        break;
353 361
                    case 'PAGES_DIRECTORY':
......
463 471
// load and activate new global translation table
464 472
    $oTrans = Translate::getInstance();
465 473
/* initializise Translate old style *************************************************** */
474

  
466 475
    $oTrans->initialize(
467 476
        'en',
468 477
        (defined('DEFAULT_LANGUAGE') ? DEFAULT_LANGUAGE : ''),
......
471 480
//        (Translate::CACHE_DISABLED|Translate::KEEP_MISSING),
472 481
        (DEBUG ? Translate::CACHE_DISABLED|Translate::KEEP_MISSING : 0)
473 482
    );
483

  
474 484
/* initializise Translate new style *************************************************** */
475 485
/*
476 486
    $oTrans->initialize(
477
        new TranslateAdaptorWbOldStyle,
487
        new TranslateAdaptorWbOldStyle($oReg),
478 488
        'en',
479 489
        (defined('DEFAULT_LANGUAGE') ? DEFAULT_LANGUAGE : ''),
480 490
        (defined('LANGUAGE') ? LANGUAGE : ''),
481
//        (Translate::CACHE_DISABLED|Translate::KEEP_MISSING),
482 491
        (DEBUG ? Translate::CACHE_DISABLED|Translate::KEEP_MISSING : 0),
483 492
        $oReg->DirModeOctal,
484 493
        $oReg->TempPath
485 494
    );
486
 */
495
*/
487 496
/* ****** */
488 497
    $oReg->setDatabase(WbDatabase::getInstance());
489 498
    $oReg->setTranslate(Translate::getInstance());
branches/2.8.x/wb/framework/LogRotation.php
1
<?php
2

  
3
/*
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

  
20
/**
21
 * LogRotation.php
22
 *
23
 * @category     Core
24
 * @package      Core_logging
25
 * @copyright    Manuela v.d.Decken <manuela@isteam.de>
26
 * @author       Manuela v.d.Decken <manuela@isteam.de>
27
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
28
 * @version      0.0.1
29
 * @revision     $Revision: $
30
 * @lastmodified $Date: $
31
 * @since        File available since 29.06.2015
32
 * @description  handles logfiles and compress it if they are too big(like server logs)
33
 */
34
class LogRotation
35
{
36
    protected $iMaxLogfileSize = 20;   // size as MB
37
    protected $iMaxLogfiles    = 10;   //
38
    protected $iScanFrequency  = 3600; // scan each hour
39
    protected $sLogPath        = '';   // AppPath + 'var/log'
40
    protected $aLogFiles       = null; // like 'error.log'
41
    protected $aLogFilesGz     = null; // like 'error.log.1.gz'
42

  
43
/**
44
 * constructor
45
 * @param WbAdaptor $oReg
46
 * @throws AppException
47
 */
48
    public function __construct(WbAdaptor $oReg)
49
    {
50
        $this->sLogPath = $oReg->VarPath.'log/';
51
        if (!file_exists($this->sLogPath)) {
52
            $iOldUmask = umask(0);
53
            if (!mkdir($this->sLogPath, $oReg->FileMode, true)) {
54
                throw new AppException('unable to create LogPath ['.$this->sLogPath.']');
55
            }
56
            umask($iOldUmask);
57
        }
58
    }
59
/**
60
 * proceed compression if needed
61
 */
62
    public function execute()
63
    {
64
        if ($this->checkSemaphore($this->sLogPath.'LOCK')) {
65
            // scan for uncompressed log files
66
            $this->aLogFiles = $this->scanForFiles($this->sLogPath);
67
            foreach($this->aLogFiles as $sFile) {
68
            // iterate matching log files
69
                if ($this->isLogFileFull($this->sLogPath.$sFile)) {
70
                // start processing if uncompressed file exceeds limit
71
                    $aGzFiles = $this->scanForFiles($this->sLogPath, $sFile.'.*.gz');
72
                    // rename existing gz files
73
                    $this->renameExistingGzFiles($aGzFiles);
74
                    // compress logfile into gz archive
75
                    $this->compressFile($this->sLogPath.$sFile, $this->sLogPath.$sFile.'.0.gz');
76
                    // remove logfile (next entry will create new one)
77
                    unlink($this->sLogPath.$sFile);
78
                }
79
            }
80
        }
81
    }
82
/**
83
 *
84
 * @param string $sSemaphoreFile file which holds the timestamp from last scan
85
 * @return bool
86
 * @throws AppException
87
 */
88
    protected function checkSemaphore($sSemaphoreFile)
89
    {
90
        // test if readable file exists
91
        if (!is_readable($sSemaphoreFile)) {
92
            // try to create with current timestamp
93
            if (!touch($sSemaphoreFile)) {
94
                throw new AppException('unable to create semaphore ['.$sSemaphoreFile.']');
95
            }
96
        } else {
97
            // return true if it's time for next scan
98
            return ((filemtime($sSemaphoreFile) + $this->iScanFrequency) > time());
99
        }
100
    }
101
/**
102
 * compress source file into target file
103
 * @param string $sSourceFile
104
 * @param string $sTargetFile
105
 */
106
    protected function compressFile($sSourceFile, $sTargetFile)
107
    {
108
        // try to write/create target file
109
        if (touch($sTargetFile)) {
110
            $oSource = gzopen($sSourceFile, 'rb');
111
            $oTarget = gzopen($sTargetFile, 'wb9');
112
            if ($oSource && $oTarget) {
113
                while (!gzeof($oSource)) {
114
                    // read 64k buffer
115
                    $sBuff = gzread($oSource, 64*1024);
116
                    $iBuffLen = strlen($sBuff);
117
                    // write the buffer
118
                    gzwrite($oTarget, $sBuff, $iBuffLen);
119
                }
120
                // close files and clear buffer
121
                gzclose($oSource);
122
                gzclose($oTarget);
123
                unset($sBuff);
124
            }
125
        }
126
    }
127
/**
128
 * rename files to index+1 and delete files with index >= iMaxLogfiles
129
 * @param array $aGzFiles
130
 * @param string $sBasefile
131
 */
132
    protected function renameExistingGzFiles(array $aGzFiles, $sBasefile)
133
    {
134
        natsort($aGzFiles);
135
        $aGzFiles = array_merge(array_reverse($aGzFiles));
136
        $sPattern = '/^'.preg_quote($sBasefile, '/').'\.([0-9]+)\.gz$/s';
137
        foreach ($aGzFiles as $iKey=>$sFile) {
138
            if ($iKey < $this->iMaxLogfiles) {
139
                if (preg_match($sPattern, $sFile, $aMatch)) {
140
                    $sNewFile = $sBasefile.'.'.(string)($aMatch[1]++).'.gz';
141
                    rename($this->sLogPath.$sFile, $this->sLogPath.$sNewFile);
142
                }
143
            } else {
144
                unlink($this->sLogPath.$sFile);
145
            }
146
        }
147
    }
148
/**
149
 * check size of file
150
 * @param string $sFile
151
 * @return bool true if filesize greater than iMaxLogfileSize
152
 */
153
    protected function isLogFileFull($sFile)
154
    {
155
        $iSize = (int)floor(filesize($sFile) / pow(2, 20));
156
        return ($iSize >= $this->iMaxLogfileSize);
157
    }
158
/**
159
 * scan for log files
160
 * @param string $sLogPath
161
 * @param string $sPattern
162
 * @return array  list of files
163
 */
164
    protected function scanForFiles($sLogPath, $sPattern = '*.log')
165
    {
166
        $sOldDir = getcwd();
167
        chdir($sLogPath);
168
        $aRetval = glob($sPattern, GLOB_NOSORT);
169
        chdir($sOldDir);
170
        return $aRetval;
171
    }
172

  
173
} // end of class LogRotation

Also available in: Unified diff