mongodb Mongo Shell - 控制台/调试日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2334451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongo Shell - Console/Debug Log
提问by
Probably a dumb question. Experimenting with Mongo shell.
大概是个愚蠢的问题。试验 Mongo shell。
I want to do something like:
我想做类似的事情:
matt@linuxvm:~/mongodb-linux-i686-1.2.3/bin$ ./mongo
MongoDB shell version: 1.2.3
url: test
connecting to: test
Thu Feb 25 20:57:47 connection accepted from 127.0.0.1:37987 #3
type "help" for help
> function test() { debug.log("hello") }
> test()
Thu Feb 25 20:58:06 JS Error: ReferenceError: debug is not defined (shell):0
In other words, how can I output to the console when evaluating JS functions from within Mongo shell. I intend some admin / populate script that I'd like to output some nicety info.
换句话说,在 Mongo shell 中评估 JS 函数时如何输出到控制台。我打算一些管理/填充脚本,我想输出一些细节信息。
I've tried console.log, debug.log, log the obvious ones..
我试过console.log,debug.log,记录明显的..
回答by
I've found the answer, it is simply print
我找到了答案,很简单 print
print("sweetnesss");
回答by gbegley
I usually use scripts when interacting with the shell, so I wrote a basic Logging object that I "load(script)" into the shell, and then use the Logging object to call logging levels (debug,info,warn,error). The Logger object does use 'print' and 'printjson' at it's core. Also, it contains a basic sprintf, padding, record formatting, etc. If you are going to be doing any signifigant amount of script work with mongodb, I recommend this route. Debugging mongodb scripts feels like being back at intro to programming and debugging with print statements. Configurable logging makes this a little better, but I really miss being able to step through the code.
我通常在与 shell 交互时使用脚本,所以我写了一个基本的 Logging 对象,我“加载(脚本)”到 shell 中,然后使用 Logging 对象调用日志级别(调试、信息、警告、错误)。Logger 对象的核心是“print”和“printjson”。此外,它还包含一个基本的 sprintf、填充、记录格式等。如果您打算使用 mongodb 进行任何大量的脚本工作,我推荐这条路线。调试 mongodb 脚本感觉就像回到了使用打印语句进行编程和调试的介绍。可配置的日志记录使这更好一点,但我真的很怀念能够单步执行代码。