Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.LogException.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp\Core;
8 
10 {
11 
12  protected static $errId;
13 
14  public static function getId()
15  {
16  return self::$errId;
17  }
18  /**
19  * @param \Throwable $e
20  *
21  * @param string $errid
22  *
23  * @return void
24  */
25  public static function writeLog($e, $errid = null)
26  {
27  if ($errid === null) {
28  $errorId = self::$errId;
29  } else {
30  $errorId = $errid;
31  }
32 
33  self::writeLogMsg(self::formatErrorLogException($e, $errorId));
34  }
35 
36 
37  public static function writeLogMsg($msg)
38  {
39  error_log($msg);
40  }
41 
42  /**
43  * Return Exception message with call stack
44  *
45  * @param \Throwable $e
46  * @param string $errorId
47  *
48  * @return string
49  */
50  public static function formatErrorLogException($e, $errorId = "DCP")
51  {
52  global $argv;
53 
54  $pid = getmypid();
55 
56  $errLine[] = sprintf("[%s] %s> Dynacase got an uncaught exception '%s' with message '%s' in file %s at line %s:", $errorId, $pid, get_class($e) , $e->getMessage() , $e->getFile() , $e->getLine());
57 
58  $errLine[] = self::getUserInfo();
59 
60  if (php_sapi_name() == 'cli' && is_array($argv)) {
61  $errLine[] = sprintf("%s> Command line arguments: %s", $pid, join(' ', array_map("escapeshellarg", $argv)));
62  $errLine[] = sprintf("%s> error_log: %s", $pid, ini_get('error_log'));
63  }
64  foreach (preg_split('/\n/', $e->getTraceAsString()) as $line) {
65  $errLine[] = sprintf("%s> %s", $pid, $line);
66  }
67  $errLine[] = sprintf("[%s] %s> End Of Exception.", $errorId, $pid);
68  return implode("\n", $errLine);
69  }
70 
71  protected static function getUserInfo()
72  {
73  $u = getCurrentUser();
74  if ($u) {
75  return sprintf("User : <%s> \"%s %s\" [%d]", $u->login, $u->firstname, $u->lastname, $u->id);
76  }
77  return null;
78  }
79  /**
80  * Log message according to CORE_DISPLAY_ERROR parameter
81  *
82  * @param \Exception|array $e Exception or Error info done by error_get_last()
83  *
84  * @param string $errorId
85  *
86  * @return string
87  */
88  public static function logMessage($e, &$errorId)
89  {
90  $msg = self::getMessage($e, $errorId, $logMsg);
91 
92  if (is_array($e)) {
93  self::writeLogMsg(sprintf("[%s] %s", $errorId, $logMsg));
94  } else {
95  self::writeLogMsg(self::formatErrorLogException($e, $errorId));
96  }
97 
98  return $msg;
99  }
100  /**
101  * Return message according to CORE_DISPLAY_ERROR parameter
102  *
103  * @param \Exception|array $e
104  *
105  * @param string $errorId return error identifier to be used in request message and log message
106  *
107  * @param string $logMessage complete message
108  *
109  * @return string anonymous message error if CORE_DISPLAY_ERROR is yes
110  */
111  public static function getMessage($e, &$errorId, &$logMessage = "")
112  {
113  global $action;
114  $errorId = uniqid("DCP");
115  self::$errId = $errorId;
116 
117  if (is_array($e)) {
118  $logMessage = sprintf("%s in %s on line %s", $e["message"], $e["file"], $e["line"]);
119  $logMessage.= "\n" . self::getUserInfo();
120  } else {
121  $logMessage = $e->getMessage();
122  }
123 
124  $displayError = (!empty($action) && $action->getParam("CORE_DISPLAY_ERROR") === "yes");
125 
126  if (!$displayError) {
127  return sprintf("%s.", ___("Whoops, looks like something went wrong", "dcp"));
128  } else {
129  return $logMessage;
130  }
131  }
132 }
global $action
static formatErrorLogException($e, $errorId="DCP")
if(!function_exists('pgettext')) ___($message, $context="")
Definition: Lib.Common.php:46
static getMessage($e, &$errorId, &$logMessage="")
static writeLog($e, $errid=null)
static logMessage($e, &$errorId)
formatErrorLogException($e)
Definition: Lib.Main.php:548
getCurrentUser()
Definition: Lib.Common.php:250
← centre documentaire © anakeen