phpPoA2
[ class tree: phpPoA2 ] [ index: phpPoA2 ] [ all elements ]

Source for file PoALog.php

Documentation is available at PoALog.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2010 RedIRIS, http://www.rediris.es/
  4.  *
  5.  *  This file is part of phpPoA2.
  6.  *
  7.  *  phpPoA2 is free software: you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation, either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  phpPoA2 is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  21.  * @version 2.0
  22.  * @author Jaime Perez <jaime.perez@rediris.es>
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * A simple class to write log messages to a file.
  28.  * @package phpPoA2
  29.  */
  30. class PoALog {
  31.     private $level;
  32.     private $file;
  33.  
  34.     /**
  35.      * Build a new logger.
  36.      * @param level The level of this log.
  37.      * @param file The file where to write messages.
  38.      */
  39.     public function __construct($level$file{
  40.         $this->level $level;
  41.         if (!file_exists($file)) {
  42.             if (!$tmp @fopen($file'a')) {
  43.                 error_log(PoAUtils::msg('cannot-open-log'array($file)));
  44.             }
  45.             @fclose($tmp);
  46.         }
  47.         if (!is_writable($file)) {
  48.             error_log(PoAUtils::msg('cannot-write-log'array($file)));
  49.         }
  50.         $this->file $file;
  51.     }
  52.  
  53.     /**
  54.      * Write a message to the log file if the current log level allows it.
  55.      * @param msg The message to write.
  56.      * @param level The level of the message.
  57.      */
  58.     public function write($msg$level E_USER_NOTICE{
  59.         if ($level <= $this->level{
  60.             $file @fopen($this->file'a');
  61.             if (!$b @fwrite($file"[".date("D M d H:i:s Y")."] ".$msg."\n")) {
  62.                 error_log(PoAUtils::msg('cannot-write-log'array($this->file)));
  63.             }
  64.             @fclose($file);
  65.         }
  66.     }
  67.  
  68.     /**
  69.      * Get the current log level.
  70.      * @return integer The constant describing the current log level.
  71.      */
  72.     public function getLogLevel({
  73.         return $this->level;
  74.     }
  75. }
  76.  
  77. ?>

Documentation generated on Tue, 14 Jun 2011 12:22:18 +0200 by phpDocumentor 1.4.3