source: OWR/Logs.php @ 590811

Revision 590811, 4.2 KB checked in by pierre-alain <pierre-alain@…>, 6 years ago (diff)

for administrator : adding the possibility to display the CLI log file

  • Property mode set to 100644
Line 
1<?php
2/**
3 * Object storing logs (errors or informations)
4 *
5 * PHP 5
6 *
7 * OWR - OpenWebReader
8 *
9 * Copyright (c) 2009, Pierre-Alain Mignot
10 *
11 * Home page: http://openwebreader.org
12 *
13 * E-Mail: contact@openwebreader.org
14 *
15 * All Rights Reserved
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 *
31 * @author Pierre-Alain Mignot <contact@openwebreader.org>
32 * @copyright Copyright (c) 2009, Pierre-Alain Mignot
33 * @license http://www.gnu.org/copyleft/gpl.html
34 * @package OWR
35 */
36namespace OWR;
37/**
38 * This object stores logs
39 * @uses Singleton implements the singleton pattern
40 * @uses Exception the exception handler
41 * @package OWR
42 */
43class Logs extends Singleton
44{
45    /**
46    * @var array the logs
47    * @access private
48    */
49    protected $_logs = array();
50
51    /**
52     * Destructor
53     * Writes the logs if not already done and if not empty
54     *
55     * @author Pierre-Alain Mignot <contact@openwebreader.org>
56     * @access public
57     */
58    public function __destruct()
59    {
60        $this->writeLogs(true);
61    }
62
63    /**
64     * Stores log message in $this->_logs
65     *
66     * @author Pierre-Alain Mignot <contact@openwebreader.org>
67     * @param string $msg the message to log
68     * @access public
69     */
70    public function log($msg, $errcode = Exception::E_OWR_WARNING)
71    {
72        $errcode = (int) $errcode;
73        isset($this->_logs[$errcode]) || $this->_logs[$errcode] = array();
74
75        if(is_array($msg))
76        {
77            foreach($msg as $m)
78                $this->_logs[$errcode][] = (string) $m;
79        }
80        else $this->_logs[$errcode][] = (string) $msg;
81    }
82
83    /**
84     * Returns all logged messages from $this->_logs and empty it
85     *
86     * @author Pierre-Alain Mignot <contact@openwebreader.org>
87     * @return array the logs
88     * @access public
89     */
90    public function getLogs()
91    {
92        $logs = (array) $this->_logs;
93        $this->_logs = array();
94        return $logs;
95    }
96
97    /**
98     * Writes the logs
99     * If in CLI mode, we will write the logs into HOME_PATH/logs/cli.log
100     * else in default php error log file
101     *
102     * @author Pierre-Alain Mignot <contact@openwebreader.org>
103     * @param boolean $write force writting of the logs into the log file
104     * @return boolean true on success
105     * @access public
106     */
107    public function writeLogs($write = false)
108    {
109        if(empty($this->_logs))
110            return;
111
112        $errstr = '';
113        if(CLI)
114        {
115            foreach($this->_logs as $code=>$logs)
116            {
117                $errstr .= "[Log ".(int)$code."]\n".join("\n", $logs)."\n";
118            }
119
120            if(!empty($errstr)) error_log($errstr, 3, HOME_PATH.'logs'.DIRECTORY_SEPARATOR.'cli.log');
121        }
122        else
123        {
124            foreach($this->_logs as $code=>$logs)
125            {
126                $errstr .= "[Log ".(int)$code."]\n".join("\n", $logs)."\n";
127            }
128
129            if(!empty($errstr)) error_log($errstr, 0);
130        }
131        $this->_logs = array();
132    }
133
134    /**
135     * Checks if we got logs
136     *
137     * @author Pierre-Alain Mignot <contact@openwebreader.org>
138     * @return boolean true if logs are not empty
139     * @access public
140     */
141    public function hasLogs()
142    {
143        return !empty($this->_logs);
144    }
145
146    /**
147     * Displays CLI log file
148     *
149     * @author Pierre-Alain Mignot <contact@openwebreader.org>
150     * @return string the content of CLI log file
151     * @access public
152     */
153    public function getCLILogs()
154    {
155        $logPath = HOME_PATH . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . 'cli.log';
156        return is_readable($logPath) ? file_get_contents($logPath) : '';
157    }
158}
Note: See TracBrowser for help on using the repository browser.