-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.php
167 lines (143 loc) · 5.17 KB
/
Log.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace omcrn\unitedlogs;
use Exception;
/**
* Created by PhpStorm.
* User: guga
* Date: 5/8/17
* Time: 11:16 AM
*/
class Log
{
const DOMAIN_KEY_IN_ENV = 'UNITED_LOGS_DOMAIN';
private $levels = array('warning', 'info', 'error', 'success');
private $key;
private $environment;
private $domain;
const LEVEL_WARNING = 'warning';
const LEVEL_ERROR = 'error';
const LEVEL_INFO = 'info';
const LEVEL_SUCCESS = 'success';
/**
* Log constructor. Initializes logger object.
*
*
* @param string $key Project key. 64 bit length string associated to project
* @param string $environment For better filtering different modes: development, testing, production. You can put any text you want
* @param array|null $levels
* @param string|null $domain The endpoint of United Logs server. If you have it on your server, please specify domain or ip.
* You can put the domain in $_ENV['UNITED_LOGS_DOMAIN'] and not specify here
* @throws Exception
*/
public function __construct($key, $environment, $levels = null, $domain = null)
{
if (!$key) {
throw new Exception('API Key not specified');
}
if (!$environment) {
throw new Exception('Environment not specified');
}
$this->key = $key;
$this->environment = $environment;
if ($levels !== null){
$this->levels = $levels;
}
if ($domain === null && !isset($_ENV[self::DOMAIN_KEY_IN_ENV])){
throw new Exception('Please put the domain in global $_ENV variable with key "'.self::DOMAIN_KEY_IN_ENV.'" or specify it when constructing '.get_class($this).' object.');
}
$this->domain = $domain ? ($domain . '/api/v1/log') : $_ENV[self::DOMAIN_KEY_IN_ENV].'/api/v1/log';
}
/**
* Write error log. It accepts message, category and optional params.
* Return whether or not the log has been successfully written.
*
* @author Zura Sekhniashvili <[email protected]>
* @param string $message
* @param string $category
* @param array|null $params
* @return bool
*/
public function error($message, $category, $params = null)
{
return $this->sendLog(self::LEVEL_ERROR, $message, $category, $params);
}
/**
* Write success log. It accepts message, category and optional params.
* Return whether or not the log has been successfully written.
*
* @author Zura Sekhniashvili <[email protected]>
* @param string $message
* @param string $category
* @param array|null $params
* @return bool
*/
public function success($message, $category, $params = null)
{
return $this->sendLog(self::LEVEL_SUCCESS, $message, $category, $params);
}
/**
* Write info log. It accepts message, category and optional params.
* Return whether or not the log has been successfully written.
*
* @author Zura Sekhniashvili <[email protected]>
* @param string $message
* @param string $category
* @param array|null $params
* @return bool
*/
public function info($message, $category, $params = null)
{
return $this->sendLog(self::LEVEL_INFO, $message, $category, $params);
}
/**
* Write warning log. It accepts message, category and optional params.
* Return whether or not the log has been successfully written.
*
* @author Zura Sekhniashvili <[email protected]>
* @param string $message
* @param string $category
* @param array|null $params
* @return bool
*/
public function warning($message, $category, $params = null)
{
return $this->sendLog(self::LEVEL_WARNING, $message, $category, $params);
}
/**
* Send log message
*
* @author Zura Sekhniashvili <[email protected]>
* @param string $level
* @param string $message
* @param string $category
* @param array $params
* @return bool
*/
private function sendLog($level, $message, $category, $params)
{
if (!in_array($level, array(self::LEVEL_WARNING, self::LEVEL_INFO, self::LEVEL_SUCCESS, self::LEVEL_ERROR))){
return false;
}
$data = array(
'api' => $this->key,
'environment' => $this->environment,
'message' => $message,
'category' => $category,
'params' => $params
);
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $this->domain.'/'.$level);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HEADER, "Content-type: application/x-www-form-urlencoded");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$result = json_decode($server_output, true);
return $result['success'];
}
}