2017-03-06 9 views
0

mongodb.confファイルで、.pyスクリプトを実行して、さまざまなコンポーネントのログレベルを設定したいとします。私は通常、update_oneのようなデータベースや問題なく挿入する基本コマンドを使います。エラーは私が取得しています:PyMongoログレベルを設定する

「TypeError例外を: 『コレクション』オブ​​ジェクトが呼び出すことはできませんあなたがそのような方法が存在しないので、それが失敗している 『データベース』オブジェクトに 『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() 
を。。?。

答えて

0

"setLogLevel"は、Javascriptで実装されたmongoシェル関数です。 OK

> 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で任意のMongoDBのコマンドPyMongoに、我々は実行と同じようにそれを実行することができます:

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) 
関連する問題