Source for file LogFile.inc
Documentation is available at LogFile.inc
* Generic log file class to encapsulate common file I/O and rendering calls
* @author John Collins <dev@alphaframework.org>
* @version $Id: LogFile.inc 1341 2011-03-17 15:02:02Z johnc $
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* An array of column headers use when rendering the log file
* The value seperator to use in the file (default is a pipe)
private $seperator = '|';
* The maximum size of the log file in megabytes before a backup is created and a
* new file is created, default is 5
* Set the max log size in megabytes
* @param integer $MaxSize
$this->MaxSize = $MaxSize;
* Set the value seperator
* @param string $seperator
$this->seperator = $seperator;
* Writes a line of data to the log file
$colCount = count($line);
for($i = 0; $i < $colCount; $i++ ) {
* we need to ensure that the seperator is not in the value anywhere, as it
* would cause problems later when reading the log
$mergedLine .= $value. $this->seperator. "\n";
$mergedLine .= $value. $this->seperator;
if($this->checkFileSize() >= $this->MaxSize) {
$logsDir = $config->get('sysRoot'). 'logs';
if($this->checkFileSize() >= $this->MaxSize) {
echo '<p class="error"><br>Unable to write to the log file ['. $this->path. '], error ['. $e->getMessage(). ']</p>';
* Returns the size in megabytes of the log file on disc
private function checkFileSize() {
return ($size/ 1024)/ 1024;
* Creates a backup of the log file, which has the same file name and location as the
* current file plus a timestamp appended
private function backupFile() {
// generate the name of the backup file name to contain a timestampe
$backName = str_replace('.log', '-backup-'. date("y-m-d H.i.s"). '.log', $this->path);
// renames the logfile as the value of $backName
rename($this->path, $backName);
//creates a new log file, and sets it's permission for writting!
$fp = fopen($this->path, 'a+'); // remember set directory permissons to allow creation!
//sets the new permission to rw+:rw+:rw+
chmod($this->path, 0666);
* Renders a log file as a HTML table
* $param array $cols The headings to use when rendering the log file
// render the start of the table
echo '<table class="log_file">';
foreach($cols as $heading)
echo '<th>'. $heading. '</th>';
// now read the file and render the data
$fields = explode($this->seperator, $LogFile);
for($line = 0; $line < $totalLines; $line++ ) {
for($col = 0; $col < $count; $col++ ) {
$index = ($line* count($cols))+ $col;
// if it is an error log, render the error types field in different colours
if($col == 1 && $cols[1] == 'Level'){
switch($fields[$index]) {
echo '<td class="validation"><pre>'. htmlentities($fields[$index]). '</pre></td>';
echo '<td class="validation"><pre>'. htmlentities($fields[$index]). '</pre></td>';
echo '<td class="warning"><pre>'. htmlentities($fields[$index]). '</pre></td>';
echo '<td class="framework"><pre>'. htmlentities($fields[$index]). '</pre></td>';
echo '<td class="other"><pre>'. htmlentities($fields[$index]). '</pre></td>';
echo '<td><pre>'. htmlentities($fields[$index]). '</pre></td>';
echo '<td><pre>'. htmlentities($fields[$index]). '</pre></td>';
|