IE11 截断控制台中的字符串

IE11 truncates string in console

我有一串可能有 20 或 30 行的字符串,我想在一个 console.log 调用中全部输出到控制台。这在 Chrome 中运行良好,但 IE11 会在控制台中截断大约一半的字符串。有什么办法可以防止这种情况?字符串类似于:

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

单个消息的 1024 个字符限制并非完全不合理。即使是这样,IE 也无法提供改变该限制的方法。

出于好奇,为什么这是一大块而不是单独调用 console。<a href="https://developer.mozilla.org/en-US/docs/Web/API/Console.log" rel="nofollow noreferrer">log()</a> grouped with console.<a href="https://developer.mozilla.org/en-US/docs/Web/API/Console.group" rel="nofollow noreferrer">group()</a>?

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

如果收到的消息是一个大字符串,您当然可以将其记录为更小的片段:

function processSlices(sliceable, sliceSize, callback) {
  var i = 0,
    n = sliceable.length,
    // handle string, array, and array-like
    fn = sliceable.slice || Array.prototype.slice;
  while (i < n) {
    callback(fn.call(sliceable, i, i += sliceSize));
  }
}
// small sample with sliceSize of 7
processSlices("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 7, console.log);
<script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script><script>console.config({maximize:true,timeStamps:false})</script><style>.as-console-wrapper{display:block;}</style>

我知道这是一个老问题,但只是发布以防万一这对找到它的人有帮助。

如果您像我一样只是想确保将所有正确的数据都放入一个字符串中以进行简单测试,您可以这样做。可能有点复杂,但是当我有一个长字符串需要检查是否正确形成并具有正确的内容时很有帮助。 您绝对可以将其与接受的(佳能)答案中提到的 console.group() 配对。

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

如果您应该记录的文本没有分成几行,或者行可能超过限制,您可以使用这个简单的函数来"format"日志输出:

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: 
    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");