<p>这对我有用。我只是以与其他模块相同的方式初始化/注册它,以避免循环导入</p>
<pre class="lang-py prettyprint-override"><code># admin/logger.py
import logging
from pymongo import monitoring
log = logging.getLogger()
log.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
class CommandLogger(monitoring.CommandListener):
# def methods...
class ServerLogger(monitoring.ServerListener):
# def methods
class HeartbeatLogger(monitoring.ServerHeartbeatListener):
# def methods
def initialize_logger():
monitoring.register(CommandLogger())
monitoring.register(ServerLogger())
monitoring.register(HeartbeatLogger())
monitoring.register(TopologyLogger())
</code></pre>
<pre class="lang-py prettyprint-override"><code># /app.py
from flask import Flask
from admin.toolbar import initialize_debugtoolbar
from admin.admin import initialize_admin
from admin.views import initialize_views
from admin.logger import initialize_logger
from database.db import initialize_db
from flask_restful import Api
from resources.errors import errors
app = Flask(__name__)
# imports requiring app
from resources.routes import initialize_routes
api = Api(app, errors=errors)
# Logger before db
initialize_logger()
# Database and Routes
initialize_db(app)
initialize_routes(api)
# Admin and Development
initialize_admin(app)
initialize_views()
initialize_debugtoolbar(app)
</code></pre>
<pre class="lang-py prettyprint-override"><code># /run.py
from app import app
app.run(debug=True)
</code></pre>
<p>然后在任何模块中</p>
<pre class="lang-py prettyprint-override"><code>from admin.logger import log
from db.models import User
# inside some class/view/queryset or however your objects are written...
log.info('Saving an item through MongoEngine...')
User(name='Foo').save()
</code></pre>
<p>我现在想弄清楚的是如何将Flask DebuggerToolbar的日志面板和来自这些监听器的监控消息集成在一起</p>