PageRenderTime 2ms CodeModel.GetById 13ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Jelix/Logger/Message/Error.php

https://github.com/gmarrot/jelix
PHP | 140 lines | 78 code | 16 blank | 46 comment | 4 complexity | f69b0625e937fb7b9c59888e501108f4 MD5 | raw file
  1<?php
  2/**
  3* @author     Laurent Jouanneau
  4* @contributor Brice Tence
  5* @copyright  2006-2014 Laurent Jouanneau, 2011 Brice Tence
  6* @link       http://www.jelix.org
  7* @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  8*/
  9namespace Jelix\Logger\Message;
 10
 11/**
 12 * this class is formatting an error message for a logger
 13 */
 14class Error implements \Jelix\Logger\MessageInterface {
 15    protected $category;
 16    protected $message;
 17    protected $file;
 18    protected $line;
 19    protected $trace;
 20    protected $code;
 21    protected $format = '%date%\t%ip%\t[%code%]\t%msg%\t%file%\t%line%\n\t%url%\n%params%\n%trace%';
 22
 23    /**
 24     * @param string $category category of the message (error, warning...)
 25     * @param integer $code  error code
 26     * @param string $message error message
 27     * @param string $file  file path + file name where the error appeared
 28     * @param integer $line the line where the error appeared
 29     * @param array $trace stack trace
 30     */
 31    public function __construct($category, $code, $message, $file, $line, $trace) {
 32        $this->category = $category;
 33        $this->message = $message;
 34        $this->code = $code;
 35        $this->file = $file;
 36        $this->line = $line;
 37        $this->trace = $trace;
 38    }
 39
 40    /**
 41     * set the pattern to format the message output
 42     * @param string $format
 43     */
 44    public function setFormat($format) {
 45        $this->format = $format;
 46    }
 47
 48    /**
 49     * @return string error code
 50     */
 51    public function getCode() {
 52        return $this->code;
 53    }
 54
 55    /**
 56     * @return string category of the message (error, warning...)
 57     */
 58    public function getCategory() {
 59        return $this->category;
 60    }
 61
 62    /**
 63     * @return string error message
 64     */
 65    public function getMessage() {
 66        return $this->message;
 67    }
 68
 69    /**
 70     * @return string file path + file name where the error appeared
 71     */
 72    public function getFile() {
 73        return $this->file;
 74    }
 75
 76    /**
 77     * @return integer the line where the error appeared
 78     */
 79    public function getLine() {
 80        return $this->line;
 81    }
 82
 83    /**
 84     * @return array the stack trace
 85     */
 86    public function getTrace() {
 87        return $this->trace;
 88    }
 89
 90    /**
 91     * @return string formated error message
 92     */
 93    public function getFormatedMessage() {
 94
 95        if (isset($_SERVER['REQUEST_URI']))
 96            $url = $_SERVER['REQUEST_URI'];
 97        elseif(isset($_SERVER['SCRIPT_NAME']))
 98            $url = $_SERVER['SCRIPT_NAME'];
 99        else
100            $url = 'Unknow request';
101
102        // url params including module and action
103        if (\Jelix\Core\App::router() && ($req = \Jelix\Core\App::router()->request)) {
104            $params = str_replace("\n", ' ', var_export($req->params, true));
105            $remoteAddr = $req->getIP();
106        }
107        else {
108            $params = isset($_SERVER['QUERY_STRING'])?$_SERVER['QUERY_STRING']:'';
109            // When we are in cmdline we need to fix the remoteAddr
110            $remoteAddr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
111        }
112
113        $traceLog="";
114        foreach($this->trace as $k=>$t){
115            $traceLog.="\n\t$k\t".(isset($t['class'])?$t['class'].$t['type']:'').$t['function']."()\t";
116            $traceLog.=(isset($t['file'])?$t['file']:'[php]').' : '.(isset($t['line'])?$t['line']:'');
117        }
118
119        // referer
120        $httpReferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Unknown referer';
121
122        $messageLog = strtr($this->format, array(
123            '%date%' => @date("Y-m-d H:i:s"), // @ because if the timezone is not set, we will have an error here
124            '%typeerror%'=>$this->category,
125            '%code%' => $this->code,
126            '%msg%'  => $this->message,
127            '%ip%'   => $remoteAddr,
128            '%url%'  => $url,
129            '%referer%'  => $httpReferer,
130            '%params%'=>$params,
131            '%file%' => $this->file,
132            '%line%' => $this->line,
133            '%trace%' => $traceLog,
134            '\t' =>"\t",
135            '\n' => "\n"
136        ));
137
138        return $messageLog;
139    }
140}