-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMessageBuilder.php
More file actions
130 lines (116 loc) · 4.24 KB
/
Copy pathMessageBuilder.php
File metadata and controls
130 lines (116 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Stackify\Log\Builder;
use Stackify\Log\Entities\Api\LogMsg;
use Stackify\Log\Entities\Api\ErrorItem;
use Stackify\Log\Entities\Api\TraceFrame;
use Stackify\Log\Entities\Api\StackifyError;
use Stackify\Log\Entities\Api\EnvironmentDetail;
use Stackify\Log\Entities\ErrorWrapper;
use Stackify\Log\Entities\LogEntryInterface;
use Stackify\Log\Entities\Api\LogMsgGroup;
use Stackify\Log\Entities\Agent\AgentMessage;
use Stackify\Exceptions\InitializationException;
class MessageBuilder implements BuilderInterface
{
protected $loggerName;
protected $appName;
protected $environmentName;
protected $logServerVariables;
public function __construct($loggerName, $appName, $environmentName = null, $logServerVariables = false)
{
if (!function_exists('json_encode')) {
throw new InitializationException('JSON extension is required for Stackify logger');
}
$this->loggerName = $loggerName;
$this->appName = $this->validateNotEmpty('AppName', $appName);
$this->environmentName = $environmentName;
$this->logServerVariables = $logServerVariables;
// set state for environment details
EnvironmentDetail::getInstance()->init($appName, $environmentName);
}
public function getAgentMessage(LogMsg $logMsg)
{
$message = new AgentMessage($this->loggerName, $this->appName, $this->environmentName, $logMsg);
return $this->encodeJSON($message). PHP_EOL;
}
/**
* @param \Stackify\Log\Entities\Api\LogMsg[] $logMsgs
*/
public function getApiMessage(array $logMsgs)
{
$message = new LogMsgGroup(
$this->loggerName,
$this->appName,
$this->environmentName,
$logMsgs
);
return $this->encodeJSON($message);
}
/**
* @return \Stackify\Log\Entities\Api\LogMsg
*/
public function createLogMsg(LogEntryInterface $logEntry)
{
$logMsg = new LogMsg(
$logEntry->getLevel(),
$logEntry->getMessage(),
$logEntry->getMilliseconds()
);
$errorWrapper = null;
if ($exception = $logEntry->getException()) {
$errorWrapper = new ErrorWrapper($exception);
} elseif ($nativeError = $logEntry->getNativeError()) {
$errorWrapper = new ErrorWrapper($nativeError);
} elseif ($logEntry->isErrorLevel()) {
$errorWrapper = new ErrorWrapper($logEntry);
}
if (null !== $errorWrapper) {
$error = new StackifyError($this->appName, $this->environmentName, $this->logServerVariables);
$error->OccurredEpochMillis = $logEntry->getMilliseconds();
$error->Error = $this->getErrorItem($errorWrapper);
$logMsg->setError($error);
}
if (null !== $logEntry->getContext()) {
$logMsg->data = $this->encodeJSON($logEntry->getContext());
}
return $logMsg;
}
/**
* @return \Stackify\Log\Entities\Api\ErrorItem
*/
protected function getErrorItem(ErrorWrapper $errorWrapper)
{
$errorItem = new ErrorItem();
$errorItem->Message = $errorWrapper->getMessage();
$errorItem->ErrorType = $errorWrapper->getType();
$errorItem->ErrorTypeCode = $errorWrapper->getCode();
$errorItem->SourceMethod = $errorWrapper->getSourceMethod();
$errorWrapperTrace = $errorWrapper->getTrace();
if ($errorWrapperTrace && is_array($errorWrapperTrace)) {
foreach ($errorWrapper->getTrace() as $index => $trace) {
$errorItem->StackTrace[] = new TraceFrame(
$trace['file'],
$trace['line'],
$trace['function']
);
}
}
$previous = $errorWrapper->getInnerError();
if ($previous) {
$errorItem->InnerError = $this->getErrorItem($previous);
}
return $errorItem;
}
protected function encodeJSON($data)
{
return json_encode($data);
}
protected function validateNotEmpty($name, $value)
{
$result = trim($value);
if (empty($result)) {
throw new InitializationException("$name cannot be empty");
}
return $result;
}
}