IE11 truncates string in console

人走茶凉 提交于 2019-12-12 09:33:09

问题


I have a string of maybe 20 or 30 lines i'd like to output to the console all in one console.log call. This works great in Chrome, but IE11 truncates about half of the string in the console. Any way to prevent this? The string is something like:

-----------------------------------------
Wed Jan 7 20:41:16 GMT-0700 2015 530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
-----------------------------------------
41:17:181 - Initiating recording...
41:17:233 - Creating NetStream...
41:17:240 - NetStream created.
41:17:240 - Recording ready.
-----------------------------------------
Wed Jan 7 20:41:16 GMT-0700 2015 NetConnectionQueue - rtmp://AMS2alt.commercialtribe.net/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
-----------------------------------------
41:16:867 - Initializing...
41:16:868 - Creating negotiator...
41:17:175 - Connection success: rtmp://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
41:17:175 - Connection added to queue.
41:17:182 - Connection request recieved...
41:17:183 - Connection request fulfilled.
41:17:452 - Connection success: rtmp://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
41:17:452 - Connection added to queue.
41:18:503 - -----------------------------------------
Wed Jan 7 20:41:16 GMT-0700 2015 NetNegotiator Log
-----------------------------------------
41:16:890 - Attempting: rtmp://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
41:17:174 - Negotiator reset
41:17:194 - Attempting: rtmp://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
41:17:282 - Attempting: rtmp://AMS2alt.commercialtribe.net:443/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
41:17:339 - Attempting: rtmp://AMS2alt.commercialtribe.net:80/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
41:17:400 - Attempting: rtmpt://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
41:17:451 - NetConnection attempt failed: rtmp://AMS2alt.commercialtribe.net:443/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9
41:17:452 - Negotiator reset

回答1:


A 1024 character limit on a single message isn't exactly unreasonable. Even if it was, IE offers no means of altering that limitation.

Out of curiosity, why is this one big block rather than individual calls to console.log() grouped with console.group()?

console.group("Wed Jan 7 20:41:16 GMT-0700 2015 530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:17:181 - Initiating recording...");
console.log("41:17:233 - Creating NetStream...");
console.log("41:17:240 - NetStream created.");
console.log("41:17:240 - Recording ready.");
console.groupEnd();
console.group("Wed Jan 7 20:41:16 GMT-0700 2015 NetConnectionQueue - rtmp://AMS2alt.commercialtribe.net/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:16:867 - Initializing...");
console.log("41:16:868 - Creating negotiator...");
console.log("41:17:175 - Connection success: rtmp://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:17:175 - Connection added to queue.");
console.log("41:17:182 - Connection request recieved...");
console.log("41:17:183 - Connection request fulfilled.");
console.log("41:17:452 - Connection success: rtmp://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:17:452 - Connection added to queue.");
console.groupEnd();
console.group("Wed Jan 7 20:41:16 GMT-0700 2015 NetNegotiator Log");
console.log("41:16:890 - Attempting: rtmp://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:17:174 - Negotiator reset");
console.log("41:17:194 - Attempting: rtmp://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:17:282 - Attempting: rtmp://AMS2alt.commercialtribe.net:443/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:17:339 - Attempting: rtmp://AMS2alt.commercialtribe.net:80/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:17:400 - Attempting: rtmpt://AMS2alt.commercialtribe.net:1935/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:17:451 - NetConnection attempt failed: rtmp://AMS2alt.commercialtribe.net:443/recorder/530d8aa855df0c2d269a5a5853a47a049c52c9d83a2d71d9");
console.log("41:17:452 - Negotiator reset");
console.groupEnd();

If the message is received as one big string you could certainly parse it and log each line individually, i.e.:

 msg.split("\n").forEach(function(line) { console.log(line); }); // *real* parser pending



回答2:


I know this is an old question but just posting in case this helps anyone who finds it.

If you're like me and just wanted to make sure you're getting all the right data into a string for a simple test, you could do something like this. Maybe a little complicated but was helpful when I had a long string I needed to check was formed correctly and had the right content. You could definitely pair this with the console.group() mentioned in the accepted (canon's) answer too.

function consoleLog(str, blockSize) {
    // blockSize is a parameter only to support the tests.
    if (blockSize === undefined) {
        blockSize = 1024;
    }
    var limit = Math.floor(str.length / blockSize);
    for (var k = 0; k < limit+1; k++) {
      if (k == limit)
          console.log(str.substring(blockSize*k, str.length));
      else
          console.log(str.substring(blockSize*k, blockSize*(k+1)));
    }
}

// Tests.

// Should print:
// aaaaa
// bbbbb
// c
consoleLog('aaaaabbbbbc', 5);

// Should print:
// aaaaa
// bbbbb
consoleLog('aaaaabbbbb', 5);



回答3:


If the text you should log is not broken into lines, or the lines might be longer than the limit, you can use this simple function to "format" the log output:

function logMessage(title, message) {
    // make sure if the 'console' object and its methods are available,
    // to avoid issues, like your JS code is broken by logging.
    // see: https://stackoverflow.com/a/7742862/704022
    if (console && console.log && console.group && console.groupEnd) {
        var maxLength = 10; // set to 10 to enable easier testing, should be 1024
        console.group(title);
        for (var pos = 0; pos < message.length ; pos += maxLength) {
            console.log(message.substring(pos, pos + maxLength));
        }
        console.groupEnd();
    }
}

logMessage("Test", "0123456789012345678901234");



回答4:


pholpar: You are correct. I was seeing some inconsistent results with multiple group messages and thought the missing parameter would fix it (later found it did not). This was only occurring in IE11 and appears to be a race condition inherent to IE. If I put a 100ms delay between message logging the problem would never occur.

There is a reference of groupEnd having that parameter but seems to be isolated to IE and I haven't found any scenario where using it makes a difference. https://technet.microsoft.com/en-us/dn265070(v=vs.80)



来源:https://stackoverflow.com/questions/27844407/ie11-truncates-string-in-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!