我想通过运行.py脚本来设置mongodb.conf文件中各个组件的日志级别。我通常只是对数据库使用基本的命令,如update_one或insert,没有任何问题。我得到的错误是:
"TypeError:'Collection‘对象不可调用。如果您打算在'Database’对象上调用'setloglevel‘方法,它将失败,因为不存在这样的方法。
我已经尝试过setLogLevel、set_log_level和其他一些网站。在pymongo文档或在线上找不到任何东西。
如何设置日志级别?可以吗?提前谢谢。
代码:
def setlvl():
## Connection to the Mongo Service ##
try:
conn=pymongo.MongoClient("myip_address")
print("Connected successfully!!!")
except pymongo.errors.ConnectionFailure:
print("Could not connect to MongoDB: %s" % e )
print("Connection:",conn)
## Connect to the DataBase ##
db = conn.mydatabase
print("database:", db)
## Update log level ##
loglvl = db.setloglevel
result=loglvl(5, "storage")
setlvl()发布于 2017-03-07 03:53:29
"setLogLevel“是在Javascript中实现的mongo外壳函数。您可以通过在mongo shell中键入其名称(不带括号)来查看任何mongo shell函数的实现:
> db.setLogLevel
function (logLevel, component) {
return this.getMongo().setLogLevel(logLevel, component);
}好的,那么让我们更深入地了解一下实现:
> db.getMongo().setLogLevel
function (logLevel, component) {
componentNames = [];
if (typeof component === "string") {
componentNames = component.split(".");
} else if (component !== undefined) {
throw Error("setLogLevel component must be a string:" + tojson(component));
}
var vDoc = {verbosity: logLevel};
// nest vDoc
for (var key, obj; componentNames.length > 0;) {
obj = {};
key = componentNames.pop();
obj[key] = vDoc;
vDoc = obj;
}
var res = this.adminCommand({setParameter: 1, logComponentVerbosity: vDoc});
if (!res.ok)
throw _getErrorWithCode(res, "setLogLevel failed:" + tojson(res));
return res;
}现在我们看到外壳程序在管理数据库上运行"setParameter“函数。请参阅setParameter文档:
https://docs.mongodb.com/manual/reference/command/setParameter/
因为"setParameter“是一个MongoDB命令,所以我们可以在PyMongo中运行它,就像我们在PyMongo中运行任何MongoDB命令一样:
from bson import SON
from pymongo import MongoClient
c = MongoClient()
result = c.admin.command(SON([
("setParameter", 1),
("logComponentVerbosity", {
"storage": {
"verbosity": 5,
"journal": {
"verbosity": 1
}
}
})]))
print(result)https://stackoverflow.com/questions/42632816
复制相似问题