简体   繁体   中英

How to save the consol.log to a file?

I am currently using the following script to save my websql data to a consol.log in json format in my phonegap html application. How do I now get this to be saved as an actual text file that I can save locally?

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

You can make use of cordova filr plugin to write a text file

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);
}

you should try implementing a logging framework as http://log4javascript.org/ . console.log will always log on the console. using log4javascript, will save your logs in a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM