2017-03-06 152 views
0

我想通過運行.py腳本來設置mongodb.conf文件中各種組件的日誌級別。我通常只是使用基本命令數據庫像update_one或插入沒有問題。錯誤我得到:PyMongo設置日誌級別

「類型錯誤:‘集合’對象不是可調用的。如果你的意思是叫‘setloglevel’的方法是失敗的,因爲沒有這樣的方法存在一個‘數據庫’對象

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() 
:提前set_log_level和一些別人不能罰款任何pymongo文檔或在網上

如何設置日誌級別,並有可能由於

代碼;我試過setLogLevel。?。

回答

0

「setLogLevel」是一個用Javascript實現的mongo shell函數。您可以通過在蒙戈shell中鍵入它的名字就能看出任何蒙戈外殼功能的實現,沒有括號:

> db.setLogLevel 
function (logLevel, component) { 
    return this.getMongo().setLogLevel(logLevel, component); 
} 

OK,讓我們更深入,真正看到了實現:

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

現在我們請參閱shell在管理數據庫上運行「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)