-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPlugin.php
199 lines (177 loc) · 5.53 KB
/
Plugin.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace VojtaSvoboda\ErrorLogger;
use Backend\Facades\BackendAuth;
use Config;
use Log;
use VojtaSvoboda\ErrorLogger\Models\Settings;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Handler\NewRelicHandler;
use Monolog\Handler\SlackHandler;
use Monolog\Handler\SyslogHandler;
use Monolog\Logger;
use System\Classes\PluginBase;
/**
* Error Logger Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'vojtasvoboda.errorlogger::lang.plugin.name',
'description' => 'vojtasvoboda.errorlogger::lang.plugin.description',
'author' => 'Vojta Svoboda',
'icon' => 'icon-bug',
];
}
public function registerSettings()
{
return [
'config' => [
'label' => 'vojtasvoboda.errorlogger::lang.settings.label',
'category' => 'system::lang.system.categories.system',
'icon' => 'icon-bug',
'description' => 'vojtasvoboda.errorlogger::lang.settings.description',
'class' => 'VojtaSvoboda\ErrorLogger\Models\Settings',
'permissions' => ['vojtasvoboda.errorlogger.*'],
'order' => 610,
]
];
}
public function registerPermissions()
{
return [
'vojtasvoboda.errorlogger.*' => [
'tab' => 'vojtasvoboda.errorlogger::lang.permissions.tab',
'label' => 'vojtasvoboda.errorlogger::lang.permissions.all.label'
]
];
}
/**
* Boot plugin and register handlers
*/
public function boot()
{
$isLaravel56OrUp = method_exists(\Illuminate\Log\Logger::class, 'getLogger');
$monolog = $isLaravel56OrUp ? Log::getLogger() : Log::getMonolog();
$this->setNativeMailerHandler($monolog);
$this->setSlackHandler($monolog);
$this->setSyslogHandler($monolog);
$this->setNewrelicHandler($monolog);
}
/**
* Set native mailer handler
*
* Formatting lines example (use before pushHandler()):
* $formater = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n");
* $handler->setFormatter($formater);
*
* @param $monolog
*
* @return Logger
*/
private function setNativeMailerHandler($monolog)
{
$required = ['nativemailer_enabled', 'nativemailer_email'];
if (!$this->checkRequiredFields($required)) {
return $monolog;
}
// disable when debug is true
$debug = Settings::get('nativemailer_debug');
if ($debug & Config::get('app.debug')) {
return $monolog;
}
$email = Settings::get('nativemailer_email');
$subject = Config::get('app.url') . ' - error report';
$from = Config::get('mail.from.address');
$level = Settings::get('nativemailer_level', 100);
$handler = new NativeMailerHandler($email, $subject, $from, $level);
$monolog->pushHandler($handler);
return $monolog;
}
/**
* Set handler for Slack messaging app
*
* @param $monolog
*
* @return Logger
*/
private function setSlackHandler($monolog)
{
$required = ['slack_enabled', 'slack_token'];
if (!$this->checkRequiredFields($required)) {
return $monolog;
}
$token = Settings::get('slack_token');
$channel = Settings::get('slack_channel', 'random');
$username = Settings::get('slack_username', 'error-bot');
$attachment = Settings::get('slack_attachment', false);
$level = Settings::get('slack_level', 100);
$handler = new SlackHandler($token, $channel, $username, $attachment, null, $level);
$monolog->pushHandler($handler);
return $monolog;
}
/**
* Set handler for Syslog
*
* @param $monolog
*
* @return Logger
*/
private function setSyslogHandler($monolog)
{
$required = ['syslog_enabled', 'syslog_ident', 'syslog_facility'];
if (!$this->checkRequiredFields($required)) {
return $monolog;
}
$ident = Settings::get('syslog_ident');
$facility = Settings::get('syslog_facility');
$level = Settings::get('syslog_level', 100);
$handler = new SyslogHandler($ident, $facility, $level);
$monolog->pushHandler($handler);
return $monolog;
}
/**
* Set handler for New Relic
*
* @param $monolog
*
* @return Logger
*/
private function setNewrelicHandler($monolog)
{
$required = ['newrelic_enabled', 'newrelic_appname'];
if (!$this->checkRequiredFields($required)) {
return $monolog;
}
$appname = Settings::get('newrelic_appname');
$level = Settings::get('newrelic_level', 100);
$bubble = true;
$handler = new NewRelicHandler($level, $bubble, $appname);
$monolog->pushHandler($handler);
return $monolog;
}
/**
* Check each required field if exist and not empty
*
* @param array $fields
*
* @return bool
*/
private function checkRequiredFields(array $fields)
{
foreach ($fields as $field) {
$value = Settings::get($field);
if (!$value || empty($value)) {
return false;
}
}
return true;
}
}