2

So i have tried doing this a few different ways. i am trying to use the flask app.logger to save messages with MYSQL database without using the SQLalchdb handler (i dont use SQLalch for anything)

anyways.... when i use app.logger with a filehandler, everything works correctly

when i use my http handler outside of flask, and have the flask route programmed to catch the error, it also works

but when i use app.logger and add the http handler to it, it does not work. have a look at my code and tell me what your think

http_handler = logging.handlers.HTTPHandler(
'localhost:5000',
'/logs',
method='POST',
)
app.logger.setLevel(logging.DEBUG)
app.logger.addHandler(http_handler)

and this is the flask route:

@app.route('/logs' , methods = ["GET","POST"])
def logs():
print('caught it')
print(request.form)
return ""

Here is the route i am using to force and error:

@app.route('/home', methods=['POST'])
def index():
[some code]
ender_template("index.html")

as previous mentioned, if i set up a new logger (not flask app.logger) with the same HTTP handler above it works, so i assume the flask route is set up correctly. also as previously mentioned, the flask app.logger will work with a file handler

Any help is greatly appreciated

EDIT:: Was recently asked to describe how the following code responds, figured it could be good information for everyone trying to help.

When i force flask to have an error (changed "render_template" to "ender_template" i see the error in the python shell, but it is not getting passed to the route. also, after i force the error flask no longer allows me to GET/POST anything until its restarted. i dont get any exceptions or errors for the logging function

3
  • what if you remove "GET" from the methods list of the /logs route? Or specify method='GET' in the handler? Commented Jan 25, 2018 at 1:10
  • i tried removing GET from the methods list and changing the method in the handler. didnt work. Thanks though! Commented Jan 25, 2018 at 4:13
  • still looking for help with this issue Commented Jan 30, 2018 at 17:00

2 Answers 2

1

This works as expected:

from logging.handlers import HTTPHandler

http_handler = HTTPHandler('localhost:5000',
                           '/logs',
                           method='POST')

from flask import Flask, request
app = Flask(__name__)

@app.route('/logs', methods=['POST'])
def logs():
    import pdb;pdb.set_trace()

app.logger.addHandler(http_handler)
app.run(debug=True)

from another terminal:

curl --form hello=world localhost:5000/logs

from inside the /logs endpoint:

(pdb) print(request.form)
ImmutableMultiDict([('hello', u'world')])
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this, added everything but the "debug=True" Maybe i should have been more specific.. the http_handler is working when as long as i am not using app.logger when you tested your script, by use of curl command, you also were not using app.logger. in order to test using the app.logger you need to make an error in the actual flask application itself. for example: @app.route('/home', methods=['POST']) def index(): ender_template("index.html") to your flask app, and visit localhost:500/home you would actually be trigger the flask logger.
will add the edit to your code so you can maybe play around with it again? thank you!
0

I was able to solve this issue.. took me a few days, and i am kind of embarrassed.

So here is the deal. Flask does come with its own built in logger (app.logger) that you can add handles to, to store your logged data elsewhere

the catch is, you cannot use a HTTP handler that points to the same flask app. To solve this issue set up a completely different flask application, (also giving it a unique IP address and/or Port number) and point the http handler to that application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.