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