6

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.

  • what is version of `socket-io`? because there is a bug wiht logging in version `0.8.6 `. Or Try you with `engineio_logge=False`. – julian salas Apr 19 '17 at 05:59
  • I checked and `python-socketio==1.7.1` and `python-engineio==1.2.3`. I tried your `engineio_logger=False` both in SocketIO object instantiation and `socketio.run` but didn't work. – Sebasthian Ogalde Apr 19 '17 at 06:20

1 Answers1

22

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.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152