Skip to content
This repository has been archived by the owner. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?

/**
* Class JsErrorLogger
* Simple js error logger for Bitrix
*
*
* @author Dmitry Panychev <panychev@code-craft.ru>
* @based Anton Dolganin post - <http://blog.d-it.ru/dev/logging-js-errors-on-the-server/>
* @version 1.0
* @package CodeCraft
* @category Debug, Logging
* @copyright Copyright © 2016, Dmitry Panychev
*/

namespace CodeCraft\Loggers;

use \Bitrix\Main\Context;

class JsErrorLogger {

/**
* JsErrorLogger constructor.
*
* @throws \Exception
*/
public function __construct() {
if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) {
throw new \Exception ('Bitrix prolog is not included');
}
}

/**
* Print onError script, it handle and send error
*
* @param $pathToAction
*/
public function watch($pathToAction) {
$js = '<script>
window.onerror = function (msg, url, line, col, exception) {
BX.ajax.get(\'' . $pathToAction . '\', {
data: {
msg: msg,
exception: exception,
url: url,
line: line,
col: col
}
});
}
</script>';

echo $js;
}

/**
* Log errors from request to file
*
* @use \Bitrix\Main\Context
* @use AddMessage2Log
*/
public function log() {
$request = Context::getCurrent()->getRequest();

if ($request->get('data')) {
AddMessage2Log(var_export($request->get('data')));
}
}
}
23 changes: 13 additions & 10 deletions local/templates/blank/header.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<? if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) {
die();
die();
}

use Bitrix\Main\Page\Asset;
use Bitrix\Main\Page\Asset, CodeCraft\Loggers\JsErrorLogger;

$asset = Asset::getInstance();
$logger = new JsErrorLogger();
$asset = Asset::getInstance();

$asset->addJs(SITE_TEMPLATE_PATH . '/js/project.js');
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="robots" content="noindex">
<?$APPLICATION->ShowHead()?>
<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<title><?$APPLICATION->ShowTitle();?></title>
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="robots" content="noindex">
<? $APPLICATION->ShowHead() ?>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<title><? $APPLICATION->ShowTitle(); ?></title>
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
</head>

<body>
<?$APPLICATION->ShowPanel();?>
<? $APPLICATION->ShowPanel(); ?>
<? $logger->watch('/local/tools/jsErrorLog.php') ?>
18 changes: 18 additions & 0 deletions local/tools/jsErrorLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?

if ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {
return;
}

define('NO_KEEP_STATISTIC', 'Y');
define('NO_AGENT_STATISTIC', 'Y');
define('PUBLIC_AJAX_MODE', true);
define('NOT_CHECK_PERMISSIONS', true);
define('LOG_FILENAME', $_SERVER['DOCUMENT_ROOT'] . '/logs/js_error.txt');

require($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php');

use CodeCraft\Loggers\JsErrorLogger;

$logger = new JsErrorLogger();
$logger->log();