1: <?php
 2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 
12: 
13: 14: 15: 16: 17: 18: 19: 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: 42: 43: 44: 
45:     public function setLineLimit($lines)
46:     {
47:         $this->lineLimit = intval($lines);
48:     }
49: 
50:     51: 52: 53: 54: 55: 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:  ?>