有没有办法将网站上的日志(javascript日志)上传到服务器?

时间:2016-12-17 20:27:45

标签: javascript

我想将javascript日志上传到在线服务器(特别是任何例外)。有没有办法做到这一点?

感谢。

1 个答案:

答案 0 :(得分:0)

通过ajax将日志发送到服务器,例如jquery:

$.post("/log/save.php", 
    log: "Hello world", 
    function(){ 
        console.log("log saved") 
    }
);

然后在/log/save.php连接到数据库并保存到日志表将日志保存到文件:

<?php
$file = 'logs.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new log to the file
$current .= $_POST["log"]."\n"; //hello world.
// Write the contents back to the file
file_put_contents($file, $current);
?>