Skip to content

Commit ae668e0

Browse files
author
Dmitry Tarasov
committed
Initial commit: forming log objects, common logic and Monolog formatter
1 parent 4e19706 commit ae668e0

11 files changed

Lines changed: 653 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Stackify\Exceptions;
4+
5+
class InitializationException extends StackifyException {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Stackify\Exceptions;
4+
5+
class StackifyException extends \Exception {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Stackify\Log\Entities;
4+
5+
class ErrorItem
6+
{
7+
/**
8+
* @var string
9+
*/
10+
public $Message;
11+
12+
/**
13+
* @var string
14+
*/
15+
public $ErrorTypeCode;
16+
17+
/**
18+
* @var array Key-value pairs
19+
*/
20+
public $Data = array();
21+
22+
/**
23+
* @var string
24+
*/
25+
public $SourceMethod;
26+
27+
/**
28+
* @var \Stackify\Log\Entities\TraceFrame[]
29+
*/
30+
public $StackTrace = array();
31+
32+
/**
33+
* @var \Stackify\Log\Entities\ErrorItem
34+
*/
35+
public $InnerError;
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Stackify\Log\Entities;
4+
5+
class LogMsg
6+
{
7+
/**
8+
* @var string
9+
*/
10+
public $Msg;
11+
12+
/**
13+
* @var string (JSON)
14+
*/
15+
public $data;
16+
17+
/**
18+
* @var \Stackify\Log\Entities\StackifyError
19+
*/
20+
public $Ex;
21+
22+
/**
23+
* @var string
24+
*/
25+
public $Th;
26+
27+
/**
28+
* @var integer
29+
*/
30+
public $EpochMs;
31+
32+
/**
33+
* @var string
34+
*/
35+
public $Level;
36+
37+
/**
38+
* @var string
39+
*/
40+
public $TransID;
41+
42+
/**
43+
* @var string
44+
*/
45+
public $SrcMethod;
46+
47+
/**
48+
* @var integer
49+
*/
50+
public $SrcLine;
51+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Stackify\Log\Entities;
4+
5+
class StackifyError
6+
{
7+
/**
8+
* @var null Environment details are not known by PHP loggers
9+
* @TODO is it really unknown?
10+
*/
11+
public $EnvironmentDetail;
12+
13+
/**
14+
* @var integer Milliseconds
15+
*/
16+
public $OccurredEpochMillis;
17+
18+
/**
19+
* @var \Stackify\Log\Entities\ErrorItem
20+
*/
21+
public $Error;
22+
23+
/**
24+
* @var \Stackify\Log\Entities\WebRequestDetail
25+
*/
26+
public $WebRequestDetail;
27+
28+
/**
29+
* @var array Key-value pairs
30+
*/
31+
public $ServerVariables = array();
32+
33+
/**
34+
* @var string
35+
*/
36+
public $CustomerName;
37+
38+
/**
39+
* @var string
40+
*/
41+
public $UserName;
42+
43+
public function __construct()
44+
{
45+
$this->WebRequestDetail = new WebRequestDetail();
46+
$this->ServerVariables = $this->getEnvironmentVariables();
47+
}
48+
49+
private function getEnvironmentVariables()
50+
{
51+
return isset($_ENV) ? $this->WebRequestDetail->getRequestMap($_ENV) : array();
52+
}
53+
54+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Stackify\Log\Entities;
4+
5+
class TraceFrame
6+
{
7+
/**
8+
* @var string
9+
*/
10+
public $CodeFileName;
11+
12+
/**
13+
* @var integer
14+
*/
15+
public $LineNum;
16+
17+
/**
18+
* @var string
19+
*/
20+
public $Method;
21+
22+
public function __construct($file, $line, $method)
23+
{
24+
$this->CodeFileName = $file;
25+
$this->LineNum = $line;
26+
$this->Method = $method;
27+
}
28+
29+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
3+
namespace Stackify\Log\Entities;
4+
5+
class WebRequestDetail
6+
{
7+
8+
const HIDDEN_VALUE = 'X-MASKED-X';
9+
10+
/**
11+
* @var string
12+
*/
13+
public $UserIPAddress;
14+
15+
/**
16+
* @var string
17+
*/
18+
public $HttpMethod;
19+
20+
/**
21+
* @var string
22+
*/
23+
public $RequestProtocol;
24+
25+
/**
26+
* @var string
27+
*/
28+
public $RequestUrl;
29+
30+
/**
31+
* @var string
32+
*/
33+
public $RequestUrlRoot;
34+
35+
/**
36+
* @var string
37+
*/
38+
public $ReferralUrl;
39+
40+
/**
41+
* @var array Key-value pairs
42+
*/
43+
public $Headers = array();
44+
45+
/**
46+
* @var array Key-value pairs
47+
*/
48+
public $Cookies = array();
49+
50+
/**
51+
* @var array Key-value pairs
52+
*/
53+
public $QueryString = array();
54+
55+
/**
56+
* @var array Key-value pairs
57+
*/
58+
public $PostData = array();
59+
60+
/**
61+
* @var array Key-value pairs
62+
*/
63+
public $SessionData = array();
64+
65+
/**
66+
* @var string
67+
*/
68+
public $PostDataRaw;
69+
70+
/**
71+
* @var string
72+
*/
73+
public $MVCAction;
74+
75+
/**
76+
* @var string
77+
*/
78+
public $MVCController;
79+
80+
/**
81+
* @var string
82+
*/
83+
public $MVCArea;
84+
85+
public function __construct() {
86+
$this->UserIPAddress = $this->getIpAddress();
87+
$this->HttpMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD');
88+
$this->RequestProtocol = $this->getProtocol();
89+
$this->RequestUrl = $this->getRequestUrl();
90+
$this->RequestUrlRoot = filter_input(INPUT_SERVER, 'SERVER_NAME');
91+
$this->ReferralUrl = filter_input(INPUT_SERVER, 'HTTP_REFERER');
92+
$this->Headers = $this->getHeaders();
93+
$this->Cookies = isset($_COOKIE) ? $this->getRequestMap($_COOKIE, true) : array();
94+
$this->QueryString = isset($_GET) ? $this->getRequestMap($_GET) : array();
95+
$this->PostData = isset($_POST) ? $this->getRequestMap($_POST) : array();
96+
$this->SessionData = isset($_SESSION) ? $this->getRequestMap($_SESSION, true) : array();
97+
$this->PostDataRaw = file_get_contents('php://input');
98+
}
99+
100+
/**
101+
* Converts $data to key-value pairs, where values are strings
102+
* @param mixed $data
103+
* @param boolean $maskValues Hide request values
104+
* @return array
105+
*/
106+
public function getRequestMap($data, $maskValues = false)
107+
{
108+
$result = array();
109+
if (is_array($data)) {
110+
foreach ($data as $key => $value) {
111+
$result[$key] = $maskValues
112+
? self::HIDDEN_VALUE
113+
: $this->stringify($value);
114+
}
115+
}
116+
return $result;
117+
}
118+
119+
private function getIpAddress() {
120+
$keys = array(
121+
'HTTP_CLIENT_IP',
122+
'HTTP_X_FORWARDED_FOR',
123+
'HTTP_X_FORWARDED',
124+
'HTTP_X_CLUSTER_CLIENT_IP',
125+
'HTTP_FORWARDED_FOR',
126+
'HTTP_FORWARDED',
127+
'REMOTE_ADDR',
128+
);
129+
foreach ($keys as $key) {
130+
$ip = filter_input(INPUT_SERVER, $key);
131+
if (null !== $ip && false !== filter_var($ip, FILTER_VALIDATE_IP)) {
132+
return $ip;
133+
}
134+
}
135+
return null;
136+
}
137+
138+
private function getProtocol()
139+
{
140+
$protocol = null;
141+
if ('cli' === php_sapi_name()) {
142+
$protocol = 'CLI';
143+
} else {
144+
$protocol = filter_input(INPUT_SERVER, 'SERVER_PROTOCOL');
145+
}
146+
return $protocol;
147+
}
148+
149+
private function getRequestUrl()
150+
{
151+
$https = filter_input(INPUT_SERVER, 'HTTPS');
152+
$ssl = null !== $https && 'off' !== $https;
153+
$protocol = $ssl ? 'https' : 'http';
154+
list($url,) = explode('?', filter_input(INPUT_SERVER, 'REQUEST_URI'));
155+
$serverName = filter_input(INPUT_SERVER, 'SERVER_NAME');
156+
if ($serverName && $url) {
157+
return "$protocol://$serverName" . $url;
158+
}
159+
return null;
160+
}
161+
162+
private function getHeaders()
163+
{
164+
$headers = array();
165+
if (function_exists('getallheaders')) {
166+
$headers = getallheaders();
167+
if (false === $headers) {
168+
// getallheaders() returns false in case of error
169+
$headers = array();
170+
}
171+
}
172+
foreach ($headers as $name => $value) {
173+
if ('cookie' === strtolower($name)) {
174+
$headers[$name] = self::HIDDEN_VALUE;
175+
}
176+
}
177+
return $headers;
178+
}
179+
180+
/**
181+
* Converts any PHP type to string
182+
* @param mixed $value
183+
* @return string
184+
*/
185+
private function stringify($value)
186+
{
187+
$string = '';
188+
if (is_scalar($value)) {
189+
// integer, float, string, boolean
190+
$string = (string)$value;
191+
} elseif (is_resource($value)) {
192+
// resource
193+
$string = '[resource]';
194+
} else {
195+
// array, object, null, callable
196+
$string = json_encode($value);
197+
}
198+
return $string;
199+
}
200+
201+
}

0 commit comments

Comments
 (0)