如何将console.log保存到文件?

时间:2014-03-04 10:43:12

标签: javascript cordova phonegap-plugins

我目前正在使用以下脚本将我的websql数据保存到我的phonegap html应用程序中的json格式的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;
}

2 个答案:

答案 0 :(得分:1)

您可以使用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);
}

答案 1 :(得分:0)

您应该尝试将日志框架实现为http://log4javascript.org/。 console.log将始终登录控制台。使用log4javascript,将日志保存在文件中。