Home » Php » javascript – Send log errors to a file

javascript – Send log errors to a file

Posted by: admin July 12, 2020 Leave a comment

Questions:

I developed a website for my graduation however it still only one thing I have do. What I want is when the script is installed on a website I want to send the name of the website who has installed my script, also whenever there is an error I want to send it to my website so for example:

This website installed my script

www.security-dz.com/myscript

I want to see the path + website in an other file in other website. For example:

www.getlog.com/mylogs.php

The purpose of this is keep my customers update and give them support and see the errors that happen so I can fix them in next updates.

How to&Answers:

You might want to take a closer look at the JQuery docs for ajax requests, so you can use a secure http connection for logging. This javascript code basically describes a function that sends the errors in text-format to your server-side script. This script can in turn write the error description to a file on the server. I’d recommend using a DB instead; That way you can easily write a web-client that displays all reported errors (and filters and the other good stuff).

You can extract the origin url from the referer [sic] field in the ajax http get-request on the server.

(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
}

Code has been validated with jshint. Please let me know if there are still issues, because I didn’t take the time to completely replicate your setup (setting up 2 different domains etc.)

Addendum: Some useful reading if you’re having issues with cross-domain messaging, JSON is not a subset of javascript, Cross-origin resource sharing, JSONP.

Answer:

What you could do is post both the name of the website that uses your script and the error code variable with AJAX through URL to your logging website, which will then get the variable and name from the URL and use these to add to your log.

You should, however, by using this tactic, also make use of some URL validation, otherwise this will leave you wide open to injection attacks.

Answer:

it is easy when your script installed , get the site info and send by socket and http request with get method and then recive it on your server.

for errors, php has some method to control error logs so custom it.