将日志错误发送到文件

时间:2014-03-29 11:01:53

标签: javascript php jquery syntax-error

我为毕业开发了一个网站,但它仍然只有我做过的一件事。我想要的是当脚本安装在网站上时我想发送安装了我脚本的网站的名称,也就是每当出现错误我想将它发送到我的网站时,例如:

本网站安装了我的脚本

www.security-dz.com/myscript

我想在其他网站的其他文件中看到路径+网站。例如:

www.getlog.com/mylogs.php

这样做的目的是让我的客户更新并给予他们支持并查看发生的错误,以便我可以在下次更新中修复它们。

3 个答案:

答案 0 :(得分:2)

您可能需要仔细查看JQuery docs for ajax requests,以便使用安全的http连接进行日志记录。这个javascript代码基本上描述了一个函数,它将文本格式的错误发送到服务器端脚本。此脚本可以将错误描述写入服务器上的文件。我建议改用DB;这样,您就可以轻松编写一个显示所有报告错误(以及过滤器和其他好东西)的Web客户端。

您可以从服务器上的ajax http get-request中的referer [sic]字段中提取原始URL。

(function () { // function operator, in case console doesn't exist
    !console ?
        (console = {}) : console;
    !console.log ?
        (console.log = function () { }) : console.log;
    !console.info ?
        (console.info = console.log) : console.info;
    !console.error ?
        (console.error = console.log) : console.error;
}());
// Uses JQuery
function reportError (errDesc) {
    var path = "www.getlog.com/mylogs.php";
    $.ajax({
        url: path,
        type: "GET",
        async: true,
        cache: false,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        crossDomain: true,
        data: errDesc,
        dataType: "jsonp",
        error: function (req, type, errObj) {
            console.error("Reporting error failed: " + type + "\nAt url: " + path + "\n" + errObj);
        // In case you need to debug the error reporting function
        },
        succes: function (res) {
            console.info("Reported error to server:\nRequest:" + errDesc + "\nResponse: " + res);
        // extra error logging facility on client-side, invisible to most users
        },
        global: false // prevent triggering global ajax event handlers
    });
    return errDesc; // in case you want to reuse the errDesc
}

代码已使用jshint验证。如果仍有问题,请告诉我,因为我没有花时间完全复制您的设置(设置2个不同的域等)。

附录:如果您遇到跨域讯息问题,请JSON is not a subset of javascriptCross-origin resource sharingJSONP进行一些有用的阅读。

答案 1 :(得分:0)

您可以做的是将使用您脚本的网站名称和带有AJAX的错误代码变量通过URL发布到您的日志记录网站,然后将从URL获取变量和名称并使用这些来添加到你的日志。

但是,您应该使用这种策略,也可以使用一些URL验证,否则这将使您对注入攻击持开阔态度。

答案 2 :(得分:0)

安装脚本时很容易,获取站点信息并通过套接字发送,使用get方法发送http请求,然后在服务器上重新发送。

对于错误,php有一些控制错误日志的方法,所以自定义它。