vishavjeet

singh.vishavjeet11@gmail.com

Get Logs error message, linenumber and path files using flask

Posted on May 13, 2022




  • Create Simple Log file


import logging
logging.basicConfig(filename="logs.txt",
filemode='a',
format='%(asctime)s\t%(lineno)d\t%(levelname)s\t%(pathname)s\t%(name)s\t%(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
msg = "NameError: name filename' is not defined "
logging.info(msg)
logging.warn(msg)
logging.error(msg)
logging.critical(msg)


  • Logs file created in logs.txt like as:
    20:05:16 10 INFO C:\Users\vishavjeet\Documents\mywork_space\flask_demo\python_log.py root NameError: name filename' is not defined
    20:05:16 11 WARNING C:\Users\vishavjeet\Documents\mywork_space\flask_demo\python_log.py root NameError: name filename' is not defined
    20:05:16 12 ERROR C:\Users\vishavjeet\Documents\mywork_space\flask_demo\python_log.py root NameError: name filename' is not defined
    20:05:16 13 CRITICAL C:\Users\vishavjeet\Documents\mywork_space\flask_demo\python_log.py root NameError: name filename' is not defined


write the code for post api using flask


from flask import Flask, request, jsonify
import os
# Create a Flask app for post api
app = Flask(__name__)
@app.route('/logs', methods= ['POST', 'GET'])
def logs_func():

response = {
"status": True,
"message": "Logs data found!",
"data":[]
}

if request.method == 'POST':
file_name = request.json['filename']
i = 1
result = {}
if os.path.exists(file_name):
with open('logs.txt') as f:
lines = f.readlines()
for line in lines:
r = line.split('\t')

if r[2]== 'ERROR':

result[i] = {
'timestamp': r[0],
'linenumber' : r[1],
'name': r[4],
'pathname': r[3],

'message': r[5]
}
i += 1
response['data']= result
return jsonify(response)
else:
response['status']= False
response['message']= "File not found!"
return jsonify(response)
else:
response['status']= False
response['message']= "Only Post method is not allow"
return jsonify(response)

if __name__ == '__main__':
app.run(debug = True)



  • API Response:


{
"data": {
"1": {
"linenumber": "12",
"message": "NameError: name filename' is not defined \n",
"name": "root",
"pathname": "C:\\Users\\vishavjeet\\Documents\\mywork_space\\flask_demo\\python_log.py",
"timestamp": "20:05:16"
},
"2": {
"linenumber": "12",
"message": "NameError: name filename' is not defined \n",
"name": "root",
"pathname": "C:\\Users\\vishavjeet\\Documents\\mywork_space\\flask_demo\\python_log.py",
"timestamp": "20:05:18"
},
"3": {
"linenumber": "12",
"message": "NameError: name filename' is not defined \n",
"name": "root",
"pathname": "C:\\Users\\vishavjeet\\Documents\\mywork_space\\flask_demo\\python_log.py",
"timestamp": "20:05:20"
},
"4": {
"linenumber": "13",
"message": "NameError: name filename' is not defined \n",
"name": "root",
"pathname": "C:\\Users\\vishavjeet\\Documents\\mywork_space\\flask_demo\\python_log.py",
"timestamp": "20:08:12"
}
},
"message": "Logs data found!",
"status": true
}


Sign In for comment and like the post.