簡體   English   中英

如何將consol.log保存到文件?

[英]How to save the consol.log to a file?

我目前正在使用以下腳本在我的phonegap html應用程序中以json格式將websql數據保存到consol.log。 現在如何將其保存為可以在本地保存的實際文本文件?

    function dbError(e) {
        console.log("SQL ERROR");
        console.dir(e);
    }

    function backup(table) {
    var def = new $.Deferred();
    curatio.webdb.db.readTransaction(function(tx) {
        tx.executeSql("select * from "+table, [], function(tx,results) {
            var data = convertResults(results);
            console.dir(data);
            def.resolve(data);
        });
    }, dbError);

    return def;
}

$(document).on("click", "#doBackupBtn", function(e) {
    e.preventDefault();
    console.log("Begin backup process");

    $.when(
        backup("allergies")

    ).then(function(allergies, log) {
        console.log("All done");
        //Convert to JSON
        var data = {allergies:allergies};
        var serializedData = JSON.stringify(data);
        console.log(serializedData);
        (function(console){

console.save = function(data, filename){

    if(!data) {
        console.error('Console.save: No data')
        return;
    }

    if(!filename) filename = 'console.json'

    if(typeof data === "object"){
        data = JSON.stringify(data, undefined, 4)
    }

    var blob = new Blob([data], {type: 'text/json'}),
        e    = document.createEvent('MouseEvents'),
        a    = document.createElement('a')

    a.download = filename
    a.href = window.URL.createObjectURL(blob)
    a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
    a.dispatchEvent(e)
 }
})(console)

    });

});

    //Generic utility
function convertResults(resultset) {
    var results = [];
    for(var i=0,len=resultset.rows.length;i<len;i++) {
        var row = resultset.rows.item(i);
        var result = {};
        for(var key in row) {
            result[key] = row[key];
        }
        results.push(result);
    }
    return results;
}

您可以使用cordova filr插件編寫文本文件

document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
//
function onDeviceReady() {
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onRequestFileSystemSuccess, null);
}
function onRequestFileSystemSuccess(fileSystem) {
    alert("Got file system!");
    fileSystem.root.getDirectory("YourDirectoryName", { create: false}, onGotDir, null);

}

function onGotDir(dataDir) {
    alert("Got Directoy!");
    dataDir.getFile("data.txt", { create: true, exclusive: false}, onFileCreated, fail);
    alert("Got File!");
}

function onFileCreated(dataFile) {
    dataFile.createWriter(gotFileWriter, null);
}
function gotFileWriter(writer) {
    writer.seek(writer.length); // so that existing data wont be overridden
    writer.write(yourdata); 
}

function fail(error) {
    console.log(error.code);
}

您應該嘗試將日志記錄框架實現為http://log4javascript.org/ console.log將始終登錄控制台。 使用log4javascript,會將您的日志保存在文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM