Tag: console.log

从控制台提取console.log方法

考虑到console没有被覆盖,并引用本地对象, console.log方法(可能还有其他)是从console对象中提取的 var log = obj.log = console.log; // instead of console.log.bind(console) log(…); obj.log(…); 在浏览器和节点兼容性方面100%安全吗? 有大量的JS例子(可能太说明了)绑定console.log表明它可能不是。

console.log中的%j说明符排除了一些属性

我最近试图在node.js加载bson (二进制JSON)模块。 API文档不清楚,所以我认为检查对象会帮助我。 检查结果令人困惑。 最后我发现这是因为我正在使用“谎言”的%j它不会打印所有对象的字典键! (我使用Python的术语“属性”作为引用的点,而“字典键”用括号引用的东西,因为我不知道这些东西在JS中的正确名称。 这里是一个例子: var bson = require("bson"); console.log("bson as %%j: %j", bson); console.log("bson as console.log: ", bson); 这里是输出: bson as %j: {"BSONPure":{},"BSONNative":{}} bson as console.log: { BSONPure: { Code: [Function: Code], Symbol: [Function: Symbol], BSON: { [Function: BSON] BSON_INT32_MAX: 2147483647, BSON_INT32_MIN: -2147483648, BSON_INT64_MAX: 9223372036854776000, BSON_INT64_MIN: -9223372036854776000, … 我以为x.key与x["key"]是一样的,这意味着JS中的属性和字典键是“同一件事物”。 我在实验后发现BSON.BSONPure是{} ,然而BSON.BSONPure.BSON和BSON.BSONPure["BSON"]是一个function对象! 这使我相信,无论"%j"做什么,都必须排除一些密钥。 […]

Node.js:console.log消息不显示,如果方法抛出exception…为什么?

在Node.js中,如果我有引发exception的方法,那么来自该方法的console.log语句不会触发。 我认识到,在下面这个简单的testing案例中,我应该从readFileSync调用中捕获exception,否则就会防御它。 只是好奇,如果有人可以解释我的行为。 简单的testing案例: var fs = require('fs'); function readAFileThatDoesntExist(filename) { console.log(filename); fs.readFileSync(filename); } console.log("We're about to read a file that doesn't exist!"); readAFileThatDoesntExist("afile"); 输出: $ node test.js We're about to read a file that doesn't exist! fs.js:338 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode); ^ Error: ENOENT, no such file or directory 'C:\blog\projects\bloggen\scripts\afile' at Object.fs.openSync (fs.js:338:18) at […]

为什么for-of-loop还没有完成运行,跳出来运行console.log?

运行环境 Visual Studio Code v1.15.1自己编译 Node.js v8.2.1 操作系统:Windows 10 描述 我想在源代码和sorting之间添加“sorting后”,如下所示: 但是控制台偶尔会显示: 为什么for-of-loop还没有完成运行,跳出来运行console.log? 源代码: // Random to generate double digits. function getRandom() { return Math.round(Math.random() * 100); } // Writing data to the array. var score = [ ["a", getRandom()], ["b", getRandom()], ["c", getRandom()], ["d", getRandom()], ["e", getRandom()] ]; console.log("Before sort:"); // Print source arry […]

Node.js OpsWorks图层控制台日志

我有一个Opsworks堆栈与Node.js图层和Node.js应用程序。 我想知道是否有人知道在Ubuntu 14.04LTS实例从我的应用程序的控制台日志正在打印到哪里。 我知道opsworks使用monit来运行我的应用程序,但我不知道它在哪里输出日志。 谢谢!

logging捕获的错误与Node.js中的未捕获错误完全相同

比方说,我有以下文件error.js : // this is invalid code var a: 0; 然后在我的main.js我需要该文件: require("./error.js"); 我从Node.js得到一个很好,简单易懂的错误消息: $ node main.js /home/jfischer/error.js:2 var a: 0; ^ SyntaxError: Unexpected token : at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:413:25) at Object.Module._extensions..js (module.js:452:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require (module.js:384:17) at Object.<anonymous> (/home/jfischer/main.js:1:63) at Module._compile (module.js:434:26) at Object.Module._extensions..js (module.js:452:10) […]

为什么在使用笑话testing节点应用程序时看不到控制台日志输出

我是新玩笑testing,我似乎无法看到模块我想testing的控制台输出。 我-module.js: var _ = require('underscore'); exports.filter = function(data) { if(_.isArray(data)) { console.log("Data is: " + data); data = data[0]; } return data; } 我的模块,test.js: jest.dontMock('../my-module'); var testdata = [{label: "test"}, {id: 5}]; describe('test my module', function(){ it('changes some data' , function(){ var transformedData = require('../my-module').filter(testdata); expect(transformedData).toBe(testdata[0]); }); }); 为什么在“my-module.js”中input我的console.log输出?

在nodejs中的console.log循环

我的MCVE是以下 var i = 0; for(;;) console.log(i++) 当我这样做,在某个特定的时刻,我的nodejs只是停止打印东西,给我一个看起来像这样的输出 […] 684665 684666 684667 然后,我得到这个: <— Last few GCs —> 69097 ms: Scavenge 1397.2 (1456.7) -> 1397.2 (1456.7) MB, 0.8 / 0 ms (+ 1.7 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep]. 70462 ms: Mark-sweep 1397.2 (1456.7) -> 1396.0 (1456.7) MB, […]

节点与Chrome,将console.log分配给一个variables?

当我将console.log分配给node.js中的variables时,它工作正常, var l = console.log l(1) # outputs 1 但是,如果我在Chromium 30的开发工具中做同样的事情, var l = console.log l(1) # TypeError: Illegal invocation 如何在Chromium的开发工具中不起作用? 为什么我得到, TypeError:非法调用

在txt文件中的Node.js console.log()

我还有一个问题( 最后一个问题 )。 目前我正在做一个Node.js项目,在这里我有很多 console.log()函数。 到目前为止,这个工作还行,但我也希望写入控制台的所有内容都写在日志文件中。 有人可以帮帮我吗? 例如: Console.log('The value of array position [5] is '+ array[5]); 在我的真实代码中它多一点,但这应该给你一个想法。 谢谢你,希望。