Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

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