简体   繁体   English

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

[英]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. 我目前正在使用以下脚本在我的phonegap html应用程序中以json格式将websql数据保存到consol.log。 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 您可以使用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);
}

you should try implementing a logging framework as http://log4javascript.org/ . 您应该尝试将日志记录框架实现为http://log4javascript.org/ console.log will always log on the console. console.log将始终登录控制台。 using log4javascript, will save your logs in a file. 使用log4javascript,会将您的日志保存在文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 JavaScript 中检查 consol.log 和播放声音 - How to check consol.log and play sound in JavaScript 我如何使用此代码 consol.log countRight? - How do i consol.log countRight with this code? 如何将node.js控制台日志保存到.json文件 - how to save node js consol log to .json file JavaScript function 执行 consol.log(var) 但不执行 var2 = var - JavaScript function does consol.log(var) but doesn't var2 = var 代码仅在调试器模式下起作用,断点在consol.log上否则不起作用 - code works only in debugger mode with break point at consol.log doesn't work otherwise 如何获取 Firebase Json api,我得到了响应,我可以解决它,但是 FlatList 的问题 - How to fetch Firebase Json api, I got response and i can consol log it, But the problem with FlatList 如何将winston日志文件保存到mysql数据库中? - How to save winston log file into mysql database? CSSLint:如何将日志从控制台日志保存到文件中 - CSSLint : How to save log from console log into file 将电子devtools中的日志保存到文件中 - Save the log in electron devtools to a file 如何将javascript变量或console.log(变量)保存到文件中 - How to save javascript variable or console.log(variable) to a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM