在Flask Socket.io中禁用记录器 [英] Disabling logger in Flask Socket.io
问题描述
我有一个使用Flask和FlaskSocket.IO 2.8.4的应用程序.初始化SocketIO时,我正在使用
I have an application using Flask and FlaskSocket.IO 2.8.4. When I initialise SocketIO, I am using
#[...]
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
行:
Anyway, the output in my log has a lot of emit
, receive
and handle_event
lines:
[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
函数:
I am using the emit
function this way:
# 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
可以防止出现这些重复性较大的消息,但事实并非如此.如果有人可以帮助我,我将非常感激.
I would think that the argument logger=False
when instantiating the SocketIO
object prevents these big repetitive messages, but it doesn't. I would be very thankful if someone can help me with this.
S.
添加的信息:socketio.run
时我已经尝试使用选项log_output=False
,但是结果完全相同.
Added info: I already tried with the option log_output=False
when socketio.run
, but the result is exactly the same.
推荐答案
您应用的日志记录更改不起作用的原因是,您通过logging.basicConfig
应用的全局更改具有优先权.
The reason the logging changes you are applying don't work is that the global changes you've applied via logging.basicConfig
take precedence.
在这里,您可以覆盖这些默认值,仅用于Socket.IO记录器:
Here is how you can override those defaults, just for the Socket.IO logger:
logging.getLogger('socketio').setLevel(logging.ERROR)
logging.getLogger('engineio').setLevel(logging.ERROR)
这会将两个软件包都设置为仅记录错误.
This will set both packages to log only errors.
这篇关于在Flask Socket.io中禁用记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!