0

I have done all kinds of logging through .ini file and all are working fine but in HttpHandler I am not getting log message into my route.

This is my logging.ini file:

[loggers]
keys=root

[logger_root]
level=DEBUG
handlers=httpHandler

[formatters]
keys=simple

[formatter_simple]
format=%(asctime)s [%(levelname)s] %(name)s: %(message)s

[handlers]
keys=httpHandler

[handler_httpHandler]
class=handlers.HTTPHandler
formatter=simple
args=('localhost:8080','/httplogtest', 'POST')

And this is my logger.py file:

import logging

from logging.config import fileConfig

fileConfig('logging.ini')
logger = logging.getLogger()

# This message should be passed to http call
logger.info("This is INFO log")

And I want to receive this log message into my request. I just want to do this functionality through .ini file without any custom handler.

from flask import Flask

app = Flask(__name__)

@app.route('/httplogtest', methods=['GET', 'POST'])
def handle_http_log():    
   # I need to print this debug message here
   print('Error log should be here')

It's not a duplicate question. I searched many resources but I didn't get my actual workaround to do httphandler logging from only .ini file.

  • Can you please elaborate on "unable to run Http handler"? You get an error (if yes, what is the error), you do not get the log, something else? Also can you edit your question adding the entire file in which you have `@app.route('/httplogtest', methods=['GET', 'POST'])`? – Nikolaos Chatzis Apr 22 '21 at 20:16
  • I am unable to get the log message into my request. I am updating route file now and sharing here again – Kamran Ahmed Khan Apr 22 '21 at 20:36
  • @NikolaosChatzis can you check now please ? I just want to receive this log message in my request, it is hitting the url but I don't know how to pass and receive value – Kamran Ahmed Khan Apr 22 '21 at 20:40

1 Answers1

0

You are missing a return in your application file and a way to obtain the log. You can try (see also Get the data received in a Flask request):

$ cat app.py 
from flask import Flask, request
import json

app = Flask(__name__)

@app.route('/httplogtest', methods=['POST', 'GET'])
def handle_http_log(): 
    print(f'Error log should be here: {request.form.get("msg")}')
    print(json.dumps(request.form))
    return ""    

if __name__ == '__main__':
    app.run(host='localhost', port = 8080, debug=True)

$ python3 app.py 
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://localhost:8080/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 235-227-512
Error log should be here: This is INFO log
{"name": "root", "msg": "This is INFO log", "args": "()", "levelname": "INFO", "levelno": "20", "pathname": "logger.py", "filename": "logger.py", "module": "logger", "exc_info": "None", "exc_text": "None", "stack_info": "None", "lineno": "9", "funcName": "<module>", "created": "1619127251.130069", "msecs": "130.06901741027832", "relativeCreated": "18.280029296875", "thread": "4653424064", "threadName": "MainThread", "processName": "MainProcess", "process": "82830"}
127.0.0.1 - - [22/Apr/2021 23:34:11] "POST /httplogtest HTTP/1.1" 200 -
Nikolaos Chatzis
  • 1,947
  • 2
  • 8
  • 17
  • Thanks a lot. It worked successfully. Now I am receiving log message in my request through request.form.get("msg"). – Kamran Ahmed Khan Apr 23 '21 at 14:42
  • I want to get in specified format that is present in .ini file. It is just passing simple text without specified format. Is there any way ? – Kamran Ahmed Khan Apr 23 '21 at 14:59
  • Great please consider accepting and upvoting if the answer was helpful for you (see https://stackoverflow.com/help/someone-answers). Regarding the format see the Note in the docs (https://docs.python.org/3/library/logging.handlers.html#logging.handlers.HTTPHandler). I believe you'll need to extract what you need and generate a string that fits your needs. – Nikolaos Chatzis Apr 23 '21 at 15:03
  • I have less than 15 points that why I can't upvote. Can you upvote my question? may be I get some points and then can upvote your answer. Thanks for help. – Kamran Ahmed Khan Apr 27 '21 at 21:09