Issues (762)

plugins/rollbar/classes/rollbarloggingprovider.php (5 issues)

1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: RocketCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 21.08.2018
25
 * Time: 20:25
26
 */
27
28
namespace Plugin\Rollbar;
29
30
use \Rollbar\Rollbar;
31
use \Rollbar\Payload\Level;
32
use LogProvider;
33
use Preferences;
34
use LogLevel;
35
36
if (!defined('ROLLBAR_SDK_DIR')) {
37
	define('ROLLBAR_SDK_DIR', dirname(__FILE__) . "/../rollbar-php-1.6.2/");
38
}
39
40
if (!defined('PSR_SDK_DIR')) {
41
	define('PSR_SDK_DIR', dirname(__FILE__) . "/../log-1.0.1/");
42
}
43
44
if (!defined('MONOLOG_SDK_DIR')) {
45
	define('MONOLOG_SDK_DIR', dirname(__FILE__) . "/../monolog-1.23.0/src/");
46
}
47
48
class RollbarLoggingProvider implements LogProvider {
49
50
	protected $logs = array();
51
52
	/**
53
	 * initialize logging provider
54
	 */
55
	public function init () {
56
		$preferences = new Preferences("plugin_rollbar");
57
		$access_token = $preferences->get("access_token", "none");
58
		$environment = $preferences->get("environment", "development");
59
60
		if ($access_token === "none") {
61
			//access token wasnt set yet
62
			return;
63
		}
64
65
		Rollbar::init(
66
			array(
67
				'access_token' => $access_token,
68
				'environment' => $environment,
69
70
				// optional - path to directory your code is in. used for linking stack traces.
71
				//'root' => ROOT_PATH,
72
				'included_errno' => E_ALL//Note: If you wish to log E_NOTICE errors make sure to pass 'included_errno' => E_ALL to Rollbar::init
73
			),
74
			true,
75
			true,
76
			true
77
		);
78
	}
79
80
	/**
81
	 * log message
82
	 */
83
	public function log (string $level, string $message, $args = array()) {
84
		$this->logs[] = array(
85
			'level' => $level,
86
			'message' => $message,
87
			'args' => $args
88
		);
89
90
		if ($level === LogLevel::ERROR || $level === LogLevel::CRITICAL) {
91
			//send it directly to rollbar server
92
			$this->send();
93
		}
94
	}
95
96
	/**
97
	 * lazy logging - after generating page write logs to file or send them to server
98
	 */
99
	public function send () {
100
		if (empty($this->logs)) {
101
			//we dont have to send anything
102
			return;
103
		}
104
105
		foreach ($this->logs as $entry) {
106
			//send log to server
107
			$response = Rollbar::log(
108
				$entry['level'],
109
				$entry['message'],
110
				$entry['args'] // key-value additional data
111
			);
112
113
			if (!$response->wasSuccessful()) {
114
				throw new \IllegalStateException('logging with Rollbar failed');
115
			}
116
		}
117
118
		//clear logs array
119
		$this->logs = array();
120
	}
121
122
	public static function addRollbarClassloader (array $params) {
0 ignored issues
show
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

122
	public static function addRollbarClassloader (/** @scrutinizer ignore-unused */ array $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
		//add classloader for psr/log composer package
124
		\ClassLoader::addLoader("Psr", function (string $class_name) {
125
			$path = PSR_SDK_DIR . str_replace("\\", "/", $class_name) . ".php";
126
127
			if (file_exists($path)) {
128
				require($path);
129
			} else {
130
				echo "Couldnt load psr class: " . $class_name . " (expected path: " . $path . ")!";
131
				exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
132
			}
133
		});
134
135
		//add classloader for monolog/monolog composer package
136
		\ClassLoader::addLoader("Monolog", function (string $class_name) {
137
			$path = MONOLOG_SDK_DIR . str_replace("\\", "/", $class_name) . ".php";
138
139
			if (file_exists($path)) {
140
				require($path);
141
			} else {
142
				echo "Couldnt load monolog class: " . $class_name . " (expected path: " . $path . ")!";
143
				exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
144
			}
145
		});
146
147
		//add classloader for rollbar sdk
148
		\ClassLoader::addLoader("Rollbar", function (string $class_name) {
149
			$path = ROLLBAR_SDK_DIR . str_replace("\\", "/", $class_name) . ".php";
150
151
			if (file_exists($path)) {
152
				require($path);
153
			} else {
154
				echo "Couldnt load rollbar class: " . $class_name . " (expected path: " . $path . ")!";
155
				exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
156
			}
157
		});
158
	}
159
160
}
161
162
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
163