1: <?php
2:
3: namespace HZSystem\Core\Logger\Appenders;
4:
5: use HZSystem\Core\Logger\HZLogRow;
6:
7: /*
8: * Copyright (C) 2015 Luca Liscio
9: *
10: * This program is free software: you can redistribute it and/or modify
11: * it under the terms of the GNU Affero General Public License as published by
12: * the Free Software Foundation, either version 3 of the License, or
13: * (at your option) any later version.
14: *
15: * This program is distributed in the hope that it will be useful,
16: * but WITHOUT ANY WARRANTY; without even the implied warranty of
17: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: * GNU Affero General Public License for more details.
19: *
20: * You should have received a copy of the GNU Affero General Public License
21: * along with this program. If not, see <http://www.gnu.org/licenses/agpl-3.0.html>.
22: */
23:
24: /**
25: * File appender per HZLogger
26: *
27: * @author Luca Liscio <hzkight@h0model.org>
28: * @version 0.0.1 2015/12/19 19:20:20
29: * @copyright 2015 Luca Liscio
30: * @license http://www.gnu.org/licenses/agpl-3.0.html GNU/AGPL3
31: *
32: * @package hzSystem
33: * @subpackage Core\Logger\Appenders
34: * @filesource
35: */
36:
37: class Appender_file extends Appender {
38:
39: private $logfile;
40: private $logfile_basename;
41: private $logfile_basedir;
42:
43: /**
44: * Construntor method
45: *
46: * @param String $logname log name
47: */
48: public function __construct($logname){
49:
50: parent::__construct();
51: $this->logfile_basedir = $_SESSION["hzSystem_path"]."log";
52: $this->logfile_basename = $logname;
53: $this->logfile = $this->logfile_basedir.DIRECTORY_SEPARATOR.$this->logfile_basename."_".date("dmY").".log";
54:
55: }
56:
57: /**
58: * Save one row in the log file
59: *
60: * @param HZLogRow $log_row
61: */
62: public function add(HZLogRow $log_row){
63:
64: if($log_row->type >= $this->loglevel)
65: error_log("(".$log_row->date.") [".$this->error_identifier[$log_row->type]."] --> ".$log_row->message."\n",3,$this->logfile);
66:
67: }
68:
69: /**
70: * Return a part of log file
71: *
72: * @param integer $start start row
73: * @param integer $stop end row
74: * @return list of log row
75: * @throws LogFileNotFoundExceprions
76: */
77: public function get_log($start=0,$stop){
78:
79: if (file_exists($this->logfile)) {
80: $log = file($this->logfile);
81: if($stop==null){
82: return array_slice($log, $start);
83: } else {
84: return array_slice($log, $start, $stop-$start);
85: }
86: } else {
87: throw new LogFileNotFoundExceprions(dgettext("hzSystem","Log file not found"));
88: }
89:
90: }
91:
92: /**
93: * change the log directory
94: *
95: * @param string $dir log dirrectory
96: */
97: public function setLogDir($dir){
98: $this->logfile_basedir = $dir;
99: $this->logfile = $this->logfile_basedir.DIRECTORY_SEPARATOR.$this->logfile_basename."_".date("dmY").".log";
100: }
101: }