Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

  • Log_Writer_Abstract
  • Log_Writer_Bd
  • Log_Writer_File
  • Log_Writer_Syslog
  • Overview
  • Package
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * Pry Framework
 4:  *
 5:  * LICENSE
 6:  *
 7:  * This source file is subject to the new BSD license that is bundled
 8:  * with this package in the file LICENSE.txt.
 9:  * 
10:  * @version $Revision: 276 $
11:  */
12: 
13: /**
14:  * Class d'écriture de log dans les fichiers
15:  * 
16:  * @package Log
17:  * @subpackage Log_Writer
18:  * @version 1.2.0
19:  * @author Olivier ROGER <oroger.fr>
20:  *       
21:  */
22: class Log_Writer_File extends Log_Writer_Abstract
23: {
24:     private $folder;
25:     private $lineLimit;
26:     
27:     public function __construct($path)
28:     {
29:         if(!is_dir($path) || !is_writable($path))
30:             throw new InvalidArgumentException('Le dossier spécifié n\'existe pas ou n\'est pas ouvert en écriture');
31:         else
32:         {
33:             if($path[strlen($path)-1] != '/')
34:                 $path.='/';
35:         }
36:         $this->folder       = $path;
37:         $this->lineLimit    = 50;
38:     }
39: 
40:     /**
41:      * Défini le nombre de ligne que contiendra le fichier de log. 0 pour aucune limite
42:      * @since 1.1.0
43:      * @param int $lines Nombre limite de ligne
44:      */
45:     public function setLineLimit($lines)
46:     {
47:         $this->lineLimit = intval($lines);
48:     }
49: 
50:     /**
51:      * Log du message
52:      *
53:      * @param string $message
54:      * @param string $level
55:      * @access public
56:      */
57:     protected function _write($message,$level)
58:     {
59:         $prefixe        = ($this->mode == self::MODE_MINI)?'':'['.date("d/m/Y - H:i:s").'] ('.$this->txtSeverity[$level].') ';
60:         
61:         $file           = $this->txtSeverity[$level].'.log';
62:         $prefixe_file   = $this->getPrefixe();
63:         $filepath       = $this->folder.$prefixe_file.$file;
64: 
65:         if(!is_file($filepath))
66:             touch($filepath);
67: 
68:         if($this->lineLimit > 0)
69:         {
70:             $logsContent = file($filepath);
71:             if(count($logsContent) >= $this->lineLimit)
72:             {
73:                 $logsContent[] = $prefixe.$message."\n";
74:                 array_shift($logsContent);
75:                 file_put_contents($filepath,$logsContent);
76:             }
77:             else
78:             {
79:                 $h = fopen($filepath,'a');
80:                 fwrite($h,$prefixe.$message."\n");
81:                 fclose($h);
82:             }
83:         }
84:         else
85:         {
86:             $h = fopen($filepath,'a');
87:             fwrite($h,$prefixe.$message."\n");
88:             fclose($h);
89:         }
90:     }
91: 
92: }
93:  ?>
Pry Framework API documentation generated by ApiGen 2.6.1