PageRenderTime 47ms CodeModel.GetById 22ms 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
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  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. */
  9. namespace Jelix\Logger\Message;
  10. /**
  11. * this class is formatting an error message for a logger
  12. */
  13. class Error implements \Jelix\Logger\MessageInterface {
  14. protected $category;
  15. protected $message;
  16. protected $file;
  17. protected $line;
  18. protected $trace;
  19. protected $code;
  20. protected $format = '%date%\t%ip%\t[%code%]\t%msg%\t%file%\t%line%\n\t%url%\n%params%\n%trace%';
  21. /**
  22. * @param string $category category of the message (error, warning...)
  23. * @param integer $code error code
  24. * @param string $message error message
  25. * @param string $file file path + file name where the error appeared
  26. * @param integer $line the line where the error appeared
  27. * @param array $trace stack trace
  28. */
  29. public function __construct($category, $code, $message, $file, $line, $trace) {
  30. $this->category = $category;
  31. $this->message = $message;
  32. $this->code = $code;
  33. $this->file = $file;
  34. $this->line = $line;
  35. $this->trace = $trace;
  36. }
  37. /**
  38. * set the pattern to format the message output
  39. * @param string $format
  40. */
  41. public function setFormat($format) {
  42. $this->format = $format;
  43. }
  44. /**
  45. * @return string error code
  46. */
  47. public function getCode() {
  48. return $this->code;
  49. }
  50. /**
  51. * @return string category of the message (error, warning...)
  52. */
  53. public function getCategory() {
  54. return $this->category;
  55. }
  56. /**
  57. * @return string error message
  58. */
  59. public function getMessage() {
  60. return $this->message;
  61. }
  62. /**
  63. * @return string file path + file name where the error appeared
  64. */
  65. public function getFile() {
  66. return $this->file;
  67. }
  68. /**
  69. * @return integer the line where the error appeared
  70. */
  71. public function getLine() {
  72. return $this->line;
  73. }
  74. /**
  75. * @return array the stack trace
  76. */
  77. public function getTrace() {
  78. return $this->trace;
  79. }
  80. /**
  81. * @return string formated error message
  82. */
  83. public function getFormatedMessage() {
  84. if (isset($_SERVER['REQUEST_URI']))
  85. $url = $_SERVER['REQUEST_URI'];
  86. elseif(isset($_SERVER['SCRIPT_NAME']))
  87. $url = $_SERVER['SCRIPT_NAME'];
  88. else
  89. $url = 'Unknow request';
  90. // url params including module and action
  91. if (\Jelix\Core\App::router() && ($req = \Jelix\Core\App::router()->request)) {
  92. $params = str_replace("\n", ' ', var_export($req->params, true));
  93. $remoteAddr = $req->getIP();
  94. }
  95. else {
  96. $params = isset($_SERVER['QUERY_STRING'])?$_SERVER['QUERY_STRING']:'';
  97. // When we are in cmdline we need to fix the remoteAddr
  98. $remoteAddr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
  99. }
  100. $traceLog="";
  101. foreach($this->trace as $k=>$t){
  102. $traceLog.="\n\t$k\t".(isset($t['class'])?$t['class'].$t['type']:'').$t['function']."()\t";
  103. $traceLog.=(isset($t['file'])?$t['file']:'[php]').' : '.(isset($t['line'])?$t['line']:'');
  104. }
  105. // referer
  106. $httpReferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Unknown referer';
  107. $messageLog = strtr($this->format, array(
  108. '%date%' => @date("Y-m-d H:i:s"), // @ because if the timezone is not set, we will have an error here
  109. '%typeerror%'=>$this->category,
  110. '%code%' => $this->code,
  111. '%msg%' => $this->message,
  112. '%ip%' => $remoteAddr,
  113. '%url%' => $url,
  114. '%referer%' => $httpReferer,
  115. '%params%'=>$params,
  116. '%file%' => $this->file,
  117. '%line%' => $this->line,
  118. '%trace%' => $traceLog,
  119. '\t' =>"\t",
  120. '\n' => "\n"
  121. ));
  122. return $messageLog;
  123. }
  124. }