编程语言
首页 > 编程语言> > python – 在Flask Socket.io中禁用记录器

python – 在Flask Socket.io中禁用记录器

作者:互联网

我有一个使用Flask和FlaskSocket.IO 2.8.4的应用程序.当我初始化SocketIO时,我正在使用

#[...]

logging.basicConfig(level=logging.DEBUG,format='[%(asctime)s][%(levelname)s] - %(funcName)s: %(message)s')
logger = logging.getLogger(__name__)
handler = logging.FileHandler(__builtin__.config['dir']['log_file_handler'])
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s][%(levelname)s] - %(funcName)s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

# Setting up flask external plugins
socketio = SocketIO(app, logger = False)

#[...]

if __name__ == '__main__':
socketio.run(app, port=8000)

无论如何,我的日志中的输出有很多emit,receive和handle_event行:

[2017-04-19 05:17:02,172][INFO] - receive: 1139f764dbe64a1ba6e1c8d95228400c: Received packet MESSAGE data 2/telescope,["AskForTimeEvent"]
[2017-04-19 05:17:02,173][INFO] - _handle_event: received event "AskForTimeEvent" from 1139f764dbe64a1ba6e1c8d95228400c [/telescope]
[2017-04-19 05:17:02,173][INFO] - emit: emitting event "clockEvent" to all [/telescope]
[2017-04-19 05:17:02,174][INFO] - send: 1139f764dbe64a1ba6e1c8d95228400c: Sending packet MESSAGE data 2/telescope,["clockEvent",{"hr":5,"sec":2,"min":17}]
[2017-04-19 05:17:03,287][INFO] - receive: 1139f764dbe64a1ba6e1c8d95228400c: Received packet MESSAGE data 2/telescope,["AskForTimeEvent"]
[2017-04-19 05:17:03,287][INFO] - _handle_event: received event "AskForTimeEvent" from 1139f764dbe64a1ba6e1c8d95228400c [/telescope]
[2017-04-19 05:17:03,288][INFO] - emit: emitting event "clockEvent" to all [/telescope]
[2017-04-19 05:17:03,288][INFO] - send: 1139f764dbe64a1ba6e1c8d95228400c: Sending packet MESSAGE data 2/telescope,["clockEvent",{"hr":5,"sec":3,"min":17}]

我这样使用emit函数:

# this inside a function of an arbitrary class, works fine, but writes a lot of log!
with self.app.test_request_context('/telescope'):
        self.socketio.emit('someEvent', json.dumps(arr), namespace='/telescope')

我认为在实例化SocketIO对象时,参数logger = False可以防止这些重复的重复消息,但事实并非如此.如果有人可以帮助我,我将非常感激.

S.

补充信息:我在socketio.run中尝试使用选项log_output = False,但结果完全相同.

解决方法:

您正在应用的日志记录更改不起作用的原因是您通过logging.basicConfig应用的全局更改优先.

以下是如何覆盖这些默认值,仅适用于Socket.IO记录器:

logging.getLogger('socketio').setLevel(logging.ERROR)
logging.getLogger('engineio').setLevel(logging.ERROR)

这将设置两个包以仅记录错误.

标签:python,flask,flask-socketio
来源: https://codeday.me/bug/20190828/1752621.html