4

I tried everything in here: Disable console messages in Flask server but the only thing I was able to do it disable the *starting server thing.

I found what seems like its functions in the werkzeug module in the Serving.py:

def log_request(self, code='-', size='-'):
    msg = self.requestline
    code = str(code)

    if termcolor:
        color = termcolor.colored

        if code[0] == '1':    # 1xx - Informational
            msg = color(msg, attrs=['bold'])
        elif code[0] == '2':    # 2xx - Success
            msg = color(msg, color='white')
        elif code == '304':   # 304 - Resource Not Modified
            msg = color(msg, color='cyan')
        elif code[0] == '3':  # 3xx - Redirection
            msg = color(msg, color='green')
        elif code == '404':   # 404 - Resource Not Found
            msg = color(msg, color='yellow')
        elif code[0] == '4':  # 4xx - Client Error
            msg = color(msg, color='red', attrs=['bold'])
        else:                 # 5xx, or any other response
            msg = color(msg, color='magenta', attrs=['bold'])

    self.log('info', '"%s" %s %s', msg, code, size)


def log(self, type, message, *args):
    _log(type, '%s - - [%s] %s\n' % (self.address_string(),
                                     self.log_date_time_string(),
                                     message % args))

But even when putting pass in all these functions instead of the code, it printed them to the console. I'm really clueless. Does anyone have a working solution for this?

Yaniv K.
  • 237
  • 4
  • 12
  • 1
    Why do you need to do this? – roganjosh Dec 27 '18 at 19:31
  • I'm trying to monitor site activity using the console in real time, and it just spams it. Even if disabling it is stupid, why can't one disable it? – Yaniv K. Dec 27 '18 at 19:36
  • Well, if you were to host on a proper production server you wouldn't get any logs. It is, after all, a development server – roganjosh Dec 27 '18 at 19:39
  • _But even when putting pass [...] it printed them to the console_ How do you launch your flask app? Emptying those 2 func should totally disable the `127.0.0.1 - - [01/Dec/2018 00:00:00] "POST / HTTP/1.1" 200 -` messages (even if it's not a correct way to do so, you should disable the `logging.getLogger('werkzeug')` logger), but your WSGI server or a web-server still may be emitting those messages. – Fine Dec 28 '18 at 08:44
  • As mentioned in the original post, I have tried to simply disable the logger but with no success. How I launch the app: `app = Flask(__name__) mysql = MySQL(app) socketio = SocketIO(app) if __name__ == '__main__': socketio.run(app , debug=debug, host=host, port=port)` – Yaniv K. Dec 29 '18 at 09:55
  • You mentioned you got no success disabling the logger, can you share what you're still seeing after you disabled the logger? – Jeff Welling Mar 22 '19 at 22:46
  • 127.0.0.1 - - [01/Dec/2018 00:00:00] "POST / HTTP/1.1" 200 - messages – Yaniv K. Mar 23 '19 at 13:20

1 Answers1

2
import flask.cli    
flask.cli.show_server_banner = lambda *args: None

import logging
logging.getLogger("werkzeug").disabled = True
Mike Conigliaro
  • 1,144
  • 1
  • 13
  • 28