简体   繁体   中英

Disabling logger in Flask Socket.io

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)

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

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')

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.

Added info: I already tried with the option log_output=False when socketio.run , but the result is exactly the same.

The reason the logging changes you are applying don't work is that the global changes you've applied via logging.basicConfig take precedence.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM