Skip to content

Commit

Permalink
Merge pull request #28 from oleg-andreyev/absolute-url
Browse files Browse the repository at this point in the history
Always generate absolute url for logging
  • Loading branch information
Seldaek authored Mar 24, 2020
2 parents 5e38a4e + 8278811 commit 27446ee
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getFunctions()

public function initErrorLogger($level = 'error', $includeScriptTag = true)
{
$url = addslashes($this->router->generate('nelmio_js_logger_log'));
$escapedUrl = $this->generateEscapedUrl();

$addStackTraceJs = <<<JS
var stackTraceJsModule = (function (basicModule) {
Expand All @@ -55,17 +55,17 @@ function(stackframes) {
if (req.readyState === 4) {
if (req.status >= 200 && req.status < 400) {
if (typeof console !== 'undefined' && typeof console.log === 'function') {
console.log('Error logged successfully to $url.');
console.log('Error logged successfully to $escapedUrl.');
}
} else {
if (typeof console !== 'undefined' && typeof console.log === 'function') {
console.log('POST to $url failed with status: ' + req.status);
console.log('POST to $escapedUrl failed with status: ' + req.status);
}
basicModule.callSimpleLogger(errorMsg, file, line, col, error);
}
}
};
req.open('post', '$url');
req.open('post', '$escapedUrl');
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify(
{
Expand Down Expand Up @@ -168,7 +168,7 @@ function(stackframes) {
basic.callSimpleLogger = function callSimpleLogger(errorMsg, file, line, col, error) {
var e = encodeURIComponent;
(new Image()).src = '$url?msg=' + e(errorMsg) +
(new Image()).src = '$escapedUrl?msg=' + e(errorMsg) +
'&level=$level' +
'&context[file]=' + e(file) +
'&context[line]=' + e(line) +
Expand Down Expand Up @@ -211,7 +211,7 @@ function(stackframes) {

public function initLogger($function = 'log', $includeScriptTag = true)
{
$url = addslashes($this->router->generate('nelmio_js_logger_log'));
$escapedUrl = $this->generateEscapedUrl();

$js = <<<JS
var $function = function(level, message, contextData) {
Expand All @@ -230,7 +230,7 @@ public function initLogger($function = 'log', $includeScriptTag = true)
context += '&context[' + e(key) + ']=' + e(customContext[key]);
}
}
(new Image()).src = '$url?msg=' + e(message) + '&level=' + e(level) + context;
(new Image()).src = '$escapedUrl?msg=' + e(message) + '&level=' + e(level) + context;
};
JS;

Expand All @@ -247,4 +247,9 @@ public function getName()
{
return 'nelmio_js_logger';
}

private function generateEscapedUrl(): string
{
return addslashes($this->router->generate('nelmio_js_logger_log', [], UrlGeneratorInterface::ABSOLUTE_URL));
}
}

0 comments on commit 27446ee

Please sign in to comment.