Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 212
Sebasthian Ogalde Avatar asked Apr 19 '17 05:04

Sebasthian Ogalde


People also ask

What is flask-Socketio?

Flask-SocketIO gives Flask applications access to low latency bi-directional communications between the clients and the server.

What is sid in socketio?

sid is indeed a protected query parameter. I will update the documentation to make it clearer. Reference: https://github.com/socketio/engine.io-protocol#urls.

What is namespace in flask-Socketio?

A Namespace is a communication channel that allows you to split the logic of your application over a single shared connection. Possible use cases: you want to create an admin namespace that only authorized users have access to, so the logic related to those users is separated from the rest of the application.


1 Answers

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.

like image 55
Miguel Avatar answered Oct 19 '22 09:10

Miguel